Skip to content

Commit 3387e08

Browse files
authored
Update README.md
1 parent 97c4209 commit 3387e08

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

README.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,20 @@ Simple JSON-RPC params validator that use Symfony validator component
1414
In order to be validated, a JSON-RPC method must :
1515
- Implements `JsonRpcMethodInterface` from [`yoanm/jsonrpc-server-sdk`](https://github.com/yoanm/php-jsonrpc-server-sdk)
1616
- 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
1931
```php
2032
use Symfony\Component\Validator\ValidatorBuilder;
2133
use Yoanm\JsonRpcParamsSymfonyValidator\Infra\JsonRpcParamsValidator;
@@ -29,6 +41,40 @@ $paramsValidator = new JsonRpcParamsValidator(
2941
$violationList = $paramsValidator->validate($jsonRpcRequest, $jsonRpcMethod);
3042
```
3143

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
3278
Each violations will have the following format :
3379
```php
3480
[

0 commit comments

Comments
 (0)