Licensed under MIT License.
Igni validation is simple, lightweight and extensible validation library.
composer install igniphp/validation
<?php
use Igni\Validation\Assertion;
$numberValidator = Assertion::number($min = 0);
$numberValidator->validate(1);// true
$numberValidator->validate(-1);// false
$numberValidator->validate(1.0);// true
$numberValidator->validate('a'); // false
Allows to validate complex arrays
<?php
use Igni\Validation\Assertion;
use Igni\Validation\Error;
$userValidator = Assertion::group([
'name' => Assertion::alnum(),
'age' => Assertion::number(1, 200),
'email' => Assertion::email(),
'address' => Assertion::text(),
]);
$userValidator->validate([
'name' => 'John',
'age' => 233,
'email' => 'johnmail',
]);// false
$errors = $userValidator->getErrors();
$errors[0] instanceof Error\OutOfRangeError;// true
$errors[0]->getContext()->getName();//age
$errors[1] instanceof Error\EmptyValueError;// true
$errors[1]->getContext()->getName();//address
// Exception can also be factored out of failure instance
throw $errors[0]->toException();
- alnum
- alpha
- boolean
- chain
- contains
- date
- each
- falsy
- group
- in
- integer
- ip
- ipv4
- ipv6
- number
- regex
- text
- truthy
- uri
- url
- uuid
Creates validator that checks if passed value contains only digits and letters.
$min
defines minimum length$max
defines maximum length
<?php
use Igni\Validation\Assertion;
$validator = Assertion::alnum($minLength = 2);
var_dump($validator->validate('a1')); // true
Creates validator that checks if passed value contains only letters.
$min
defines minimum length$max
defines maximum length
<?php
use Igni\Validation\Assertion;
$validator = Assertion::alpha($minLength = 2);
var_dump($validator->validate('aaa')); // true
Creates validator that checks if passed value is valid boolean expression.
<?php
use Igni\Validation\Assertion;
$validator = Assertion::boolean();
var_dump($validator->validate(false)); // true
Creates validator that uses other validators to perform multiple validations on passed value.
<?php
use Igni\Validation\Assertion;
$validator = Assertion::chain(Assertion::text(), Assertion::date());
var_dump($validator->validate('2018-09-10')); // true
Creates validator that checks if passed string is contained in the validated string.
<?php
use Igni\Validation\Assertion;
$validator = Assertion::contains('example');
var_dump($validator->validate('Test example')); // true
Creates validator that checks if passed value is valid date.
$format
restricts format of passed value$min
defines minimum date range$max
defines maximum date range
<?php
use Igni\Validation\Assertion;
$validator = Assertion::date('Y-m-d');
var_dump($validator->validate('2018-09-10')); // true
Creates validator that checks if passed value is valid email address.
<?php
use Igni\Validation\Assertion;
$validator = Assertion::email();
var_dump($validator->validate('test@test.com')); // true
Creates validator that checks if each item in passed set can be successfully validated against $validator
.
$validator
validator used for each item of the passed set.
<?php
use Igni\Validation\Assertion;
$validator = Assertion::each(Assertion::date('Y-m-d'));
var_dump($validator->validate(['2019-01-01', '2018-10-11'])); // true
Creates validator that checks if passed value is valid falsy expression;
off
no
false
- 0
<?php
use Igni\Validation\Assertion;
$validator = Assertion::falsy();
var_dump($validator->validate('no')); // true
Creates validator with key/value hash that validates other hashes.
<?php
use Igni\Validation\Assertion;
$validator = Assertion::group([
'email' => Assertion::email(),
'password' => Assertion::text(),
'date_of_birth' => Assertion::date('Y-m-d'),
]);
var_dump($validator->validate([
'email' => 'test@domain.com',
'password' => 'secret',
'date_of_birth' => '2019-01-01',
])); // true
Creates validator that checks if passed string matches the pattern.
<?php
use Igni\Validation\Assertion;
$validator = Assertion::regex('^-[a-z]+$');
var_dump($validator->validate('-aa')); // true
Creates validator that checks if passed value is valid truthy expression;
on
true
- 1
yes
<?php
use Igni\Validation\Assertion;
$validator = Assertion::truthy();
var_dump($validator->validate('yes')); // true
Creates validator that checks if passed value is string.
$minLength
defines minimum length$maxLength
defines maximum length
<?php
use Igni\Validation\Assertion;
$validator = Assertion::text($minLength = 2);
var_dump($validator->validate('aaa')); // true
Creates validator that checks if passed value exists in defined list of values.
<?php
use Igni\Validation\Assertion;
$validator = Assertion::in('no', 'yes', 'test');
var_dump($validator->validate('no')); // true
Creates validator that checks if passed value is valid integer expression.
$min
defines minimum value$max
defines maximum value
<?php
use Igni\Validation\Assertion;
$validator = Assertion::integer(10, 100);
var_dump($validator->validate(11)); // true
Creates validator that checks if passed value is valid ip address.
<?php
use Igni\Validation\Assertion;
$validator = Assertion::ip();
var_dump($validator->validate('123.123.123.123')); // true
Creates validator that checks if passed value is valid ip v4 address.
<?php
use Igni\Validation\Assertion;
$validator = Assertion::ipv4();
var_dump($validator->validate('123.123.123.123')); // true
Creates validator that checks if passed value is valid ip v6 address.
<?php
use Igni\Validation\Assertion;
$validator = Assertion::ipv6();
var_dump($validator->validate('2001:0db8:85a3:0000:0000:8a2e:0370:7334')); // true
Creates validator that checks if passed value is valid number expression.
$min
defines minimum value$max
defines maximum value
<?php
use Igni\Validation\Assertion;
$validator = Assertion::number(10, 100);
var_dump($validator->validate('11.2')); // true
Creates validator that checks if passed value is valid uuid.
<?php
use Igni\Validation\Assertion;
$validator = Assertion::uuid();
var_dump($validator->validate('1ff60619-81cc-4d8e-88ac-a3ae36a97dce')); // true
Creates validator that checks if passed value is valid uri string.
<?php
use Igni\Validation\Assertion;
$validator = Assertion::uri();
var_dump($validator->validate('/some/uri')); // true
Creates validator that checks if passed value is valid url string.
<?php
use Igni\Validation\Assertion;
$validator = Assertion::uri();
var_dump($validator->validate('http://domain.com/some/uri')); // true
Creates validator that accepts every non empty string.
Creates validator that validates passed value by group of defined validators.
<?php
use Igni\Validation\Assertion;
$validator = Assertion::group([
'name' => Assertion::text(),
'age' => Assertion::integer(1, 200),
'email' => Assertion::email(),
]);
var_dump($validator->validate(['name' => 'John Doe', 'age' => 29, 'email' => 'john@gmail.com'])); // true
To create custom validator we have to simply extend \Igni\Validation\Assertion
class, please consider following example:
<?php declare(strict_types=1);
use Igni\Validation\Assertion;
class ValidateIn extends Assertion
{
public function __construct(...$values)
{
$this->attributes['valid_values'] = $values;
}
protected function assert($input): bool
{
return in_array($input, $this->attributes['valid_values'], $strict = true);
}
}
That's all folks!