2017 © Pedro Peláez
 

library validator

PHP validator for json style data structures

image

mooti/validator

PHP validator for json style data structures

  • Thursday, April 19, 2018
  • by lalobo
  • Repository
  • 1 Watchers
  • 2 Stars
  • 1,537 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 25 Versions
  • 7 % Grown

The README.md

Mooti Validator

Build Status Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

A standalone validator for json style data structures., (*2)

Installation

You can install this through packagist, (*3)

$ composer require mooti/validator

Run the tests

If you would like to run the tests. Use the following:, (*4)

$ ./vendor/bin/phpunit -c config/phpunit.xml

Usage

The libray allows you to validate a json style data structure using a set of validation rules. The structure can be an array or a standard object (no other type of object will be validated). A validation rule is an associative array with a key corresponding to the item being validated. An example is:, (*5)

<?php
    require __DIR__.'/vendor/autoload.php';

    $rules = [
        'name' => [
            'name'        => 'Name',
            'required'    => true,
            'type'        => 'string',
            'constraints' => [
                'length' => [1,null]
            ]
        ],
        'age' => [
            'name'        => 'Age',
            'required'    => false,
            'type'        => 'number',
            'constraints' => [
                'integer' => true
            ]
        ],
        'address' => [
            'name'       => 'Address',
            'required'   => false,
            'type'       => 'object',
            'properties' => [
                'line1' => [
                    'required' => true,
                    'type'     => 'string',
                    'constraints' => [
                        'length' => [1,null]
                    ]
                ],
                'line2' => [
                    'required' => false,
                    'type'     => 'string'
                ],
                'postCode' => [
                    'required' => true,
                    'type'     => 'string',
                    'constraints' => [
                        'length' => [3,12]
                    ]
                ]
            ]
        ],
        'nickNames' => [
            'required' => false,
            'type'     => 'array',
            'items'    => [
                '*' => [
                    'type' => 'string',
                    'constraints' => [
                        'length' => [1,null]
                    ]
                ]
            ]
        ]
    ];

    //This will succeed
    $data = [
        'name' => 'Ken Lalobo',
        'age'  => 102,
        'address' => [
            'line1'    => 'test 1',
            'line2'    => 'test 2',
            'postCode' => 'BR12 2NN',
        ],
        'nickNames' => ['Len Kalobo', 'Kenny McKenface']
    ];

    $validator = new Mooti\Validator\Validator;

    if ($validator->isValid($rules, $data) == false) {
        print_r($validator->getErrors());
    }

Rules

The rules follow a simple structure. There are two categories of rules. Named, and wildcard. Each named rule has a key that corresponds to a key in your data structure., (*6)

$rules = [
    'name' => [
        'name'        => 'Name',
        'required'    => true,
        'nullable'    => true,
        'type'        => 'string',
        'constraints' => [
            'callback' => 'MyValidator::checkName'
        ]
    ]
];

For wildcard rules, the key is an asterisk *. Wildcard rules will validate against every item in the data structure (excluding children). If you use a wildcard rule, you cannot add additional rules for that level of validation., (*7)

$rules = [
    '*' => [
        'type' => 'string',
        'constraints' => [
            'callback' => 'MyValidator::checkValue'
        ]
    ]
];

All rules have a mandatory type and an optional constraints property. Additionaly, all named rules have a mandatory required property., (*8)

  • properties
    • name [string]: The human readable name of your item (this is optional)
    • required [true/false]: Wether the item is required or not (For named rules only)
    • nullable [true/false]: Wether the item can be defined but be null (For named rules only)
    • type [string]: The type of item. Currently string, number, array or object
    • message [string]: optional message to display if type validation fails. Does not effect constraints
    • constraints [array] : an optional associative array of constraints. These are:
      • callback [php callback] : A valid php callback function. This should throw a Mooti\Validator\Exception\DataValidationException exception if validation fails

Each type also has additional properties., (*9)

  • string, (*10)

    $rules = [
        'name' => [
            'required'    => false,
            'type'        => 'string',
            'constraints' => [
                'length' => [1,null]
            ]
        ]
    ]
    

    The string type validates the item as a string. it also has the following properties:, (*11)

    • constraints [array] : Exta constraints are:
      • enum [array] : the possible values of the string. so [FOO, BAR] means that the string can only be one of those values and nothing else
      • length [array] : the minimum and maximum length of the string as a numeric array in the format [min, max]. If you don't want to set a value set it to null. So [1,null] will be a string with a minimum of one character but no maximum set.
      • regex [string] : A regular expression to match against. This needs to be a Perl Compatible Regular Expression (i.e /foo/)
  • number, (*12)

    $rules = [
        'name' => [
            'required'    => false,
            'type'        => 'number',
            'constraints' => [
                'integer' => true
            ]
        ]
    ]
    

    The number type validates the item as a number. it also has the following properties:, (*13)

    • constraints [array] : Exta constraints are:
      • integer [true/false] : Wether this has to be an integer. true validates it to be an integer, false validates it to be anything but an integer
  • object, (*14)

    $rules = [
       'address' => [
            'required'   => false,
            'type'       => 'object',
            'properties' => [
                'line1' => [
                    'required' => true,
                    'type'     => 'string',
                    'constraints' => [
                        'length' => [1,null]
                    ]
                ],
                'line2' => [
                    'required' => false,
                    'type'     => 'string'
                ]
            ]
        ]
    ]
    

    with inheritance:, (*15)

    $rules = [
       'animal' => [
           'name'        => 'Animal',
           'required'    => true,
           'type'        => 'object',
           'properties'  => [
               'type' => [
                   'required' => true,
                   'type'     => 'string',
                   'constraints' => [
                       'enum' => ['cat', 'dog']
                   ]
               ],
               'age'  => [
                   'required' => true,
                   'type'     => 'number',
                   'constraints' => [
                       'integer' => true
                   ]
               ]
           ],
           'inheritance' => [
               'discriminator' => 'type',
               'properties' => [
                   'cat' => [
                       'miceCaught' => [
                           'required' => true,
                           'type'     => 'number',
                           'constraints' => [
                               'integer' => true
                           ]
                       ]
                   ],
                   'dog' => [
                       'carsChased' => [
                           'required' => true,
                           'type'     => 'number',
                           'constraints' => [
                               'integer' => true
                           ]
                       ],
                       'collar' => [
                           'required' => true,
                           'type'     => 'object',
                           'properties' => [
                               'colour' => [
                                   'required' => true,
                                   'type' => 'string'
                               ]
                           ]
                       ]
                   ]
               ]
           ]
       ]
    ]
    

    The object type validates the item as an associative array/standard object. it also has the following properties:, (*16)

    • properties [array] : an optional associative array of rules for the object's properties. You can use a wildcard rule here.
    • inheritance [object] : optional. Used inheritance. The child objects' properties are defined here. This hs the following properties
      • discriminator [string] this is required. the property on the parent that will act as a discriminator
      • properties [object] The properties of the children organised by child name
  • array, (*17)

    $rules = [
       'nickNames' => [
            'required' => false,
            'type'     => 'array',
            'items'    => [
                '*' => [
                    'type' => 'string',
                    'constraints' => [
                        'length' => [1,null]
                    ]
                ]
            ]
        ]
    ]
    

    The array type validates the item as an numeric array/standard object. it also has the following properties:, (*18)

    • items [array] : an optional associative array of rules for the array's items. This should be a wildcard rule and nothing else.

The Versions

19/04 2018

dev-master

9999999-dev

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

php validator validation

19/04 2018

2.3.2

2.3.2.0

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

php validator validation

18/04 2018

2.3.1

2.3.1.0

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

php validator validation

18/04 2018

dev-validate_object_inheritence

dev-validate_object_inheritence

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

php validator validation

18/04 2018

2.3.0

2.3.0.0

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

php validator validation

02/01 2018

2.2.0

2.2.0.0

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

php validator validation

02/01 2018

dev-add_boolean

dev-add_boolean

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

php validator validation

13/12 2017

2.1.0

2.1.0.0

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

php validator validation

13/12 2017

dev-add_regex

dev-add_regex

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

php validator validation

01/12 2017

2.0.0

2.0.0.0

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

php validator validation

22/07 2016

1.4.2

1.4.2.0

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

php validator validation

22/07 2016

1.4.1

1.4.1.0

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

php validator validation

21/07 2016

1.4.0

1.4.0.0

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

php validator validation

21/07 2016

dev-add_allow_null_values_option

dev-add_allow_null_values_option

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

php validator validation

28/06 2016

1.3.0

1.3.0.0

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

php validator validation

28/06 2016

1.2.0

1.2.0.0

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

php validator validation

07/06 2016

1.1.1

1.1.1.0

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

php validator validation

06/06 2016

1.1.0

1.1.0.0

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

php validator validation

12/05 2016

0.0.6

0.0.6.0

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

validator validation

12/05 2016

1.0.0

1.0.0.0

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

validator validation

11/05 2016

0.0.5

0.0.5.0

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

validator validation

10/05 2016

0.0.4

0.0.4.0

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

validator validation

21/04 2016

0.0.3

0.0.3.0

PHP validator for json style data structures

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

validator validation

20/04 2016

0.0.2

0.0.2.0

  Sources   Download

Apache-2.0

The Requires

  • php >=5.5.9
  • mooti/testable ^2.0

 

The Development Requires

20/04 2016

0.0.1

0.0.1.0

  Sources   Download

Apache-2.0

The Requires

  • php >=5.5.9
  • mooti/testable ^2.0

 

The Development Requires