https://github.com/influxow/php-oop-project-lvl1
Validator
https://github.com/influxow/php-oop-project-lvl1
data-validator hexlet oop php php81 validator
Last synced: 3 months ago
JSON representation
Validator
- Host: GitHub
- URL: https://github.com/influxow/php-oop-project-lvl1
- Owner: InfluxOW
- Created: 2021-04-22T11:58:36.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2021-10-20T14:39:12.000Z (over 3 years ago)
- Last Synced: 2025-01-13T16:47:50.565Z (4 months ago)
- Topics: data-validator, hexlet, oop, php, php81, validator
- Language: PHP
- Homepage:
- Size: 135 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### Hexlet tests and linter status
[](https://github.com/InfluxOW/php-oop-project-lvl1/actions)
[](https://codeclimate.com/github/InfluxOW/php-oop-project-lvl1/maintainability)
[](https://codeclimate.com/github/InfluxOW/php-oop-project-lvl1/test_coverage)### Requirements
* PHP >= 8.0
* Composer### Usage
```php
required()->string();$schema->isValid('what does the fox say'); // true
$schema->isValid(''); // false// numbers
$schema = $v->required()->number()->positive();$schema->isValid(-10); // false
$schema->isValid(10); // true// array shape
$schema = $v->array()->sizeof(2)->shape([
'name' => $v->string()->required(),
'age' => $v->number()->positive(),
]);$schema->isValid(['name' => 'kolya', 'age' => 100]); // true
$schema->isValid(['name' => '', 'age' => null]); // false// custom validation rules for existing validators
$fn = fn($value, $start) => str_starts_with($value, $start);
$v->addValidator('string', 'startWith', $fn);$schema = $v->string()->test('startWith', 'H');
$schema->isValid('exlet'); // false
$schema->isValid('Hexlet'); // true// custom validators
$fn = fn($value, $start) => str_starts_with((string) $value, (string) $start);
$v->addValidator('customValidator', 'startWith', $fn);$schema = $v->customValidator()->test('startWith', 5);
$schema->isValid(13); // false
$schema->isValid('test'); // false
$schema->isValid(55); // true
```