2017 © Pedro Peláez
 

library jsonschemavalidator

Json Schema Validator

image

nezarfadle/jsonschemavalidator

Json Schema Validator

  • Thursday, June 1, 2017
  • by nezarfadle
  • Repository
  • 1 Watchers
  • 0 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

PHP Json Schema Validator

Installation

composer require nezarfadle/jsonschemavalidator, (*1)

Usage

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;

1. Simple Validation:

$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) {}

2. Nested Validation:

$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) {}

3. Multiple Validation:

$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) {}

4. JsonApi Spec Use Case:

$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) {}

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 <= 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 Schema Validator components:

Json Validator:

Json\Validation\JsonSchemaValidator

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 )

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

Exceptions

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

Exceptions execution order:

1. EmptySchemaException

This exception will be thrown if the given, (*3)

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

2. InvalidJsonPayloadException

This exception will be thrown if the given json payload is invalid, (*4)

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

3. EmptyPayloadException

This exception will be thrown if the given json payload is empty object, (*5)

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

4. InvalidArgumentException

This exception will be thrown if the validation failed, (*6)

5. NotExistsPropertyException

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 

How to run the test

phpunit, (*8)

The Versions

01/06 2017

dev-master

9999999-dev

Json Schema Validator

  Sources   Download

MIT

The Requires

  • php >= 5.1.0

 

The Development Requires

by Nezar Fadle

21/06 2016

1.0

1.0.0.0

Json Schema Validator

  Sources   Download

MIT

The Requires

  • php >= 5.1.0

 

The Development Requires

by Nezar Fadle