mittwald / psr7-validation
用于 JSON schema 验证的 PSR-7 中间件
v1.1.0
2018-04-16 07:53 UTC
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-09-12 23:51:49 UTC
README
概述
本包包含一个用于验证 HTTP 请求的 PSR-7 中间件,特别是使用 JSON schema 验证。
警告:本包仍在开发中;其 API 可能随时更改,恕不另行通知。请自行承担风险。
许可证
本包遵循 MIT 许可协议。
示例
使用 JSON schema 验证请求体(使用 Slim 框架)
$app->post('/customers', $handler) ->add(new ValidationMiddleware( Factory::buildJsonValidatorFromUri('path/to/json-schema.json') ));
使用 Swagger 规范文件 验证请求体
$app->post('/customers', $handler) ->add(new ValidationMiddleware( Factory::buildJsonValidatorFromSwaggerDefinition('path/to/swagger.json', 'MyType') ));
使用自定义验证器验证请求体(使用 PHP 7 的匿名类,没有其他原因,因为我可以)
$app->post('/customers', $handler) ->add(new ValidationMiddleware( new class implements ValidatorInterface { public function validateJson($jsonDocument, ValidationResult $result) { $result->addErrorForProperty('customernumber', 'Foo'); } } ));
组合多个验证器
$app->post('/customers', $handler) ->add(new ValidationMiddleware( new CombinedValidator( Factory::buildJsonValidatorFromUri('path/to/schema.json'), new MyVerySpecialCustomValidator() ) ));