Coder Social home page Coder Social logo

jsonschemabundle's Introduction

Json-schema bundle

Build Status SensioLabsInsight

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
{
}

See here for the full class.

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.

jsonschemabundle's People

Contributors

hadesarchitect avatar soyuka avatar aleliberty avatar

Stargazers

 avatar Ryota Mochizuki avatar Alvaro avatar  avatar Bertrand Zuchuat avatar Sebastian Luberriaga avatar

Watchers

James Cloos avatar  avatar Sebastian Luberriaga avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.