dentelis / php-json-structure-validator
JSON数据结构验证框架
2.0.0
2024-02-06 08:58 UTC
Requires
- php: ~8.2
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ^10.2@dev
This package is auto-updated.
Last update: 2024-09-22 18:12:20 UTC
README
Validator是一个轻量级的PHP库,用于验证从外部JSON API检索到的数据结构。
该库最初是为接受API测试而创建的,但也可用于其他任何地方。
具体
该库是为了测试与JSON兼容的数据结构而制作的。不支持数组键验证。
安装
使用包管理器 composer 进行安装。
composer require dentelis/php-json-structure-validator
用法
您可以使用您想要的任何测试框架(或没有测试框架)使用该库。
有关PhpUnit支持,请参阅 dentelis/phpunit-json-assert。
//object example $user = (new ObjectType()) ->addProperty('name', (new StringType())->assertNotEmpty()) ->addProperty('email', (new StringType())->assertEmail()); $data = json_decode('{"name":"user", "email":"user@example.com"}'); try { $user->validate($data); } catch (ValidationException $e) { //do smth } //array of objects $users = (new ArrayType()) ->assertNotEmpty() ->assertType($user); $data = json_decode('[{"name":"user", "email":"user@example.com"},{"name":"user", "email":"user@example.com"}]'); try { $users->validate($data); } catch (ValidationException $e) { //do smth }
有关完整示例,请参阅 examples 目录。
待办事项
- 更多示例
- 异常中的正确路径
- 不要在第一次异常时失败
- 注释翻译成英文
- 从类名创建TypeInterface(数组要做什么)
贡献
欢迎提交拉取请求。对于主要更改,请先打开一个问题来讨论您想要更改的内容。
请确保根据需要更新测试。