nezarfadle/jsonschemavalidator

1.0 2016-06-21 10:03 UTC

This package is auto-updated.

Last update: 2024-09-21 20:13:29 UTC


README

Installation

composer require nezarfadle/jsonschemavalidator

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:

$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

$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 = <<<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