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.
- Symfony v3/4 - PHP >=7.1 :
^v1.0
- Symfony v4/5 - PHP >=7.2 :
^v2.0
v0.2.0
is replaced by v1.0.0
!
v0.3.0
was badly taggued, used v2.0.0
instead !
In order to be validated, a JSON-RPC method must :
- Implements
JsonRpcMethodInterface
fromyoanm/jsonrpc-server-sdk
- Implements
MethodWithValidatedParamsInterface
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.
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);
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(),
],
]
);
}
}
Each violations will have the following format :
[
'path' => 'property_path',
'message' => 'violation message',
'code' => 'violation_code'
]