PHPackages                             nezarfadle/jsonschemavalidator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. nezarfadle/jsonschemavalidator

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

nezarfadle/jsonschemavalidator
==============================

Json Schema Validator

1.0(10y ago)030MITPHP &gt;= 5.1.0

Since Jun 21Compare

[ Source](https://github.com/nezarfadle/php-json-schema-validator)[ Packagist](https://packagist.org/packages/nezarfadle/jsonschemavalidator)[ RSS](/packages/nezarfadle-jsonschemavalidator/feed)WikiDiscussions Synced today

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

PHP Json Schema Validator
=========================

[](#php-json-schema-validator)

Installation
============

[](#installation)

`composer require nezarfadle/jsonschemavalidator`

Usage
=====

[](#usage)

Make sure that you import all the needed classes:

```
use Json\Validation\JsonSchemaValidator;
use Json\Validation\Validators\ArrayValueValidator;
use Json\Validation\Validators\NumericValueValidator;
use Json\Validation\Validators\ObjectValidator;
use Json\Validation\Validators\StringValueValidator;
use Json\Validation\Validators\RequiredStringValueValidator;

// Exceptions
use Json\Validation\Exceptions\EmptySchemaException;
use Json\Validation\Exceptions\InvalidJsonPayloadException;
use Json\Validation\Exceptions\EmptyPayloadException;
use Json\Validation\Exceptions\NotExistsPropertyException;
```

1. Simple Validation:
---------------------

[](#1-simple-validation)

```
$json =  new NumericValueValidator() // validate whether the id is numeric or not
];

try {

	$validator->validate( $json, $schema );
	echo "Valid numeric value";

}
catch (EmptySchemaException $e) {}
catch (InvalidJsonPayloadException $e) {}
catch (EmptyPayloadException $e) {}
catch (\InvalidArgumentException $e) {}
catch (NotExistsPropertyException $e) {}
```

2. Nested Validation:
---------------------

[](#2-nested-validation)

```
$json =  new ObjectValidator(), // validate whether item4 is an object
	'data.item1.item2.item3.item4.title' => new StringValueValidator() // validate whether item4.title is a string
];

try {

	$validator->validate( $json, $schema );
	echo "Valid nested payload ";

}
catch (EmptySchemaException $e) {}
catch (InvalidJsonPayloadException $e) {}
catch (EmptyPayloadException $e) {}
catch (\InvalidArgumentException $e) {}
catch (NotExistsPropertyException $e) {}
```

3. Multiple Validation:
-----------------------

[](#3-multiple-validation)

```
$json =  [ new StringValueValidator(), new RequiredStringValueValidator() ]
];

try {

  $validator->validate( $json, $schema );
  echo "Valid Title";

}
catch (EmptySchemaException $e) {}
catch (InvalidJsonPayloadException $e) {}
catch (EmptyPayloadException $e) {}
catch (\InvalidArgumentException $e) {}
catch (NotExistsPropertyException $e) {}
```

4. JsonApi Spec Use Case:
-------------------------

[](#4-jsonapi-spec-use-case)

```
$json =  new ObjectValidator(), // validate whether the data is an object
  'data.attributes' => new ObjectValidator(), // validate whether the attributes is an object
  'data.attributes.title' => new StringValueValidator(), // validate whether the title is string
  'data.attributes.src' => new StringValueValidator(), // validate whether the src is string
];

try {

  $validator->validate( $json, $schema );
  echo "Valid json api spec payload ";

}
catch (EmptySchemaException $e) {}
catch (InvalidJsonPayloadException $e) {}
catch (EmptyPayloadException $e) {}
catch (\InvalidArgumentException $e) {}
catch (NotExistsPropertyException $e) {}
```

How to write your own Assertion class
-------------------------------------

[](#how-to-write-your-own-assertion-class)

1. You have to implement `php Json\Validation\Interfaces\IJsonValidator`

```
interface IJsonValidator
{
  public function validate( $propertyName, $value );
}
```

```
$propertyName: The property name you want to validate
$value: The actual value you want to validate
```

\####Greater than ten Validator

```
class GreaterThanTenValidator implements Json\Validation\Interfaces\IJsonValidator
{
    public function validate( $propertyName, $value )
    {
         if( !is_numeric( $value ) || $value validate( $json, $schema );
  echo "Greater than 10";

}
catch (EmptySchemaException $e) {}
catch (InvalidJsonPayloadException $e) {}
catch (EmptyPayloadException $e) {}
catch (\InvalidArgumentException $e) {}
catch (NotExistsPropertyException $e) {}
```

\#Json Schema Validator components:

### Json Validator:

[](#json-validator)

```
Json\Validation\JsonSchemaValidator
```

### Interfaces:

[](#interfaces)

```
Json\Validation\Interfaces\IJsonSchemaValidator
```

### Assertion Classes ( more will be added in future or [take a look at how you can create your own assertion class](#how-to-write-your-own-assertion-class) )

[](#assertion-classes--more-will-be-added-in-future-or-take-a-look-at-how-you-can-create-your-own-assertion-class-)

```
Json\Validation\Validators\ArrayValueValidator
Json\Validation\Validators\NumericValueValidator
Json\Validation\Validators\ObjectValidator
Json\Validation\Validators\StringValueValidator
Json\Validation\Validators\RequiredStringValueValidator
```

### Exceptions

[](#exceptions)

```
Json\Validation\Exceptions\EmptySchemaException
Json\Validation\Exceptions\InvalidJsonPayloadException
Json\Validation\Exceptions\EmptyPayloadException
Json\Validation\Exceptions\NotExistsPropertyException
```

Exceptions execution order:
---------------------------

[](#exceptions-execution-order)

\####1. EmptySchemaException

This exception will be thrown if the given

```
$schema = [];
$validator->validate( $json, $schema );
```

\####2. InvalidJsonPayloadException

This exception will be thrown if the given json payload is invalid

```
$json = 'invalid json payload';
$validator->validate( $json, $schema );
```

\####3. EmptyPayloadException

This exception will be thrown if the given json payload is empty object

```
$json = '{}';
$validator->validate( $json, $schema );
```

\####4. InvalidArgumentException

This exception will be thrown if the validation failed

\####5. NotExistsPropertyException

This exception will be thrown if the given property is not exists in the json payload

```
$json =  new StringValueValidator()
];

$validator->validate( $json, $schema ); // NotExistsPropertyException will be thrown
```

How to run the test
===================

[](#how-to-run-the-test)

`phpunit`

###  Health Score

25

—

LowBetter than 35% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3661d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6154103?v=4)[Nezar Fadle](/maintainers/nezarfadle)[@nezarfadle](https://github.com/nezarfadle)

---

Top Contributors

[![nezarfadle](https://avatars.githubusercontent.com/u/6154103?v=4)](https://github.com/nezarfadle "nezarfadle (19 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/nezarfadle-jsonschemavalidator/health.svg)

```
[![Health](https://phpackages.com/badges/nezarfadle-jsonschemavalidator/health.svg)](https://phpackages.com/packages/nezarfadle-jsonschemavalidator)
```

###  Alternatives

[chaoswey/taiwan-id-validator

台灣身分證、統一編號驗證

319.9k](/packages/chaoswey-taiwan-id-validator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
