|
1 | 1 | # JSON-RPC params symfony validator
|
2 |
| - [](https://github.com/yoanm/php-jsonrpc-params-symfony-validator-sdk) [](https://github.com/yoanm/php-jsonrpc-params-symfony-validator-sdk) [](https://php.net/) |
| 2 | +[](https://github.com/yoanm/php-jsonrpc-params-symfony-validator-sdk) [](https://github.com/yoanm/php-jsonrpc-params-symfony-validator-sdk) [](https://libraries.io/packagist/yoanm%2Fjsonrpc-params-symfony-validator-sdk) |
3 | 3 |
|
4 |
| -[](https://scrutinizer-ci.com/g/yoanm/php-jsonrpc-params-symfony-validator-sdk/?branch=master) [](https://scrutinizer-ci.com/g/yoanm/php-jsonrpc-params-symfony-validator-sdk/build-status/master) [](https://scrutinizer-ci.com/g/yoanm/php-jsonrpc-params-symfony-validator-sdk/?branch=master) |
| 4 | +[](https://scrutinizer-ci.com/g/yoanm/php-jsonrpc-params-symfony-validator-sdk/build-status/master) [](https://scrutinizer-ci.com/g/yoanm/php-jsonrpc-params-symfony-validator-sdk/?branch=master) [](https://scrutinizer-ci.com/g/yoanm/php-jsonrpc-params-symfony-validator-sdk/?branch=master) |
5 | 5 |
|
6 |
| -[](https://travis-ci.org/yoanm/php-jsonrpc-params-symfony-validator-sdk) [](https://travis-ci.org/yoanm/php-jsonrpc-params-symfony-validator-sdk) |
| 6 | +[](https://travis-ci.com/yoanm/php-jsonrpc-params-symfony-validator-sdk) [](https://travis-ci.com/yoanm/php-jsonrpc-params-symfony-validator-sdk) [](https://php.net/) |
7 | 7 |
|
8 | 8 | [](https://packagist.org/packages/yoanm/jsonrpc-params-symfony-validator-sdk) [](https://packagist.org/packages/yoanm/jsonrpc-params-symfony-validator-sdk)
|
9 | 9 |
|
10 | 10 | Simple JSON-RPC params validator that use Symfony validator component
|
11 | 11 |
|
| 12 | +See [yoanm/symfony-jsonrpc-params-validator](https://github.com/yoanm/symfony-jsonrpc-params-validator) for automatic dependency injection. |
| 13 | + |
| 14 | +See [yoanm/jsonrpc-params-symfony-constraint-doc-sdk](https://github.com/yoanm/php-jsonrpc-params-symfony-constraint-doc-sdk) for documentation generation. |
| 15 | + |
12 | 16 | ## How to use
|
13 | 17 |
|
| 18 | +In order to be validated, a JSON-RPC method must : |
| 19 | + - Implements `JsonRpcMethodInterface` from [`yoanm/jsonrpc-server-sdk`](https://github.com/yoanm/php-jsonrpc-server-sdk) |
| 20 | + - Implements [`MethodWithValidatedParamsInterface`](./src/Infra/JsonRpcParamsValidator.php) |
| 21 | + |
| 22 | +### With [`yoanm/jsonrpc-server-sdk`](https://github.com/yoanm/php-jsonrpc-server-sdk) |
| 23 | +Create the validator and inject it into request handler : |
| 24 | +```php |
| 25 | +$requestHandler->setMethodParamsValidator( |
| 26 | + new JsonRpcParamsValidator( |
| 27 | + (new ValidatorBuilder())->getValidator() |
| 28 | + ) |
| 29 | +); |
| 30 | +``` |
| 31 | + |
| 32 | +Then you can send JSON-RPC request string to the server and any method wich implements `MethodWithValidatedParamsInterface` will be validated. |
| 33 | + |
| 34 | +### Standalone |
| 35 | +```php |
| 36 | +use Symfony\Component\Validator\ValidatorBuilder; |
| 37 | +use Yoanm\JsonRpcParamsSymfonyValidator\Infra\JsonRpcParamsValidator; |
| 38 | + |
| 39 | +// Create the validator |
| 40 | +$paramsValidator = new JsonRpcParamsValidator( |
| 41 | + (new ValidatorBuilder())->getValidator() |
| 42 | +); |
| 43 | + |
| 44 | +// Validate a given JSON-RPC method instance against a JSON-RPC request |
| 45 | +$violationList = $paramsValidator->validate($jsonRpcRequest, $jsonRpcMethod); |
| 46 | +``` |
| 47 | + |
| 48 | +### Params validation example |
| 49 | +```php |
| 50 | +use Symfony\Component\Validator\Constraint; |
| 51 | +use Symfony\Component\Validator\Constraints\Collection; |
| 52 | +use Symfony\Component\Validator\Constraints\NotBlank; |
| 53 | +use Symfony\Component\Validator\Constraints\NotNull; |
| 54 | +use Yoanm\JsonRpcParamsSymfonyValidator\Domain\MethodWithValidatedParamsInterface; |
| 55 | +use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface; |
| 56 | + |
| 57 | +class MethodExample implements JsonRpcMethodInterface, MethodWithValidatedParamsInterface |
| 58 | +{ |
| 59 | + /** |
| 60 | + * {@inheritdoc} |
| 61 | + */ |
| 62 | + public function apply(array $paramList = null) |
| 63 | + { |
| 64 | + return 'result'; |
| 65 | + } |
| 66 | + |
| 67 | + public function getParamsConstraint(): Constraint |
| 68 | + { |
| 69 | + return new Collection( |
| 70 | + [ |
| 71 | + 'fields' => [ |
| 72 | + 'fieldA' => new NotNull(), |
| 73 | + 'fieldB' => new NotBlank(), |
| 74 | + ], |
| 75 | + ] |
| 76 | + ); |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +### Violations format |
| 82 | +Each violations will have the following format : |
| 83 | +```php |
| 84 | +[ |
| 85 | + 'path' => 'property_path', |
| 86 | + 'message' => 'violation message', |
| 87 | + 'code' => 'violation_code' |
| 88 | +] |
| 89 | +``` |
14 | 90 |
|
15 | 91 | ## Contributing
|
16 | 92 | See [contributing note](./CONTRIBUTING.md)
|
0 commit comments