项目作者: mittwald

项目描述 :
PSR-7 middlewares for JSON schema validation
高级语言: PHP
项目地址: git://github.com/mittwald/php-psr7-validation.git
创建时间: 2016-02-12T15:01:40Z
项目社区:https://github.com/mittwald/php-psr7-validation

开源协议:MIT License

下载


PSR-7 validation middlewares

Build Status

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):

  1. $app->post('/customers', $handler)
  2. ->add(new ValidationMiddleware(
  3. Factory::buildJsonValidatorFromUri('path/to/json-schema.json')
  4. ));

Validating request bodies using a Swagger specification file:

  1. $app->post('/customers', $handler)
  2. ->add(new ValidationMiddleware(
  3. Factory::buildJsonValidatorFromSwaggerDefinition('path/to/swagger.json', 'MyType')
  4. ));

Validating request bodies using a custom validator (using PHP 7’s anonymous classes, for no other reason because I can):

  1. $app->post('/customers', $handler)
  2. ->add(new ValidationMiddleware(
  3. new class implements ValidatorInterface {
  4. public function validateJson($jsonDocument, ValidationResult $result) {
  5. $result->addErrorForProperty('customernumber', 'Foo');
  6. }
  7. }
  8. ));

Combining multiple validators:

  1. $app->post('/customers', $handler)
  2. ->add(new ValidationMiddleware(
  3. new CombinedValidator(
  4. Factory::buildJsonValidatorFromUri('path/to/schema.json'),
  5. new MyVerySpecialCustomValidator()
  6. )
  7. ));