2017 © Pedro Peláez
 

library silex-json-schema-provider

A Silex service provider for working with JSON Schema

image

jdesrosiers/silex-json-schema-provider

A Silex service provider for working with JSON Schema

  • Tuesday, April 24, 2018
  • by jdesrosiers
  • Repository
  • 1 Watchers
  • 5 Stars
  • 702 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 20 % Grown

The README.md

silex-json-schema-provider

Build Status Scrutinizer Code Quality Code Coverage, (*1)

silex-json-schema-provider is a Silex service provider for working with JSON Schema., (*2)

Installation

Install the silex-json-schema-provider using composer. This project uses sematic versioning., (*3)

composer require jdesrosiers/silex-json-schema-provider "~1.0"

Parameters

  • json-schema.correlationMechanism: ("profile" or "link") Defaults to "link".
  • json-schema.describedBy: (string) A URI identifying the location of a schema that describes the response.

Services

  • json-schema.schema-store: An instance of SchemaStore as described here https://github.com/geraintluff/jsv4-php.
  • json-schema.validator: An object that exposes the Jsv4 methods described here https://github.com/geraintluff/jsv4-php.

Registering

$app->register(new JDesrosiers\Silex\Provider\JsonSchemaServiceProvider());

JSON Validation

JSON Schema validation is supported by https://github.com/geraintluff/jsv4-php., (*4)

$schemaJson = <<<SCHEMA
{
    "$schema": "http://json-schema.org/draft-04/hyper-schema#",
    "type": "object",
    "properties": {
        "id": {
            "type": "string"
        }
    }
}
SCHEMA;
$app["json-schema.schema-store"]->add("/schema/foo", json_decode($schemaJson));
$schema = $app["json-schema.schema-store"]->get("/schema/foo");
$validation = $app["json-schema.validator"]->validate($data, $schema);

Correlation

The JSON Schema specification has two recommendations for correlating a schema to a resource. This service provider registers after middleware that supports both. See http://json-schema.org/latest/json-schema-core.html#anchor33 for more information on schema correlation. Set the $app["json-schema.describedBy"] parameter to the schema that the response should be correlated to., (*5)

$app->get("/foo/{id}", function ($id) use ($app) {
    $app["json-schema.describedBy"] = "/schema/foo";
    return JsonResponse::create(array("id" => $id));
});

Full Example

$app["json-schema.schema-store"]->add("/schema/foo", $app["schemaRepository"]->fetch("foo"));

$app->put("/foo/{id}", function (Request $request, $id) use ($app) {
    $data = json_decode($request->getContent());

    $schema = $app["json-schema.schema-store"]->get("/schema/foo");
    $validation = $app["json-schema.validator"]->validate($data, $schema);
    if (!$validation->valid) {
        $error = array("validationErrors" => $validation->errors);
        return JsonResponse::create($error, Response::HTTP_BAD_REQUEST);
    }

    $isCreated = !$app["fooRepository"]->contains($id);
    $app["fooRepository"]->save($id, $data);

    $app["json-schema.describedBy"] = "/schema/foo";
    return JsonResponse::create($data, $isCreated ? Response::HTTP_CREATED : Response::HTTP_OK);
});

The Versions

24/04 2018

dev-master

9999999-dev

A Silex service provider for working with JSON Schema

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jason Desrosiers

json silex json-schema json schema json hyper-schema hyper-schema jsv4

24/04 2018

v1.0.1

1.0.1.0

A Silex service provider for working with JSON Schema

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jason Desrosiers

json silex json-schema json schema json hyper-schema hyper-schema jsv4

06/06 2016

v1.0.0

1.0.0.0

A Silex service provider for working with JSON Schema

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jason Desrosiers

json silex json-schema json schema json hyper-schema hyper-schema jsv4

31/01 2016

v0.x-dev

0.9999999.9999999.9999999-dev

A Silex service provider for working with JSON Schema

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jason Desrosiers

json silex json-schema json schema json hyper-schema hyper-schema jsv4

29/03 2015

v0.1.0

0.1.0.0

A Silex service provider for working with JSON Schema

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jason Desrosiers

json silex json-schema json schema json hyper-schema hyper-schema jsv4