dev-master
9999999-devJson Schema Validator
MIT
The Requires
- php >= 5.1.0
The Development Requires
by Nezar Fadle
1.0
1.0.0.0Json Schema Validator
MIT
The Requires
- php >= 5.1.0
The Development Requires
by Nezar Fadle
Wallogit.com
2017 © Pedro Peláez
Json Schema Validator
composer require nezarfadle/jsonschemavalidator, (*1)
Make sure that you import all the needed classes:, (*2)
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;
$json = <<<EOL
{
"id" : 1
}
EOL;
$validator = new JsonSchemaValidator();
$schema = [
'id' => 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) {}
$json = <<<EOL
{
"data": {
"item1":{
"item2":{
"item3":{
"item4":{
"title": "Fake Content"
}
}
}
}
}
}
EOL;
$validator = new JsonSchemaValidator();
$schema = [
'data.item1.item2.item3.item4' => 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) {}
$json = <<<EOL
{
"title" : "PHP is Cool"
}
EOL;
$validator = new JsonSchemaValidator();
$schema = [
'title' => [ 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) {}
$json = <<<EOL
{
"data": {
"type": "photos",
"attributes": {
"title": "Ember Hamster",
"src": "http://example.com/images/productivity.png"
}
}
}
EOL;
$validator = new JsonSchemaValidator();
$schema = [
'data' => 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) {}
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
class GreaterThanTenValidator implements Json\Validation\Interfaces\IJsonValidator
{
public function validate( $propertyName, $value )
{
if( !is_numeric( $value ) || $value <= 10 ) {
throw new \InvalidArgumentException( $propertyName . ": is less than 10" );
}
}
}
$json = <<<EOL
{
"id" : 11
}
EOL;
$validator = new JsonSchemaValidator();
$schema = [
'id' => new GreaterThanTenValidator()
];
try {
$validator->validate( $json, $schema );
echo "Greater than 10";
}
catch (EmptySchemaException $e) {}
catch (InvalidJsonPayloadException $e) {}
catch (EmptyPayloadException $e) {}
catch (\InvalidArgumentException $e) {}
catch (NotExistsPropertyException $e) {}
Json\Validation\JsonSchemaValidator
Json\Validation\Interfaces\IJsonSchemaValidator
Json\Validation\Validators\ArrayValueValidator Json\Validation\Validators\NumericValueValidator Json\Validation\Validators\ObjectValidator Json\Validation\Validators\StringValueValidator Json\Validation\Validators\RequiredStringValueValidator
Json\Validation\Exceptions\EmptySchemaException Json\Validation\Exceptions\InvalidJsonPayloadException Json\Validation\Exceptions\EmptyPayloadException Json\Validation\Exceptions\NotExistsPropertyException
This exception will be thrown if the given, (*3)
$schema = []; $validator->validate( $json, $schema );
This exception will be thrown if the given json payload is invalid, (*4)
$json = 'invalid json payload'; $validator->validate( $json, $schema );
This exception will be thrown if the given json payload is empty object, (*5)
$json = '{}';
$validator->validate( $json, $schema );
This exception will be thrown if the validation failed, (*6)
This exception will be thrown if the given property is not exists in the json payload, (*7)
$json = <<<EOL
{
"title" : "PHP is Cool"
}
EOL;
$validator = new JsonSchemaValidator();
$schema = [
'id' => new StringValueValidator()
];
$validator->validate( $json, $schema ); // NotExistsPropertyException will be thrown
phpunit, (*8)
Json Schema Validator
MIT
Json Schema Validator
MIT