Lightweight dependency free validator component of the earc framework for an SOLID validation approach.
- Install
- bootstrap
- configure
- basic usage
- advanced usage
- releases
$ composer require earc/validator
The earc/validator does not require any bootstrapping.
The earc/validator does not require any configuration.
Validation will be done in two steps.
- Define the validation logic to validate against.
- Check if the validation logic holds true against a target.
After setting the validation logic, checking can be repeated against different targets unlimited times.
use eArc\Validator\ValidatorFactory;
$validator = (new ValidatorFactory())->build();
$validator->email();
$validator->check('validator@example.com'); // returns true
$validator->check('no-email-address'); // returns false
By passing a true
value as second parameter instead of returning true or false,
an AssertException
is thrown.
use eArc\Validator\Exceptions\AssertException;
use eArc\Validator\ValidatorFactory;
$validator = (new ValidatorFactory())->build();
$validator->email();
$throwOnNotValid = true;
try {
$validator->check('no-email-address', $throwOnNotValid);
} catch (AssertException) {
// email not valid
}
To receive a message explaining why the value is not valid use validate()
instead
of check()
. It returns a result object instead of a bool.
use eArc\Validator\Exceptions\AssertException;
use eArc\Validator\ValidatorFactory;
$validator = (new ValidatorFactory())->build();
$validator->email();
$result = $validator->validate('no-email-address');
if (!$result->isValid()) {
echo $result->getFirstErrorMessage(); // echos 'has to be a valid email address'
}
$throwOnError = true;
try {
$validator->validate('no-email-address', $throwOnError);
} catch (AssertException $e) {
echo $e->getResult()->getFirstErrorMessage(); // echos 'has to be a valid email address'
}
You can use your own messages.
The simplest chain is build by the AND
conjunction. You can do this in three
different ways:
- Subsequent calls to atomic validator methods.
- Method chaining of atomic validator methods.
- Use of the
AND()
or theAllOf()
method.
The validation holds true, if all arguments validate to true.
use eArc\Validator\ValidatorFactory;
// 1. Subsequent calls to atomic validator methods:
$validator = (new ValidatorFactory())->build();
$validator->email();
$validator->regex('/@example\.com$/');
$validator->check('validator@example.com'); // returns true
$validator->check('validator@example.de'); // returns false
$validator->check('valid\\tor@example.com'); // returns false
// 2. Method chaining of atomic validator methods:
$validator = (new ValidatorFactory())
->build()
->email()
->regex('/@example\.com$/');
// 3.1 Use of the `AND()` method.
$validator = (new ValidatorFactory())->build();
$validator = $validator->AND(
$validator->email(),
$validator->regex('/@example\.com$/')
);
// 3.1 Use of the `AllOf()` method.
$validator = (new ValidatorFactory())->build();
$validator = $validator->AllOf(
$validator->email(),
$validator->regex('/@example\.com$/')
);
Choosing between or mixing them is just a case of syntactic preference.
The logical OR
is represented by two methods OR()
and OneOf()
. Both take
Validators
as arguments.
The validation holds true, if one argument validates to true.
use eArc\Validator\ValidatorFactory;
$validator = (new ValidatorFactory())->build();
$validator = $validator->email()->OR(
$validator->regex('/@example\.com$/'),
$validator->regex('/@coding-crimes\.com$/'),
);
$validator->check('validator@example.com'); // returns true
$validator->check('validator@coding-crimes.com'); // returns true
$validator->check('validtor@example.de'); // returns false
// The above validation logic is equivalent to:
$validator = (new ValidatorFactory())->build();
$validator = $validator->email()->OneOf(
$validator->regex('/@example\.com$/'),
$validator->regex('/@coding-crimes\.com$/')
);
The logical XOR
is represented by two methods XOR()
and NoneOf()
. Both take
Validators
as arguments.
The validation holds true, if no argument validates to true.
use eArc\Validator\ValidatorFactory;
$validator = (new ValidatorFactory())->build();
$validator = $validator->email()->XOR(
$validator->regex('/@blacklisted\.com$/'),
$validator->regex('/@coding-crimes\.com$/'),
);
$validator->check('validator@example.com'); // returns true
$validator->check('validator@blacklisted.com'); // returns false
$validator->check('validator@coding-crimes.com'); // returns false
// The above validation logic is equivalent to:
$validator = (new ValidatorFactory())->build();
$validator = $validator->email()->NoneOf(
$validator->regex('/@blacklisted\.com$/'),
$validator->regex('/@coding-crimes\.com$/')
);
Logical inversion can be realized via the NOT()
method.
NOT()
holds true, if all arguments and the validation chain on the right-hand
side validates to false.
To realize conditional validation there exists the WHEN()
method.
If the first argument is true, the second argument will be validated. Otherwise, the third argument will be used for validation, which defaults to true.
There are two methods that influence the validation error messages. withKey()
sets the message key for the validation error message. with()
sets the validation
error message itself.
Messages can be individualized on a global scope, too.
- first official release
- PHP ^8.0
- is coming soon (code base <= release candidate status)
- TODO:
- Documentation
- Tests