yoanm / jsonrpc-params-symfony-validator-sdk
Simple JSON-RPC params validator that use Symfony validator component
Installs: 106 767
Dependents: 3
Suggesters: 1
Security: 0
Stars: 1
Watchers: 3
Forks: 2
Open Issues: 1
Requires
- php: ^8.0
- symfony/validator: ^4.4 || ^5.4 || ^6.0
- yoanm/jsonrpc-server-sdk: ^3.0
Requires (Dev)
- behat/behat: ^3.9.0
- dvdoug/behat-code-coverage: ^5.0
- phpspec/prophecy: ^1.15
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^3.5
- yoanm/php-unit-extended: ^2.0
Suggests
- yoanm/jsonrpc-params-symfony-constraint-doc-sdk: To generate payload documentation from validation constraints
- yoanm/symfony-jsonrpc-http-server: Symfony Bundle to convert an HTTP json-rpc request into HTTP json-rpc response
- yoanm/symfony-jsonrpc-params-validator: Symfony bundle for easy JSON-RPC params validation
This package is auto-updated.
Last update: 2024-10-31 00:21:55 UTC
README
Simple JSON-RPC params validator that use Symfony validator component
See yoanm/symfony-jsonrpc-params-validator for automatic dependency injection.
See yoanm/jsonrpc-params-symfony-constraint-doc-sdk for documentation generation.
Versions
-
Symfony v3/4 - PHP >=7.1 :
^v1.0
-
Symfony v4/5 - PHP >=7.2 :
^v2.0
⚠️⚠️
v0.2.0
is replaced byv1.0.0
! ⚠️⚠️⚠️⚠️
v0.3.0
was badly taggued, usedv2.0.0
instead ! ⚠️⚠️ -
Symfony v4.4/5.4/6.0 - PHP ^8.0 :
^v2.1
How to use
In order to be validated, a JSON-RPC method must :
- Implements
JsonRpcMethodInterface
fromyoanm/jsonrpc-server-sdk
- Implements
MethodWithValidatedParamsInterface
With yoanm/jsonrpc-server-sdk
Create the validator and inject it into request handler :
$requestHandler->setMethodParamsValidator( new JsonRpcParamsValidator( (new ValidatorBuilder())->getValidator() ) );
Then you can send JSON-RPC request string to the server and any method wich implements MethodWithValidatedParamsInterface
will be validated.
Standalone
use Symfony\Component\Validator\ValidatorBuilder; use Yoanm\JsonRpcParamsSymfonyValidator\Infra\JsonRpcParamsValidator; // Create the validator $paramsValidator = new JsonRpcParamsValidator( (new ValidatorBuilder())->getValidator() ); // Validate a given JSON-RPC method instance against a JSON-RPC request $violationList = $paramsValidator->validate($jsonRpcRequest, $jsonRpcMethod);
Params validation example
use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraints\Collection; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\NotNull; use Yoanm\JsonRpcParamsSymfonyValidator\Domain\MethodWithValidatedParamsInterface; use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface; class MethodExample implements JsonRpcMethodInterface, MethodWithValidatedParamsInterface { /** * {@inheritdoc} */ public function apply(array $paramList = null) { return 'result'; } public function getParamsConstraint(): Constraint { return new Collection( [ 'fields' => [ 'fieldA' => new NotNull(), 'fieldB' => new NotBlank(), ], ] ); } }
Violations format
Each violations will have the following format :
[ 'path' => 'property_path', 'message' => 'violation message', 'code' => 'violation_code' ]