danielz / shape-validator-php
Simple (array) shape validator.
v1.0.0
2022-01-28 12:20 UTC
Requires
- php: ^7.4|^8.0
Requires (Dev)
- pestphp/pest: ^1.21
README
Define all possible fields for a shape and set rules for them. Any field outside of the defined set will trigger an error.
Usage:
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()); }
Available rules:
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