Simple validator for validate accos arrays.
composer require alexpts/simple-validator
$body = (array)$this->request->getParsedBody();
$validator = new Validator;
// shot format
$errors = $validator->validate($body, [
'parentId' => ['int', 'betweenInt:0:99999'],
'name' => ['string', 'min:3'],
'slug' => ['string', 'min:3', 'max:120'],
'sort' => ['int']
]);
// full format
$errors = $validator->validate($body, [
'parentId' => ['int', ['betweenInt' => [0, 99999]] ],
'name' => ['string', ['min' => [3]] ],
'slug' => ['string', ['min' => [3]], ['max' => [120]] ],
'sort' => ['int']
]);
// validate if exists
$errors = $validator->validateIfExists($body, [
'name' => ['string', ['min' => [3]] ],
'sort' => ['int'] // field not passed, not validate
]);
// deep attributes
$errors = $validator->validate($body, [
'user' => ['array', 'min:3'],
'user.name' => ['string', 'min:3'],
'user.age' => ['int'],
]);
$errors = $validator->validateIfExists($body, [
'user' => ['array', 'min:3'],
'user.name' => ['string', 'min:3'],
'user.age' => ['int'],
]);
The field under validation must be string
The field under validation must be integer
The field under validation must be float
The field under validation must be array
Required field
Int from range [min, max]
Float from range [min, max]
The field under validation must be boolean
The field under validation must be entirely alphabetic characters
The field under validation may have alpha-numeric characters, as well as dashes and underscores
The field under validation must be entirely alpha-numeric characters
The field under validation must be a valid date according to the strtotime PHP function
Value instance of \DateTime object
Check value in array
Min length fot string, min value for number, min count for array
Max length fot string, max value for number, max count for array
The field under validation must be simple email string
You can add custom validator via method
public function registerRule(string $name, callable $handler): self