Download the PHP package pln0w/rest-api-validator without Composer
On this page you can find all versions of the php package pln0w/rest-api-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pln0w/rest-api-validator
More information about pln0w/rest-api-validator
Files in pln0w/rest-api-validator
Package rest-api-validator
Short Description A package for creating request objects for pre-controller validation
License MIT
Homepage https://github.com/pln0w/rest-api-validator
Informations about the package rest-api-validator
Request Object validator
A simple library to force and make consistent development REST API controllers. It allows to define structure of the payload and it validator constraints. It also provides much easier way to access validated data from within the controller body.
Installation
composer require pln0w/rest-api-validator
Add following section to your services.yml
Each request object must have _requeststack service injected, i.e:
Usage
-
Steps to provide validation to your controllers:
- Create custom request overriding Pawly\RestApiValidator\Request\AbstractCustomRequest
- Define request class properties that you want to map request values with
- Add validation constraints to $metadata (no yaml, no addnotations - pure PHP config)
- (optional) Override getters for properties in needed or add constraint explicit against getter
See validation docs: https://symfony.com/doc/master/validation.html
Example 1.
- Create controller having this request injected
Different custom request validation example
- Validation errors handling
If request has invalid data, then exception is thrown and handled in subscriber, that will provide following message structure:
More
Validate responses interface
If you want to ensure you use unified response for controllers, you can enable checking response interface by addint below definition to your services.yaml
All versions of rest-api-validator with dependencies
symfony/validator Version 5.0.*
symfony/http-foundation Version 5.0.*
symfony/http-kernel Version 5.0.*
symfony/dependency-injection Version 5.0.*