Define all possible fields for a shape and set rules for them. Any field outside of the defined set will trigger an error.
try {
$shape = [
'name' => 'required|string',
'phone' => 'required|numeric',
'notes' => 'string',
'data' => 'any',
];
$shape_validator = new ShapeValidator($shape);
$shape_validator->validate([
'name' => 'Hello, world!', // valid
'extra' => null, // this will trigger an error
]);
} catch(ShapeException $exception) {
// getValidationErrors() returns an array like ['field name' => 'error message']
log($exception->getValidationErrors());
}
required
- the field must be presentnullable
- the field can take null valuesstring
- if the field value is set it must be a valid stringnumeric
- if the field value is set it must have valid numeric valuebool
/boolean
- if the field value is set it must be a valid boolean value (validation is using type comparison===
)any
- allows any values