Extendable validation library for testing primitive and complex types (including key-value arrays) against a contract
Require as a composer dependency:
composer require "dsheiko/validate"
- Validators are dead simple to extend
- It's really easy to validate precondition/postcondition contracts
- Validator assertions are directly accessible
- Validation of nested arrays
- Basic Usage
- Provided Validators
- Validator Chain
- Validation by Contract
- Map Validation
- Custom Validators
<?php
use \Dsheiko\Validate;
function login($email, $password)
{
Validate::contract([
"email" => [ $email, "IsEmailAddress" ],
"password" => [ $password, [ "IsString"=> [ "minLength" => 6, "maxLength" => 32, "notEmpty" => true ] ] ],
]);
// do login
}
may throw
Dsheiko\Validate\IsString\Exception
Dsheiko\Validate\IsString\minLength\Exception
Dsheiko\Validate\IsString\maxLength\Exception
Dsheiko\Validate\IsEmail\Exception
with a message like
Parameter "email" validation failed: "jon#snow.i.." is not a valid email address
Parameter "password" validation failed: "123" is too short; must be more than 6 chars
<?php
$params = [
"email" => "jon#snow.io",
"password" => "******",
];
Validate::map($params, [
"email" => ["mandatory", "IsEmailAddress"],
"password" => ["mandatory", "IsString" => ["minLength" => 6, "maxLength" => 128]],
"rememberMe" => ["optional", "IsBool" ],
]);
Exception messages look like:
Property "email" validation failed: "jon#snow.i.." is not a valid email address