mittwald / psr7-validation
PSR-7 middleware for JSON schema validation
Installs: 17 507
Dependents: 0
Suggesters: 1
Security: 0
Stars: 4
Watchers: 6
Forks: 2
Open Issues: 0
Requires
- php: >=5.6
- flow/jsonpath: ^0.3.1
- guzzlehttp/psr7: ^1.2
- justinrainbow/json-schema: ^5.0
- psr/http-message: ^1.0
Requires (Dev)
- phpunit/phpunit: ^5.2
This package is auto-updated.
Last update: 2024-12-13 00:21:54 UTC
README
Synposis
This package contains a PSR-7 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.
Examples
Validating request bodies using a JSON schema (using the Slim framework):
$app->post('/customers', $handler) ->add(new ValidationMiddleware( Factory::buildJsonValidatorFromUri('path/to/json-schema.json') ));
Validating request bodies using a Swagger specification file:
$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):
$app->post('/customers', $handler) ->add(new ValidationMiddleware( new class implements ValidatorInterface { public function validateJson($jsonDocument, ValidationResult $result) { $result->addErrorForProperty('customernumber', 'Foo'); } } ));
Combining multiple validators:
$app->post('/customers', $handler) ->add(new ValidationMiddleware( new CombinedValidator( Factory::buildJsonValidatorFromUri('path/to/schema.json'), new MyVerySpecialCustomValidator() ) ));