onlinesid/json-schema-provider
最新稳定版本:v0.2
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
统计信息
- 总下载量: 46.42k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-04-03