Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/daniel-zahariev/shape-validator-php
Basic (array) shape validator
https://github.com/daniel-zahariev/shape-validator-php
Last synced: 17 days ago
JSON representation
Basic (array) shape validator
- Host: GitHub
- URL: https://github.com/daniel-zahariev/shape-validator-php
- Owner: daniel-zahariev
- License: apache-2.0
- Created: 2022-01-28T11:11:22.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-01-28T12:21:04.000Z (almost 3 years ago)
- Last Synced: 2024-10-03T05:09:11.068Z (about 1 month ago)
- Language: PHP
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Simple shape validator
Define _**all**_ possible fields for a shape and set rules for them.
Any field outside of the defined set will trigger an error.### Usage:
```php
try {
$shape = [
'name' => 'required|string',
'phone' => 'required|numeric',
'notes' => 'string',
'data' => 'any',
];
$shape_validator = new ShapeValidator($shape);
$shape_validator->validate([
'name' => 'Hello, world!', // valid
'extra' => null, // this will trigger an error
]);
} catch(ShapeException $exception) {
// getValidationErrors() returns an array like ['field name' => 'error message']
log($exception->getValidationErrors());
}```
### Available rules:
- `required` - the field must be present
- `nullable` - the field can take null values
- `string` - if the field value is set it must be a valid string
- `numeric`- if the field value is set it must have valid numeric value
- `bool` / `boolean` - if the field value is set it must be a valid boolean value (validation is using type comparison `===`)
- `any` - allows any values