slack/hack-json-schema
Composer 安装命令:
composer require slack/hack-json-schema
包简介
Hack JSON Schema generator
README 文档
README
Hack JSON Schema is a library for validating JSON inputs, using JSON schemas, in Hack.
Given a JSON schema file, you can generate a validator in Hack to validate incoming JSON against the schema. If the JSON conforms to the schema, the validator will returned typed output.
There are several benefits to generation:
- We don't have to parse the JSON schema to validate the incoming object at runtime.
- We can output typed shapes that are generated from the JSON schema, increasing the type safety of downstream code.
Usage
Codegen::forPath
The most basic way to use this library is to generate a validator from a JSON schema file:
use type Slack\Hack\JsonSchema\Codegen\Codegen; Codegen::forPath('/path/to/json-schema.json', shape( 'validator' => shape( 'file' => '/path/to/MyJsonSchemaValidator.php', 'class' => 'MyJsonSchemaValidator', ), ))->build();
/path/to/MyJsonSchemaValidator.php now exists with a class:
final class MyJsonSchemaValidator extends BaseValidator { ... class contents }
Each validator has a validate method, which takes a decoded JSON object:
$json = json_decode($args['json_input'], true); $validator = new MyJsonSchemaValidator($json); $validator->validate(); if (!$validator->isValid()) { print_r("invalid_json", $validator->getErrors()); return; } // JSON is valid, get typed object: $validated = $validator->getValidatedInput();
Codegen::forPaths
If you have multiple JSON schemas that leverage the $ref attribute, you should prefer to use Codegen::forPaths over Codegen::forPath.
The workflow for Codegen::forPath is:
- Given a JSON schema, "de-reference" the schema. De-referencing is the process of resolving all of the
$refpaths with their actual schema. This creates a single de-referenced schema. - With the de-referenced schema, generate a validator.
This works well if you only have one primary schema, but if you have multiple schemas, each with common refs, you'll start to generate a lot of duplicate code.
In these cases, you can use Codegen::forPaths.
use type Slack\Hack\JsonSchema\Codegen\Codegen; $schemas = vec['/path/to/json-schema-1.json', '/path/to/json-schema-2.json', '/path/to/json-schema-3.json']; Codegen::forPaths($schemas, shape( 'validator' => shape( 'refs' => shape( 'unique' => shape( 'source_root' => '/path/to', 'output_root' => '/path/gen' ) ) ), ))->build();
By defining the source_root and output_root we can generate unique validators per $ref we come across. We can then re-use those validators when generating other validators.
Developing
Installing Dependencies
We handle all dependencies through Docker. It's as simple as:
make install
Running Tests
make test
Related Libraries
This library was inspired by the ideas in these related libraries:
License
Hack JSON Schema is MIT-licensed.
slack/hack-json-schema 适用场景与选型建议
slack/hack-json-schema 是一款 基于 Hack 开发的 Composer 扩展包,目前已累计 2.44k 次下载、GitHub Stars 达 30, 最近一次更新时间为 2019 年 07 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 slack/hack-json-schema 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 slack/hack-json-schema 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 30
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2019-07-25