datto / json-rpc-validator
JSON-RPC 库的认证扩展
5.0.0
2017-12-19 16:09 UTC
Requires
- datto/json-rpc-simple: ~4.0
- doctrine/annotations: ~1.3
- symfony/validator: ~3.2
Requires (Dev)
- phpunit/phpunit: ~5.7
This package is not auto-updated.
Last update: 2024-09-14 18:31:36 UTC
README
这是一个基于注解的验证扩展,适用于 php-json-rpc 库。它通过定义良好的约束(例如类型检查、正则表达式检查等)验证 JSON-RPC API 端点。
该库依赖于 symfony/Validator、doctrine/annotations 以及 php-json-rpc-simple。
示例
按照以下方式注解您的 API 端点类
namespace Datto\API; use Datto\JsonRpc\Validator\Validate; use Symfony\Component\Validator\Constraints as Assert; class Math { /** * @Validate(fields={ * "a" = @Assert\Type(type="integer"), * "b" = { * @Assert\Type(type="integer"), * @Assert\NotEqualTo(value="0"), * } * }) */ public function divide($a, $b) { return $a / $b; }
有了这些,就可以像这样使用它。此示例使用 Simple\Evaluator
(请参阅 php-json-rpc-simple)作为底层映射机制
$server = new Server(new Validator\Evaluator(new Simple\Evaluator())); $result = $server->reply('{"jsonrpc": "2.0", "method": "math/divide", "params": { "a": 1, "b": 0 }, "id": 1}'); // Because 'b' cannot be 0, this will return // {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid params"}}
要求
- PHP >= 5.3
安装
"require": { "datto/json-rpc-validator": "~2.0" }
许可协议
此包在开源许可下发布: LGPL-3.0。
作者
由 Philipp C. Heckel 编写。