zfegg / content-validation
PSR-15 中间件的内容验证
6.0.0
2024-08-22 06:07 UTC
Requires
- php: >=7.4
- ext-json: *
- opis/json-schema: ^2.1
- psr/http-server-middleware: ^1.0
Requires (Dev)
- ext-pdo: *
- doctrine/annotations: ^1.13
- doctrine/cache: ^1.11
- doctrine/orm: ^2.9 || ^3.0
- laminas/laminas-diactoros: ^2.0 || ^3.3.1
- laminas/laminas-eventmanager: ^3.0
- laminas/laminas-filter: ^2.11
- laminas/laminas-servicemanager: ^3.7 || ^4.0
- laminas/laminas-validator: ^2.0
- mezzio/mezzio-router: ^3.0
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: 9.5.16
- slevomat/coding-standard: ^7.0.12
- slim/slim: ^4.8.1
README
PSR-15 中间件的内容验证。基于 opis/json-schema
。
安装
通过 composer 安装。
composer require zfegg/content-validation
用法
Opis\JsonSchema\Validator
工厂配置。
// config.php return [ Opis\JsonSchema\Validator::class => [ 'resolvers' => [ 'protocolDir' => [ // foo-schema://host/foo.create.json => schema/dir/foo.create.json ['foo-schema', 'host', 'schema/dir'], ], 'protocol' => [ ], 'prefix' => [ ['prefix1', 'path/to/dir'], ['prefix2', 'path/to/dir'], ], 'file' => [ ['SchemaFoo', 'path/to/file'], ['SchemaBar', 'path/to/file2'], ], 'raw' => [ ['{"type":"object", ...}', 'schema id 1'], ['{"type":"object", ...}', 'schema id 2'], ] ], 'filters' => [ 'foo-filter' => ['filter' => 'FilterFilterName', 'types' => ['integer']], ], 'filtersNS' => [ 'foo-ns' => 'FilterResolverName', ], ] ]
Mezzio
在 'config.php' 中添加 ConfigProvider
。
$aggregator = new ConfigAggregator( [ // ... \Zfegg\ContentValidation\ConfigProvider::class, ] ); return $aggregator->getMergedConfig();
$app->post( '/api/users', [ \Zfegg\ContentValidation\ContentValidationMiddleware::class, function (\Psr\Http\Message\ServerRequestInterface $request) { $data = $request->getParsedBody(); // Get valid data. } ], 'api.users.create') ->setOptions(['schema' => 'path-to-json-schema.json']) //->setOptions([ // // or set json-schema object. // 'schema' => (object) [ // 'type' => 'object', // 'properties' => (object) [ // 'age' => (object) [ // 'type' => 'integer' // ] // ], // 'required' => ['age'] // ] // ]) ;
无效请求将返回状态码 422。
curl "http://host/api/users" -d 'username=foo' HTTP/1.1 422 { "status": 422, "detail": "Failed Validation", "validation_messages": { "age": [ "The required properties (age) are missing" ] } }
Slim
$app->post( '/api/users', function (\Psr\Http\Message\ServerRequestInterface $request) { $data = $request->getParsedBody(); // Get valid data. } ) ->add(\Zfegg\ContentValidation\ContentValidationMiddleware::class) ->setArgument('schema', 'path-to-json-schema.json') ;
验证器
DbalRecordExistsFilter
: 使用doctrine/dbal
检查记录是否存在。json-schema 的$filters
配置{ "$func": "dbal-exists", "$vars": { "db": "db", // Get DBAL object by container. "sql": "select ...", // Set custom SQL "table": "foo", // Table name "field": "key", // Field name "exists": true // Check record exists or not exists. Default: false } }
DoctrineRecordExistsFilter
: 使用doctrine/orm
检查记录是否存在。json-schema 的$filters
配置{ "$func": "orm-exists", "$vars": { "db": "orm.default", // Get ORM object by container. "dql": "select ...", // Set custom DQL "entity": "Foo", // Entity name "field": "key", // Field name "exists": true // Check record exists or not exists. Default: false } }
RecordExistsFilter
: 使用PDO
检查记录是否存在。json-schema 的$filters
配置{ "$func": "db-exists", "$vars": { "db": "db", // Get DBAL object by container. "sql": "select ...", // Set custom SQL "table": "foo", // Table name "field": "key", // Field name "exists": true // Check record exists or not exists. Default: false } }