zfegg / content-validation
Content validation for PSR-15 middleware
Installs: 1 389
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
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
Content validation for PSR-15 middleware.
Based on opis/json-schema
.
Installation
Install via composer.
composer require zfegg/content-validation
Usage
Opis\JsonSchema\Validator
factory config.
// 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
Add ConfigProvider
in 'config.php'.
$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'] // ] // ]) ;
Invalid request will response status 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') ;
Validators
DbalRecordExistsFilter
: Usedoctrine/dbal
to check record exists. The json-schema$filters
config:{ "$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
: Usedoctrine/orm
to check record exists. The json-schema$filters
config:{ "$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
: UsePDO
to check record exists. The json-schema$filters
config:{ "$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 } }