@@ -14,8 +14,20 @@ Simple JSON-RPC params validator that use Symfony validator component
14
14
In order to be validated, a JSON-RPC method must :
15
15
- Implements ` JsonRpcMethodInterface ` from [ ` yoanm/jsonrpc-server-sdk ` ] ( https://github.com/yoanm/php-jsonrpc-server-sdk )
16
16
- Implements [ ` MethodWithValidatedParamsInterface ` ] ( ./src/Infra/JsonRpcParamsValidator.php )
17
-
18
- Then use it as following :
17
+
18
+ ### With [ ` yoanm/jsonrpc-server-sdk ` ] ( https://github.com/yoanm/php-jsonrpc-server-sdk )
19
+ Create the validator and inject it into request handler :
20
+ ``` php
21
+ $requestHandler->setMethodParamsValidator(
22
+ new JsonRpcParamsValidator(
23
+ (new ValidatorBuilder())->getValidator()
24
+ )
25
+ );
26
+ ```
27
+
28
+ Then you can send JSON-RPC request string to the server and any method wich implements ` MethodWithValidatedParamsInterface ` will be validated.
29
+
30
+ ### Standalone
19
31
``` php
20
32
use Symfony\Component\Validator\ValidatorBuilder;
21
33
use Yoanm\JsonRpcParamsSymfonyValidator\Infra\JsonRpcParamsValidator;
@@ -29,6 +41,40 @@ $paramsValidator = new JsonRpcParamsValidator(
29
41
$violationList = $paramsValidator->validate($jsonRpcRequest, $jsonRpcMethod);
30
42
```
31
43
44
+ ### Params validation example
45
+ ``` php
46
+ use Symfony\Component\Validator\Constraint;
47
+ use Symfony\Component\Validator\Constraints\Collection;
48
+ use Symfony\Component\Validator\Constraints\NotBlank;
49
+ use Symfony\Component\Validator\Constraints\NotNull;
50
+ use Yoanm\JsonRpcParamsSymfonyValidator\Domain\MethodWithValidatedParamsInterface;
51
+ use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface;
52
+
53
+ class MethodExample implements JsonRpcMethodInterface, MethodWithValidatedParamsInterface
54
+ {
55
+ /**
56
+ * {@inheritdoc}
57
+ */
58
+ public function apply(array $paramList = null)
59
+ {
60
+ return 'result';
61
+ }
62
+
63
+ public function getParamsConstraint(): Constraint
64
+ {
65
+ return new Collection(
66
+ [
67
+ 'fields' => [
68
+ 'fieldA' => new NotNull(),
69
+ 'fieldB' => new NotBlank(),
70
+ ],
71
+ ]
72
+ );
73
+ }
74
+ }
75
+ ```
76
+
77
+ ### Violations format
32
78
Each violations will have the following format :
33
79
``` php
34
80
[
0 commit comments