mvlabs / ze-content-validation
PSR-7 Validation middleware for Zend Expressive
Installs: 1 874
Dependents: 0
Suggesters: 0
Security: 0
Stars: 8
Watchers: 3
Forks: 7
Open Issues: 7
Requires
- php: ^7.1
- roave/security-advisories: dev-master
- zendframework/zend-expressive: ^3.0
- zendframework/zend-filter: ^2.8
- zendframework/zend-http: ^2.7
- zendframework/zend-i18n: ^2.7
- zendframework/zend-inputfilter: ^2.8
- zendframework/zend-problem-details: ^1.0
- zendframework/zend-servicemanager: ^3.3
- zendframework/zend-stdlib: ^3.1
- zendframework/zend-validator: ^2.10
- zf2timo/zf-mvc-expressive-bridge: ^1.0
Requires (Dev)
- codeception/codeception: *
- composer/composer: >=1.0.0-alpha10
- filp/whoops: ^2.1.12
- fzaninotto/faker: ^1.5
- phpunit/phpunit: 7.0.1
- robmorgan/phinx: ^0.4.6
- zendframework/zend-coding-standard: ~1.0.0
- zendframework/zend-expressive-tooling: ^1.0.0alpha2
- zendframework/zend-expressive-zendrouter: ^3.0
- zendframework/zend-servicemanager: ^3.3
- zfcampus/zf-development-mode: ^3.1
This package is not auto-updated.
Last update: 2024-11-09 20:56:23 UTC
README
Introduction
Zend Expressive Content Validation is a Middleware for automating validation of incoming input.
Allows the following:
- Defining named input filters.
- Mapping named input filters to routes.
- Returning a PSR-7 response representation of application/problem with validation error messages on invalid input using Zend Problem Details
Installation
Run the following composer
command:
$ composer require mvlabs/ze-content-validation
Configuration
The ze-content-validation key is a mapping between routes names as the key, and the value being an array of
mappings that determine which HTTP method to respond to and what input filter to map to for the given request.
The keys for the mapping can either be an HTTP method or *
wildcard for applying to any http method.
Example:
'ze-content-validation' => [ 'user.add' => [ 'POST' => \App\InputFilter\UserInputFilter::class ], ],
In the above example, the \App\InputFilter\UserInputFilter will be selected for POST requests.
input_filter_spec
input_filter_spec
is for configuration-driven creation of input filters. The keys for this array
will be a unique name, but more often based off the service name it is mapped to under the
ze-content-validation
key. The values will be an input filter configuration array, as is
described in the ZF2 manual section on input
filters.
Example:
'input_filter_specs' => [ 'App\\InputFilter\\LoginInputFilter' => [ 0 => [ 'name' => 'displayName', 'required' => true, 'filters' =>[], 'validators' => [ 0 => [ 'name' => 'not_empty', ] ], ], 1 => [ 'name' => 'password', 'required' => true, 'filters' => [], 'validators' => [ 0 => [ 'name' => 'not_empty', ], 1 => [ 'name' => 'string_length', 'options' => [ 'min' => 8, 'max' => 12 ], ], ], ], ], ],
Validating
In the following request, an email value is provided with an invalid format, and the displayName field is omitted entirely:
POST /users HTTP/1.1 Accept: application/json Content-Type: application/json; charset=utf-8 { "email": "foo", "password": "mySecretPassword!" }
The response:
HTTP/1.1 422 Unprocessable Entity Content-Type: application/problem+json { "detail": "Validation Failed", "status": 422, "title": "Unprocessable Entity", "type": "https://httpstatus.es/422", "errors": { "email": { "emailAddressInvalidFormat": "The input is not a valid email address. Use the basic format local-part@hostname" }, "displayName": { "isEmpty": "Value is required and can't be empty" } }, }