onlinesid/json-schema-provider
Composer 安装命令:
composer require onlinesid/json-schema-provider
包简介
Json Schema Silex Service Provider
README 文档
README
Json Schema Silex Service Provider and validator tool
A Silex Service Provider for validating JSON data against a given Schema.
Installation
Library
$ git clone https://github.com/onlinesid/json-schema-provider.git
Dependencies
Composer (will use the Composer ClassLoader)
$ wget http://getcomposer.org/composer.phar
$ php composer.phar require onlinesid/json-schema-provider:dev-master
Usage
Registering the Service Provider
$app->register(new OnlineSid\Silex\Provider\JsonSchema\ServiceProvider(), array( 'json-schema.options' => array( 'json_schema_dir' => __DIR__.'/public/json-schema/', 'json_message_dir' => __DIR__.'/public/json-message/', 'default_json_message' => 'default.json', ), ));
json_schema_dir: where the json schema files are locatedjson_message_dir: where the json files for custom error messages are locateddefault_json_message: json file where default error messages are located, file must be in directoryjson_message_dir
Usage in controller
$validator = $this->app['json-schema-validator']; $validation_result = $validator->validate(array( 'request' => $this->request->get('booking'), // array to validate 'json_schema' => 'booking.json', // under json_schema_dir 'json_message' => 'booking.json', // under json_message_dir )); // check $validation_result->isValid() to see if validation pass or fail
####json-schema/booking.json See json-schema for more details.
This is your json schema containing constraints/rules
{
"type": "object",
"properties": {
"first_name": {
"type": "string",
"required": true,
"maxLength": 100
},
"last_name": {
"type": "string",
"required": true,
"maxLength": 100
}
}
}
####json-message/booking.json You can specify your own custom error messages.
{
"first_name": {
"label": "First name",
"messages": {
"required": "{{ label }} is required.",
"maxLength": "{{ label }} must not be more than {{ schema.maxLength }} characters long."
}
},
"last_name": {
"label": "Last name",
"messages": {
"required": "{{ label }} is required.",
"maxLength": "{{ label }} must not be more than {{ schema.maxLength }} characters long."
}
}
}
####json-message/default.json You can specify global default error messages (e.g.: not per field but per constraint rule type)
{
"messages": {
"required": "Required field.",
"minLength": "Must be {{ schema.minLength }} chars or more.",
"maxLength": "Must not be more than {{ schema.maxLength }} chars.",
"pattern": "Incorrect format"
}
}
Running the tests
$ phpunit
onlinesid/json-schema-provider 适用场景与选型建议
onlinesid/json-schema-provider 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 50.11k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2015 年 04 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 onlinesid/json-schema-provider 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 onlinesid/json-schema-provider 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 50.11k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-04-03