Skip to content

hunomina/data-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Json Data Validator

Build Status

Description : Library for json schemas validation

This library is mainly composed of 3 interfaces and 3 classes implementing them.

Interfaces and classes

Allows to encapsulate the data into an object and format it for DataSchema validation

JsonData implements DataType.

JsonData::format() uses json_decode() to format json string into php array.


Allows to validate data unit by checking if the data is null, optional and his length, pattern and type.

JsonRule implements Rule.

JsonRule can validate :

  • Array types: list, array
  • Integer types: int, integer, long
  • Float types: float, double
  • Numeric types: numeric, number
  • Boolean types: boolean, bool
  • Character types: char, character
  • Typed Array types: numeric list, string list, boolean list, integer list, float list, character list
  • Object types: entity, object
  • Strings

Only strings and typed arrays can be length checked.

Only strings can be date format checked.

Only integers, floats, numbers and typed arrays can be min/max checked.

Only strings, characters, string typed arrays and character typed array can be pattern checked.

Only strings, integers, floats, numbers, typed list can be date format checked.

An object is a "child" schema and an array typed value is an object list.


DataSchema is the library main class. It allows to validate DataType based on "child" schema or Rule

DataSchema::validate() method allows this validation. If the DataType does not validate the DataSchema, DataSchema::validate() will return false and DataSchema::getLastError() will return the validation error.

JsonSchema implements DataSchema, validates JsonData and uses JsonRule for validation.

How it works

See tests for examples

A JsonSchema has a type : object or list.

Objects are composed of rules and "child" schemas if needed.

This is a schema definition :

use hunomina\Validator\Json\Schema\JsonSchema;

$schema = (new JsonSchema())->setSchema([
    'success' => ['type' => 'bool'],
    'error' => ['type' => 'string', 'null' => true],
    'user' => ['type' => 'object', 'null' => true, 'optional' => true, 'schema' => [
        'name' => ['type' => 'string'],
        'age' => ['type' => 'int']
    ]]
]);

Schemas are just php arrays passe to JsonSchema::setSchema() method.

This schema is composed of 3 elements :

  • a rule success which :

    • is a boolean
    • can not be null
    • is not optional
  • a rule error which :

    • is a string
    • can be null
    • is not optional
  • a "child" schema user which :

    • is an object and therefor is represented by a schema which contains 2 elements : a name (string) and an age (integer)
    • can be null
    • is optional

When a data unit is being validated using this schema by calling the JsonSchema::validate() method, the schema will check recursively if the data respects the rules and the "child" schemas.

If the data has :

  • a boolean element success
  • a null or string element error
  • an optionally, null or object element user which must have :
    • a string element name
    • an integer element age

This data is valid :

use hunomina\Validator\Json\Data\JsonData;

$data = (new JsonData())->setDataFromArray([
    'success' => true,
    'error' => null,
    'user' => [
        'name' => 'test',
        'age' => 10
    ]
]);

This one is not :

use hunomina\Validator\Json\Data\JsonData;

$data = (new JsonData())->setDataFromArray([
    'success' => true,
    'error' => null,
    'user' => 'test'
]);

As said earlier, rules can be used to validate length or data pattern.

This schema uses the pattern validation on the name element and the length validation on the geolocation element :

use hunomina\Validator\Json\Schema\JsonSchema;

$schema = (new JsonSchema())->setSchema([
    'name' => ['type' => 'string', 'pattern' => '/^[a-z]+$/'],
    'geolocation' => ['type' => 'integer-list', 'length' => 2]
]);

When calling the JsonSchema::validate() method, the schema will recursively check all the rule set and "child" schemas. If one rule or one "child" schema is invalid, JsonSchema::validate() returns false.

The "first level" schema is an object typed schema. It could be changed but is not meant to.

Finally, if a "child" schema is typed as an object, the schema will validate it as described above. If it's typed as a list, the schema will simply check each element of the data as an object type using the given "child" schema.