yoanm/jsonrpc-params-symfony-validator-sdk

使用 Symfony 验证组件的简单 JSON-RPC 参数验证器

v2.1.0 2023-04-02 10:52 UTC

README

License Code size Dependabot Status

Scrutinizer Build Status Scrutinizer Code Quality Codacy Badge

CI codecov Symfony Versions

Latest Stable Version Packagist PHP version

使用 Symfony 验证组件的简单 JSON-RPC 参数验证器

有关自动依赖注入,请参阅yoanm/symfony-jsonrpc-params-validator

有关文档生成,请参阅yoanm/jsonrpc-params-symfony-constraint-doc-sdk

版本

  • Symfony v3/4 - PHP >=7.1 : ^v1.0

  • Symfony v4/5 - PHP >=7.2 : ^v2.0

    ⚠️⚠️ v0.2.0 已被替换为 v1.0.0 ! ⚠️⚠️

    ⚠️⚠️ v0.3.0 标记错误,请使用 v2.0.0 代替!⚠️⚠️

  • Symfony v4.4/5.4/6.0 - PHP ^8.0 : ^v2.1

使用方法

要验证,一个 JSON-RPC 方法必须

使用 yoanm/jsonrpc-server-sdk

创建验证器并将其注入到请求处理器中

$requestHandler->setMethodParamsValidator(
  new JsonRpcParamsValidator(
    (new ValidatorBuilder())->getValidator()
  )
);

然后您可以将 JSON-RPC 请求字符串发送到服务器,并且任何实现了 MethodWithValidatedParamsInterface 的方法都将进行验证。

独立使用

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(),
        ],
      ]
    );
  }
}

违规格式

每个违规将具有以下格式

[
  'path' => 'property_path',
  'message' => 'violation message',
  'code' => 'violation_code'
]

贡献

有关贡献指南,请参阅贡献说明