https://github.com/dentelis/php-json-structure-validator
Lightweight library to validate/assert json documents structure
https://github.com/dentelis/php-json-structure-validator
assert assertions json php8 structure testing validate validation
Last synced: 4 months ago
JSON representation
Lightweight library to validate/assert json documents structure
- Host: GitHub
- URL: https://github.com/dentelis/php-json-structure-validator
- Owner: dentelis
- License: mit
- Created: 2023-09-20T07:32:53.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-05-22T17:19:05.000Z (over 1 year ago)
- Last Synced: 2025-04-30T07:49:49.547Z (8 months ago)
- Topics: assert, assertions, json, php8, structure, testing, validate, validation
- Language: PHP
- Homepage:
- Size: 174 KB
- Stars: 18
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# dentelis/php-json-structure-validator
Validator is a lightweight PHP library for validating the structure of data retrieved from an external json API.
The library was originally created for use in acceptance api tests, but can be used anywhere else as well.
## Specific
The library was made to test json compatible data structures. Array key validation is not supported.
## Installation
Use the package manager [composer](https://getcomposer.org/) to install.
```bash
composer require dentelis/php-json-structure-validator
```
## Usage
You can use library with(or without) any testing framework you want.
See [dentelis/phpunit-json-assert](https://github.com/dentelis/phpunit-json-assert) for PhpUnit support.
```php
//object example
$user = (new ObjectType())
->addProperty('name', (new StringType())->assertNotEmpty())
->addProperty('email', (new StringType())->assertEmail());
$data = json_decode('{"name":"user", "email":"user@example.com"}');
try {
$user->validate($data);
} catch (ValidationException $e) {
//do smth
}
//array of objects
$users = (new ArrayType())
->assertNotEmpty()
->assertType($user);
$data = json_decode('[{"name":"user", "email":"user@example.com"},{"name":"user", "email":"user@example.com"}]');
try {
$users->validate($data);
} catch (ValidationException $e) {
//do smth
}
```
See [examples](https://github.com/dentelis/validator/tree/master/examples) directory for full example.
## Todo
- [ ] more examples
- [ ] proper path in exceptions
- [ ] do not fail on first exception
- [ ] comments translate to english
- [ ] create TypeInterface from Classname (what to do with arrays)
## Contributing
Pull requests are welcome. For major changes, please open an issue first
to discuss what you would like to change.
Please make sure to update tests as appropriate.
## License
[MIT](https://choosealicense.com/licenses/mit/)