lefuturiste / validator
A simple validator helper for PS7 requests
Installs: 1 455
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: >=7.4
- ext-ctype: *
- ext-mbstring: *
Requires (Dev)
- ext-json: *
- phpunit/phpunit: ^9.5.1
README
Simple php validator helper for PSR7 request.
How to use ?
From PSR-7
$validator = new Validator($request->getParsedBody());
From php input
$validator = new Validator($_POST);
Validate methods (or rules)
$validator->required('example'); $validator->notEmpty('example');
And more validate methods (or rules)...
Known if the input is valid
$validator->isValid(); // TRUE|FALSE
Get errors
To get the list of all the errors that your input have you can use the getErrors() method which return all the errors as an array:
$validator->getErrors();
You can get errors in a different format, with the rules as key:
$validator->getErrors(\Validator\ValidationError::FORMAT_WITH_KEYS);
Or as a array of array, with each array representing an error with the key 'code' and 'message' (format to use in an JSON:API compliant API):
$validator->getErrors(\Validator\ValidationError::FORMAT_ARRAY);
If you dont' want to specify each time the ValidationError format you can use this static call to set as a setting for the whole project. For example if you want to set the FORMAT_ARRAY as the default format for the whole project you can use this piece of code:
\Validator\ValidationError::setDefaultFormat(\Validator\ValidationError::FORMAT_ARRAY);
I18n
English, french and spanish are supported
ValidationLanguage::setLang('fr'); // or `en` or `es`
Tests
All the tests are in the tests
folder. You can run tests with theses commands (do a composer install before).
- With composer:
composer run test
orcomposer run tests
- On linux/mac:
vendor/bin/phpunit tests
- On windows:
vendor/bin/phpunit.bat tests