onlinesid / json-schema-provider
Json Schema Silex Service Provider
Installs: 45 212
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 1
Open Issues: 0
pkg:composer/onlinesid/json-schema-provider
Requires
- php: >=5.6.0
- justinrainbow/json-schema: ~5.2
- silex/silex: >=2.0
Requires (Dev)
- phpunit/phpunit: ~4.8
This package is auto-updated.
Last update: 2025-10-20 14:03:32 UTC
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