This bundle integrates league/json-guard
with Symfony and offers a light builder for League\JsonGuard\Validator
instance.
composer require jzp-coder/json-guard
For now, the bundle requires only the folder path where the JSON schemas are stored:
json_guard:
json_schema_root_path: '%kernel.root_dir%/../config/schemas/'
Assuming that the json_schema_root_path
is set and inside is a test.json
schema, you could use the JSON builder like this:
public function test(\JzpCoder\JsonGuard\Builder\JsonValidatorBuilder $builder)
{
$data = <<<EOF
{
"eanCode": '123',
}
EOF;
$validator = $builder->setJsonSchema('test')
->setData($data)
->build();
return new Response($validator->passes());
}
There's also a new constraint added, isNumeric
, which validates that a value is numeric or not. So, '123'
and 123
values will pass, but if isNumeric
is set to false
, then 123
and '123'
values will not pass. See a small example of JSON schema below:
{
"properties": {
"eanCode": {
"type": ["integer", "string"],
"isNumeric": true
}
}
}
vendor/bin/phpspec run
An option for caching the schemas will be added soon.
If you want to contribute, please fork the repo and create a pull request from your custom branch to master. Thanks!