atasciuc / zend-expressive-validation
PSR-7 Validation middleware for zend-expressive
Requires
- php: ^5.5 || ^7.0
- doctrine/doctrine-module: 0.*
- doctrine/orm: *
- fzaninotto/faker: ~1.5.0
- mtdowling/cron-expression: ^1.0
- nesbot/carbon: ^1.21
- roave/security-advisories: dev-master
- symfony/serializer: ^2.7
- zendframework/zend-expressive: ^0.5
- zendframework/zend-expressive-fastroute: ^0.1
- zendframework/zend-filter: ^2.5
- zendframework/zend-http: ^2.5
- zendframework/zend-i18n: ^2.5
- zendframework/zend-servicemanager: ^2.5
- zendframework/zend-stdlib: ~2.7
- zendframework/zend-validator: ^2.5
Requires (Dev)
- codeception/codeception: *
- composer/composer: >=1.0.0-alpha10
- filp/whoops: ^1.1
- fzaninotto/faker: ^1.5
- phpunit/phpunit: ^4.8
- robmorgan/phinx: ^0.4.6
- squizlabs/php_codesniffer: ^2.3
This package is not auto-updated.
Last update: 2024-12-17 20:28:18 UTC
README
Validation library for the zend-expressive (http://zend-expressive.readthedocs.org/en/latest)
Installation
Using composer!
composer require atasciuc/zend-expressive-validation
Usage
1. ----
Create a validator factory
/** * Instantiates the validator * Class YourValidatorFactoryClass */ class YourValidatorFactoryClass { public function __invoke(ContainerInterface $container) { /** @var EntityManagerInterface $entityManager */ $entityManager = $container->get('orm.default'); // = null return new Validator( $container->get(OptionsExtractor::class), $container->get(RouterInterface::class), $entityManager ); } }
Note:
The entityManager is optional and required only if you want to use database validation classes such as EntityExist
2. ----
Add the validator classes to the dependecies.php
of your project
<?php use ExpressiveValidator\Middleware\ErrorMiddleware; use ExpressiveValidator\Validator\OptionExtractorFactory; use ExpressiveValidator\Validator\OptionsExtractor; use ExpressiveValidator\Validator\ValidationMiddleware; use ExpressiveValidator\Validator\ValidationMiddlewareFactory; use ExpressiveValidator\Validator\Validator; return [ 'dependencies' => [ 'abstract_factories' => [ ], 'invokables' => [ ErrorMiddleware::class => ErrorMiddleware::class, ], 'factories' => [ Zend\Expressive\Application::class => Zend\Expressive\Container\ApplicationFactory::class, OptionsExtractor::class => OptionExtractorFactory::class, Validator::class => YourValidatorFactoryClass::class, ValidationMiddleware::class => ValidationMiddlewareFactory::class, ], 'shared' => [ ] ] ];
3. ----
Edit your middleware-pipeline.global
and register the validation middlewares
<?php use ExpressiveValidator\Middleware\ErrorMiddleware; use ExpressiveValidator\Validator\ValidationMiddleware; return [ // This can be used to seed pre- and/or post-routing middleware 'middleware_pipeline' => [ // An array of middleware to register prior to registration of the // routing middleware 'pre_routing' => [ [ 'middleware' => ValidationMiddleware::class, ], //[ // Required: // 'middleware' => 'Name of middleware service, or a callable', // Optional: // 'path' => '/path/to/match', // 'error' => true, //], ], // An array of middleware to register after registration of the // routing middleware 'post_routing' => [ [ 'middleware' => ErrorMiddleware::class, 'error' => true ], //[ // Required: // 'middleware' => 'Name of middleware service, or a callable', // Optional: // 'path' => '/path/to/match', // 'error' => true, //], ], ], ];
Note
Or you can add your own ErrorMiddleware
and register it in you dependencies, example of the one provided:
class ErrorMiddleware { /** * @param mixed $error * @param Request $request * @param Response $response * @param callable|null $out * @return * @throws Exception */ public function __invoke($error, Request $request, Response $response, callable $out = null) { if (!($error instanceof Exception)) { $error = new MethodNotAllowedException(); } switch (true) { case $error instanceof MethodNotAllowedException || $error instanceof EntityNotFoundException: return $out($request, new JsonExceptionResponse($error->getCode(), $error->getMessage())); case ($error instanceof ValidationFailedException): $messages = $error->getValidationResult()->getMessages(); return $out($request, new JsonExceptionResponse( $error->getCode(), count($messages) > 0 ? $messages[0] : 'Something is not right' )); default: throw $error; } } }
4. ----
To your route add the validation to the route:
'routes' => [ [ 'name' => 'user', 'path' => '/user/{id}', 'allowed_methods' => ['GET'], 'middleware' => UserAction::class, 'options' => [ 'validation' => [ 'GET' => GetUserValidationRules::class ] ] ] ],
The validation might be aplyed to the http method or to all the methods:
'options' => [ 'validation' => [ '*' => GetUserValidationRules::class ] ]
The validation rules class my comply to the - ValidationRulesInterface
Example of the validation rules:
class GetUserValidationRules implements ValidationRulesInterface { use ValidationRulesConstructorTrait; /** * Return a class mapping * of the validations * @return \array[] mixed */ public function getValidationRules() { return [ 'id' => [ NotEmpty::class => [ 'locale' => 'en' ], Regex::class => [ 'pattern' => $this->idRegex ], EntityExist::class => [ 'entity' => User::class, 'field' => 'id' ] ] ]; } /** * Return the error messages * @return array [] mixed */ public function getMessages() { return [ 'id' => [ NotEmpty::class => 'Please provide the user', Regex::class => 'user id must have the following pattern xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', EntityExist::class => 'This user does not exist' ] ]; } }
5. ----
Now in your action you can extract the validated data:
public function __invoke(Request $request, Response $response, callable $next = null) { $data = $request->getValidationResult()->getData(); return $next($request, new JsonResponse($data)); }
That it is it
ToDo
[] - add docs to the validation classes