hadesarchitect/json-schema-bundle
Composer 安装命令:
composer require hadesarchitect/json-schema-bundle
包简介
Symfony2 bundle for Justin Rainbow's json-schema library.
README 文档
README
Symfony bundle for Justin Rainbow's JsonSchema library: https://github.com/justinrainbow/json-schema. Offers symfony services instead of classic-style $validator = new Validator();
A PHP Implementation for validating `JSON` Structures against a given `Schema`.
See http://json-schema.org for more details.
Installation
Install through composer:
First step: require bundle
composer require hadesarchitect/json-schema-bundle ~1.0
Second step: enable bundle
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new HadesArchitect\JsonSchemaBundle\JsonSchemaBundle(), ); }
Usage
Example
Get schema from predefined schemas
// Acme\DemoBundle\Controller\DemoController // Get the schema and data as objects $retriever = $this->get('hades.json_schema.uri_retriever'); $schema = $retriever->retrieve('http://json-schema.org/geo'));
Validate object against schema
// Acme\DemoBundle\Controller\DemoController // Obtain validator $validator = $this->get('hades.json_schema.validator'); // Get schema $schema = $this->get('hades.json_schema.uri_retriever')->retrieve('http://json-schema.org/address'); // Prepare object $object = array(); $object['country-name'] = 'Some country'; $object['region'] = 'Some region'; $object['locality'] = 'Some locality'; // Validate with check method, which throws exceptions in case of violations $validator->check((object) $object, $schema); // Minimal requirements satisfied, so everything is OK unset($object['country-name']); unset($object['region']); try { $validator->check((object) $object, $schema); // ViolationException thrown. } catch (\HadesArchitect\JsonSchemaBundle\Exception\ViolationException $exc) { echo $exc->getMessage(); // The property region is required. The property country-name is required. foreach ($exc->getErrors() as $error) { // the property region is required // the property country-name is required echo $error->getViolation(); } } // Also you can use isValid instead of check $result = $validator->isValid((object) $object, $schema); // Result is false, because necessary properties not set // Result is false now, so we can obtain errors from validator if (!$result) { $errors = $validator->getErrors(); foreach ($errors as $error) { // the property region is required // the property country-name is required echo $error->getViolation(); } // Errors will be kept in validator till next validation (check or isValid) }
Available services
- hades.json_schema.validator
- hades.json_schema.uri_retriever
- hades.json_schema.uri_resolver
Parameters
Parameters which you could override in your parameters.yml:
- hades.json_schema.validator.class - real validator class
- hades.json_schema.validator.service.class - validator service class
- hades.json_schema.uri_resolver.class - real resolver class
- hades.json_schema.uri_resolver.service.class - resolver service class
- hades.json_schema.uri_retriever.class - real retriever class
- hades.json_schema.uri_retriever.service.class - retriever service class
hadesarchitect/json-schema-bundle 适用场景与选型建议
hadesarchitect/json-schema-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 91.42k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2014 年 11 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 hadesarchitect/json-schema-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hadesarchitect/json-schema-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 91.42k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 19
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-11-29