2017 © Pedro Peláez
 

symfony-bundle json-schema-bundle

Symfony2 bundle for Justin Rainbow's json-schema library.

image

hadesarchitect/json-schema-bundle

Symfony2 bundle for Justin Rainbow's json-schema library.

  • Friday, June 17, 2016
  • by HadesArchitect
  • Repository
  • 1 Watchers
  • 12 Stars
  • 25,128 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 7 Forks
  • 2 Open issues
  • 4 Versions
  • 12 % Grown

The README.md

Json-schema bundle

Build Status Latest Stable Version Total Downloads Dependency Status SensioLabsInsight, (*1)

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

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

Installation

Install through composer:, (*3)

First step: require bundle, (*4)

composer require hadesarchitect/json-schema-bundle ~1.0

Second step: enable bundle, (*5)

<?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:, (*6)

  • 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

The Versions

17/06 2016

dev-master

9999999-dev

Symfony2 bundle for Justin Rainbow's json-schema library.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aleksander Volochnev

17/06 2016

v1.0.0

1.0.0.0

Symfony2 bundle for Justin Rainbow's json-schema library.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aleksander Volochnev

05/06 2016

0.1.1

0.1.1.0

Symfony2 bundle for Justin Rainbow's json-schema library.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aleksander Volochnev

30/11 2014

0.1.0

0.1.0.0

Symfony2 bundle for Justin Rainbow's json-schema library.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aleksander Volochnev