jzp-coder / json-guard
Installs: 12 417
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- league/json-guard: ^1.0
- league/json-reference: ^1.0
- symfony/config: ^4.0|^5.0
- symfony/dependency-injection: ^4.0|^5.0
- symfony/http-kernel: ^4.0|^5.0
Requires (Dev)
- phpspec/phpspec: ^4.3
This package is not auto-updated.
Last update: 2024-11-02 06:40:00 UTC
README
About
This bundle integrates league/json-guard
with Symfony and offers a light builder for League\JsonGuard\Validator
instance.
Install
composer require jzp-coder/json-guard
Configuration
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/'
Usage
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 } } }
Testing
vendor/bin/phpspec run
Features to be added
An option for caching the schemas will be added soon.
Contributing
If you want to contribute, please fork the repo and create a pull request from your custom branch to master. Thanks!