https://github.com/smoren/validator-php
Responsible validation tools with fluent interface
https://github.com/smoren/validator-php
fluentvalidation php php-library validation validation-library validator
Last synced: 6 months ago
JSON representation
Responsible validation tools with fluent interface
- Host: GitHub
- URL: https://github.com/smoren/validator-php
- Owner: Smoren
- License: mit
- Created: 2023-03-21T23:25:18.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-12-18T21:21:03.000Z (10 months ago)
- Last Synced: 2025-03-28T13:21:13.552Z (6 months ago)
- Topics: fluentvalidation, php, php-library, validation, validation-library, validator
- Language: PHP
- Homepage:
- Size: 160 KB
- Stars: 6
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP Validation Tools

[](https://scrutinizer-ci.com/g/Smoren/validator-php/?branch=master)
[](https://coveralls.io/github/Smoren/validator-php?branch=master)

[](https://opensource.org/licenses/MIT)## How to install to your project
```
composer require smoren/validator
```## Usage
```php
use Smoren\Validator\Factories\Value;
use Smoren\Validator\Exceptions\ValidationError;$rule = Value::container()
->array()
->hasAttribute('id', Value::integer()->positive())
->hasAttribute('probability', Value::float()->between(0, 1))
->hasAttribute('vectors', Value::container()->array()->allValuesAre(
Value::container()
->array()
->lengthIs(Value::integer()->equal(2))
->allValuesAre(Value::integer())
));$validInput = [
'id' => 13,
'probability' => 0.92,
'vectors' => [[1, 2], [3, 4], [5, 6]],
];try {
$rule->validate($validInput);
} catch (ValidationError $e) {
// Input is valid so this block is unreachable.
}$invalidInput = [
'id' => '13',
'probability' => 1.92,
'vectors' => [[1, 2.1], [3, 4], [5, 6]],
];try {
$rule->validate($invalidInput);
} catch (ValidationError $e) {
// Input is invalid so we catch the exception.
print_r($e->getViolatedRestrictions());
/*
[
['attribute_is', [
'attribute' => 'id',
'rule' => 'integer',
'violated_restrictions' => [
['integer', []]
]
]],
['attribute_is', [
'attribute' => 'probability',
'rule' => 'float',
'violated_restrictions' => [
['between', [
'start' => 0,
'end' => 1
]]
]
]],
['attribute_is', [
'attribute' => 'vectors',
'rule' => 'container',
'violated_restrictions' => [
['all_values_are', [
'rule' => 'container',
'violated_restrictions' => [
['all_values_are', [
'rule' => 'integer',
'violated_restrictions' => [
['integer', []]
]
]]
]
]]
]
]]
]
*/
}```
## Unit testing
```
composer install
composer test-init
composer test
```## Standards
PHP Validator Tools conforms to the following standards:
* PSR-1 — [Basic coding standard](https://www.php-fig.org/psr/psr-1/)
* PSR-4 — [Autoloader](https://www.php-fig.org/psr/psr-4/)
* PSR-12 — [Extended coding style guide](https://www.php-fig.org/psr/psr-12/)## License
PHP Validation Tools is licensed under the MIT License.