Coder Social home page Coder Social logo

jsonschemabundle's Introduction

Json-schema bundle

Build Status Latest Stable Version Total Downloads Dependency Status SensioLabsInsight

Symfony bundle for Justin Rainbow's JsonSchema library: https://github.com/justinrainbow/json-schema. Offers symfony services instead of classic-style $validator = new Validator();

A PHP Implementation for validating `JSON` Structures against a given `Schema`.
See http://json-schema.org for more details.

Installation

Install through composer:

First step: require bundle

composer require hadesarchitect/json-schema-bundle ~1.0

Second step: enable bundle

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new HadesArchitect\JsonSchemaBundle\JsonSchemaBundle(),
    );
}

Usage

Example

Get schema from predefined schemas

// Acme\DemoBundle\Controller\DemoController
// Get the schema and data as objects

$retriever = $this->get('hades.json_schema.uri_retriever');
$schema = $retriever->retrieve('http://json-schema.org/geo'));

Validate object against schema

// Acme\DemoBundle\Controller\DemoController
// Obtain validator
$validator = $this->get('hades.json_schema.validator');

// Get schema
$schema = $this->get('hades.json_schema.uri_retriever')->retrieve('http://json-schema.org/address');

// Prepare object
$object = array();
$object['country-name'] = 'Some country';
$object['region']       = 'Some region';
$object['locality']     = 'Some locality';

// Validate with check method, which throws exceptions in case of violations
$validator->check((object) $object, $schema); // Minimal requirements satisfied, so everything is OK

unset($object['country-name']);
unset($object['region']);

try {
    $validator->check((object) $object, $schema); // ViolationException thrown.
} catch (\HadesArchitect\JsonSchemaBundle\Exception\ViolationException $exc) {

    echo $exc->getMessage(); // The property region is required. The property country-name is required.

    foreach ($exc->getErrors() as $error) {
        // the property region is required
        // the property country-name is required
        echo $error->getViolation();
    }
}

// Also you can use isValid instead of check
$result = $validator->isValid((object) $object, $schema); // Result is false, because necessary properties not set

// Result is false now, so we can obtain errors from validator
if (!$result) {
    $errors = $validator->getErrors();

    foreach ($errors as $error) {
        // the property region is required
        // the property country-name is required
        echo $error->getViolation();
    }
    // Errors will be kept in validator till next validation (check or isValid)
}

Available services

  • hades.json_schema.validator
  • hades.json_schema.uri_retriever
  • hades.json_schema.uri_resolver

Parameters

Parameters which you could override in your parameters.yml:

  • hades.json_schema.validator.class - real validator class
  • hades.json_schema.validator.service.class - validator service class
  • hades.json_schema.uri_resolver.class - real resolver class
  • hades.json_schema.uri_resolver.service.class - resolver service class
  • hades.json_schema.uri_retriever.class - real retriever class
  • hades.json_schema.uri_retriever.service.class - retriever service class

jsonschemabundle's People

Contributors

hadesarchitect avatar lube avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

jsonschemabundle's Issues

I forked you

Hi,

I added some features to this bundle in my fork:

  • CLI schema generation (based on doctrine or php)
  • annotation validation
  • translations (probably a lot to improve there, just a proof of concept for now)

Are you interested by those features? I could send a PR, but I'm not sure about the primary goal of this bundle, or how much is it supposed to do.

Cheers

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.