https://github.com/mittwald/php-psr7-validation
PSR-7 middlewares for JSON schema validation
https://github.com/mittwald/php-psr7-validation
json-schema-validator middleware php php-library psr psr-7
Last synced: 2 days ago
JSON representation
PSR-7 middlewares for JSON schema validation
- Host: GitHub
- URL: https://github.com/mittwald/php-psr7-validation
- Owner: mittwald
- License: mit
- Created: 2016-02-12T15:01:40.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2018-04-16T07:55:25.000Z (about 7 years ago)
- Last Synced: 2025-04-13T15:17:51.638Z (13 days ago)
- Topics: json-schema-validator, middleware, php, php-library, psr, psr-7
- Language: PHP
- Size: 16.6 KB
- Stars: 4
- Watchers: 5
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# PSR-7 validation middlewares
[](https://travis-ci.org/mittwald/php-psr7-validation)
## Synposis
This package contains a [PSR-7][psr7] middleware for validating HTTP requests,
especially using JSON schema validation.**Warning**: This package is still under development; its API can change at any time without notice. Use at own risk.
## License
This package is [MIT-licensed](LICENSE.txt).
## Examples
Validating request bodies using a JSON schema (using the [Slim framework][slim]):
```php
$app->post('/customers', $handler)
->add(new ValidationMiddleware(
Factory::buildJsonValidatorFromUri('path/to/json-schema.json')
));
```Validating request bodies using a [Swagger specification file][swag]:
```php
$app->post('/customers', $handler)
->add(new ValidationMiddleware(
Factory::buildJsonValidatorFromSwaggerDefinition('path/to/swagger.json', 'MyType')
));
```Validating request bodies using a custom validator (using PHP 7's anonymous classes, for no other reason because I can):
```php
$app->post('/customers', $handler)
->add(new ValidationMiddleware(
new class implements ValidatorInterface {
public function validateJson($jsonDocument, ValidationResult $result) {
$result->addErrorForProperty('customernumber', 'Foo');
}
}
));
```Combining multiple validators:
```php
$app->post('/customers', $handler)
->add(new ValidationMiddleware(
new CombinedValidator(
Factory::buildJsonValidatorFromUri('path/to/schema.json'),
new MyVerySpecialCustomValidator()
)
));
```[slim]: http://www.slimframework.com/
[swag]: http://swagger.io/specification/
[psr7]: http://www.php-fig.org/psr/psr-7/