soyuka / json-schema-bundle
Symfony2 tools bundle for Justin Rainbow's json-schema library.
Installs: 9 500
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 4
Forks: 10
Type:symfony-bundle
Requires
- justinrainbow/json-schema: <2
- psr/cache: ^1.0
- soyuka/php-to-json-schema: ~1.0.1
- symfony/console: ^3.0
- symfony/finder: ^3.0
- symfony/framework-bundle: ^3.0
- symfony/property-access: ^3.0
- symfony/property-info: ^3.0
- symfony/validator: ^3.0
Requires (Dev)
- doctrine/doctrine-bundle: ^1.6
- doctrine/orm: ^2.5
- phpunit/phpunit: ^5.0@dev
- symfony/cache: ^3.1@dev
Suggests
- doctrine/orm: To convert Doctrine entities to json schema's based on metadata
- symfony/cache: Enable caching abilities
README
Based on a fork of https://github.com/HadesArchitect/JsonSchemaBundle
Features
- Json Schema generation through command (via Doctrine or Php)
- Json Schema validation annotation
- Json Schema service that accepts class instances
Installation
Composer:
composer require soyuka/jsonschemabundle
Load the bundle:
<?php // app/AppKernel.php use Soyuka\JsonSchemaBundle\JsonSchemaBundle; public function registerBundles() { $bundles = array( // ... new JsonSchemaBundle(), ); }
Json Schema generation
The default is to parse files located in every Entity
directory of every Bundle. The default strategy uses php to parse your class (see php-to-json-schema).
app/console jsonschema:generate
If you want to parse another directory, just specify it through the --directory
option:
app/console jsonschema:generate -d Model
If you want to use Doctrine annotation to generate a json schema, specify the doctrine
strategy. Note that the doctrine/orm-bundle
will be needed:
app/console jsonschema:generate --strategy doctrine
Every json schema will be written to the configurable directory of your choice by keeping the following tree architecture:
BundleName/Entity.json
The default value is %kernel.root_dir%/Resources/validators
, so assuming the Entity parsed is in AcmeBundle/Entity/Product
, the schema will be written in %kernel.root_dir%/Resources/validators/AcmeBundle/Product.json
.
Validation annotation
This bundle adds a JsonSchema class annotation. When no option is given, the validator will look in the json path (same architecture than with schema generation).
The most basic example will be:
<?php use Soyuka\JsonSchemaBundle\Constraints as JsonSchemaAssert; /** * @JsonSchemaAssert\JsonSchema */ class DefaultJsonSchema { }
If you want to use a json schema at a different location, or even remote, specify the path of your choice:
<?php use Soyuka\JsonSchemaBundle\Constraints as JsonSchemaAssert; /** * @JsonSchemaAssert\JsonSchema(schema = "validators/specific.json") */ class SpecificJsonSchema { }
The schema option value is retrieved using the JsonSchema\Uri\UriRetriever
(any value that works there works in the annotation).
Configuration
json_schema: path: 'foobar' cache: 'some_psr_cache_service' #cache schema's
Translations
Translation domain is JsonSchemaViolations
, therefore default translations would be located in:
%kernel.root_dir%/Resources/translations/JsonSchemaViolations.{lang}.{format}
Every translation will be given following string parameters:
%property%
: the property path%expected%
: the expected value%invalid%
: the current invalid value
Keys are the constraint code, for example with minLength
:
<source>minLength</source> <target>%property% is not long enough, expected %expected% got "%invalid%"</target>
Hack
If you want to handle JsonSchema validation yourself, but have issues using Entity instances with justinrainbow/json-schema
, you may want to take a look at the PropertyAccessorConstraint
. Here, it'll override the ObjectConstraint
so that object properties are accessed through the PropertyAccessor. See the Validator class.
Licence
The MIT License (MIT)
Copyright (c) 2015 Antoine Bluchet
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.