hadesarchitect/json-schema-bundle

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

Installs: 87 321

Dependents: 1

Suggesters: 0

Security: 0

Stars: 12

Watchers: 2

Forks: 10

Open Issues: 2

Type:symfony-bundle

v1.0.0 2016-06-16 22:54 UTC

This package is not auto-updated.

Last update: 2024-11-09 16:48:07 UTC


README

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