This vection component provides a data and schema validator for PHP. You can use it from validate simple values up to big data arrays via chained validators. This component also provides a schema based validation of data structures. The flexible and open closed aspect of vection components allows to extend and modify the most classes of this component or define custom validator classes. This component can be used for the following scenarios:
- simple value validation
- data set validation by chained validators
- extend with custom validators
- schema based data structure validation
Vection Components supports only installation via composer. So first ensure your composer is installed, configured and ready to use.
$ composer require vection-framework/validator
$validator = new Vection\Component\Validator\Validator\Date("H:i:s");
# Each validator returns null on success or an object of Violation on fail
$violation = $validator->validate("17:12-43");
if( $violation ){
$violation->getMessage(); // or
$violation->getValue(); // or
echo $violation; // Date "17:12-43" is invalid or does not match format "H:i:s".
}
// e.g. the POST request input
$data = [
'name' => 'John Doe',
'age' => 42,
'date' => '2019-02-03'
];
$chain = new Vection\Component\Validator\ValidatorChain();
$chain('name')
->notNull()
->alphaNumeric()
->betweenLength(3, 20)
;
$chain('age')
->notNull()
->numeric()
->range(0, 100)
;
$chain('date')
->nullable()
->date("Y-m-d")
;
$chain->verify($data);
if( $violations = $chain->getViolations() ){
//here we got an array of Violation objects
print json_encode($violations);
# output: {"name": {"value":"2019-02-03", "message":"..."}, "age": {...}, ...}
}
class CustomValidator extends Vection\Component\Validator\Validator { ... }
$customValidator = new CustomValidator(...);
$customValidator->validate(...);
// or
$chain = new ValidatorChain();
$chain('xxx')
->notNull()
->use($customValidator)
;
The schema validator uses a json schema definition to validate the given data structure by json/yaml file, json/yaml strings or data array.
The schema starts always with an object which must have properties and optional reusable template definitions.
{
"@type": "object",
"@properties": {},
"@templates": {}
}
{
"@type": "object",
"@properties": {
"foo": {
"@type": "string",
"@required": true,
"@allowed": "yes|no"
}
}
}
{
"@type": "object",
"@properties": {
"foo": {
"@type": "integer",
"@required": true,
"@range": "0..100"
},
"bar": "float"
}
}
Use @properties for fixed property names to ensure that this keys are set with an value.
{
"@type": "object",
"@properties": {
"foo": {
"@type": "string"
},
"bar": {
"@type": "integer",
"@required": true
}
}
}
Use @property for variable property names to allow multiple properties with different names but same property schema definition.
{
"@type": "object",
"@property": {
"@type": "string",
"@required": true
}
}
Use the array type to define an array property which contains elements by schema defined in @property.
{
"@type": "object",
"@properties": {
"foo": {
"@type": "array",
"@property": {
"@type": "object",
"@properties": {
"foo": {
"@type": "string"
},
"bar": {
"@type": "integer",
"@required": true
}
}
}
}
}
}
The schema definition can use the validators of this component to validate a schema property value.
{
"@type": "object",
"@properties": {
"foo": {
"@type": "string",
"@validator": "alphaNumeric"
},
"bar": {
"@type": "string",
"@validator": {
"@name": "email"
}
},
"xxx": {
"@type": "string",
"@validator": {
"@name": "betweenLength",
"@constraints": {
"min": 3,
"max": 30
}
}
}
}
}
Use can use templates to reuse on different properties. The templates are defined at the root of the schema and is an object contains multiple names templates.
{
"@type": "object",
"@properties": {
"books": {
"@type": "array",
"@property": {
"@template": "book"
}
}
},
"@templates": {
"book": {
"@type": "object",
"@properties": {
"name": {
"@type": "string",
"@required": true
},
"description": {
"@type": "string"
},
"summary": "string",
"pages": "integer"
}
}
}
}