morebec / validator
The Validator component provides tools to validate data.
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- phpstan/phpstan: ^0.12.5
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2025-04-06 19:07:55 UTC
README
The Validator component makes data validation easy. Its primary use is intended to ensure that the data of forms, Rest API and configuration files is in an appropriate and valid state.
A lot of validation libraries rely on an exception mechanism. This forces developers to check multiple related data fields one by one. This component instead uses validation errors that can be used for multiple pieces of data together. This is useful for example to return all errors for a given form at once.
Installation
composer require morebec/validator
Usage
use Morebec\Validator\Rule as Assert; use Morebec\Validator\Validator; // Validate a single rule for a given field Validator::validate($form['email_address'], new Assert\One( new Assert\IsString('The email address field was expected to be a string') )); // Validate Multiple Rules for a given field Validator::validate($form['email_address'], new Assert\All([ new Assert\IsString('The email address field was expected to be a string'), new Assert\NotBlank('The email address field was expected not to be blank'), new Assert\IsEmail('The email address field was expected to be a valid email address'), ])); // Ensure At least one rule is valid Validator::validate($form['email_address'], new Assert\AtLeastOne([ new Assert\IsString('The email address field was expected to be a string'), new Assert\NotBlank('The email address field was expected not to be blank'), new Assert\IsEmail('The email address field was expected to be a valid email address'), ]));
Running Tests
php vendor/bin/phpunit --bootstrap vendor/autoload.php tests
Contributing
Thank you for your interest :)
To contribute, please read the CONTRIBUTING.md
guidelines.
Getting help
To get help, open a new issue on this repository.
For Morebec team members, please use the appropriate internal channels.