Download the PHP package mooti/validator without Composer
On this page you can find all versions of the php package mooti/validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mooti/validator
More information about mooti/validator
Files in mooti/validator
Package validator
Short Description PHP validator for json style data structures
License Apache-2.0
Informations about the package validator
Mooti Validator
A standalone validator for json style data structures.
Installation
You can install this through packagist
Run the tests
If you would like to run the tests. Use the following:
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:
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.
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.
All rules have a mandatory type
and an optional constraints
property. Additionaly, all named rules have a mandatory required
property.
- 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
orobject
- 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
- callback [php callback] : A valid php callback function. This should throw a
Each type also has additional properties.
-
string
The string type validates the item as a string. it also has the following properties:
- 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/
)
- constraints [array] : Exta constraints are:
-
number
The number type validates the item as a number. it also has the following properties:
- 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
- constraints [array] : Exta constraints are:
-
object
with inheritance:
The object type validates the item as an associative array/standard object. it also has the following properties:
- 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
The array type validates the item as an numeric array/standard object. it also has the following properties:
- items [array] : an optional associative array of rules for the array's items. This should be a wildcard rule and nothing else.