@@ -54,7 +54,8 @@ if ($validation->isFailed()) {
5454
5555### Middleware
5656
57- The ` ValidationExceptionMiddleware ` PSR-15 middleware catches all exceptions and converts it into a nice JSON response.
57+ The ` ValidationExceptionMiddleware ` PSR-15 middleware catches all exceptions and converts
58+ it into a nice JSON response.
5859
5960#### Slim 4 integration
6061
@@ -63,6 +64,7 @@ The `ValidationExceptionMiddleware` PSR-15 middleware catches all exceptions and
6364
6465use Selective\Validation\Encoder\JsonEncoder;
6566use Selective\Validation\Middleware\ValidationExceptionMiddleware;
67+ use Selective\Validation\Transformer\ErrorDetailsTransformer;
6668use Slim\Factory\AppFactory;
6769
6870require_once __DIR__ . '/../vendor/autoload.php';
@@ -71,6 +73,7 @@ $app = AppFactory::create();
7173
7274$app->add(new ValidationExceptionMiddleware(
7375 $app->getResponseFactory(),
76+ new ErrorDetailsTransformer(),
7477 new JsonEncoder()
7578));
7679
@@ -93,6 +96,7 @@ use Psr\Container\ContainerInterface;
9396use Psr\Http\Message\ResponseFactoryInterface;
9497use Selective\Validation\Encoder\JsonEncoder;
9598use Selective\Validation\Middleware\ValidationExceptionMiddleware;
99+ use Selective\Validation\Transformer\ErrorDetailsTransformer;
96100use Slim\App;
97101use Slim\Factory\AppFactory;
98102// ...
@@ -101,7 +105,7 @@ return [
101105 ValidationExceptionMiddleware::class => static function (ContainerInterface $container) {
102106 $factory = $container->get(ResponseFactoryInterface::class);
103107
104- return new ValidationExceptionMiddleware($factory, new JsonEncoder());
108+ return new ValidationExceptionMiddleware($factory, new ErrorDetailsTransformer(), new JsonEncoder());
105109 },
106110
107111 ResponseFactoryInterface::class => static function (ContainerInterface $container) {
@@ -143,6 +147,31 @@ if ($validation->isFailed()) {
143147}
144148```
145149
150+ ## Transformer
151+
152+ If you want to implement a custom response data structure,
153+ you can implement against the ` \Selective\Validation\Transformer\TransformerInterface ` interface.
154+
155+ ** Example**
156+ ```
157+ <?php
158+
159+ namespace App\Transformer;
160+
161+ use Selective\Validation\ValidationResult;
162+
163+ final class MyValidationTransformer implements TransformerInterface
164+ {
165+ public function transform(ValidationResult $validationResult): array
166+ {
167+ // Implement your own data structure for the response
168+ // ...
169+
170+ return [];
171+ }
172+ }
173+ ```
174+
146175## License
147176
148177The MIT License (MIT). Please see [ License File] ( LICENSE ) for more information.
0 commit comments