2017 © Pedro Peláez
 

library php-json-schema-generator

A JSON Schema Generator.

image

evaisse/php-json-schema-generator

A JSON Schema Generator.

  • Friday, August 18, 2017
  • by evaisse
  • Repository
  • 1 Watchers
  • 5 Stars
  • 5,920 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 12 Forks
  • 1 Open issues
  • 10 Versions
  • 67 % Grown

The README.md

PHP JSON Schema Generator

Build Statuscodecov, (*1)

Originaly forked from solvire/php-json-schema-generator, (*2)

Introduction to json schema below (and tools) :, (*3)

  • http://json-schema.org — reference
  • http://www.jsonschemavalidator.net - validator (not 100% valid)
  • https://www.openapis.org - use json schema to define REST API docs
  • https://jsonschema.net/#/editor - convenient editor for json schema

To validate your structure against a given schema, you can use :, (*4)

  • http://json-guard.thephpleague.com

Quickstart

Install using composer, (*5)

composer require evaisse/php-json-schema-generator

Most simple case, (*6)

$output = JSONSchemaGenerator\Generator::fromJson('{"a":{"b":2}');

// $output ==> json string
// {
//   "$schema": "http://json-schema.org/draft-04/schema#",
//   "type": "object",
//   "properties": {
//     "a": {
//       "type": "object",
//       "properties": {
//         "b": {
//           "type": "integer"
//         }
//       },
//       "required": ["b"]
//     }
//   },
//   "required": ["a"]
// }

Default configuration values, (*7)

[
    'schema_id'                      => null,
    'properties_required_by_default' => true,
    'schema_uri'                     => 'http://json-schema.org/draft-04/schema#',
    'schema_title'                   => null,
    'schema_description'             => null,
    'schema_type'                    => null,
    "items_schema_collect_mode"      => 0,
    'schema_required_field_names'    => []
]

Advanced usage, (*8)

$result = Generator::fromJson($this->addressJson1, [
    'schema_id' => 'http://foo.bar/schema'
]);

/*

  {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": "http://foo.bar/schema",
    "type": "object",
    "properties": {
      "a": {
        "type": "object",
        "id": "http://foo.bar/schema/a",
        "properties": {
          "b": {
            "id": "http://foo.bar/schema/a/b",
            "type": "integer"
          }
        }
      }
    }

*/


// if you want items as strict lists instead of "anyOf" type
$result = Generator::fromJson($this->addressJson1, [
    'schema_id'                      => 'http://bar.foo/schema2',
    'schema_title'                   => 'coucouc',
    'schema_description'             => 'desc',
    "items_schema_collect_mode"      => Definition::ITEMS_AS_LIST,
]);

/*
    {
        "$schema":"http:\/\/json-schema.org\/draft-04\/schema#",
        ...
        "properties": {
            "phoneNumber":{
                "id":"http:\/\/bar.foo\/schema2\/phoneNumber",
                "type":"array",
                "items": [ 
                    {"id":"http:\/\/bar.foo\/schema2\/0",...},
                    {"id":"http:\/\/bar.foo\/schema2\/1",...}}
*/

For more advanced usage, see tests/JSONSchemaGenerator/Tests/GeneratorTest.php, (*9)

Testing

just run phpunit through, (*10)

composer test

debug with, (*11)

DEBUG=true composer test -- --filter="SearchWord" # for filtering *SearchWord* test case with output debugging

Roadmap

  • Adjust schema comparison using re-ordering of properties to compare two schema against their semantic values instead of just comparing their JSON form. For example { a: 1, b: 2 }, and { b: 2, a: 1 } should result in the same schema., (*12)

  • provide an option to allow null values in most fields ("type": ["string", "null"]}, (*13)

The Versions

18/08 2017

dev-master

9999999-dev https://github.com/evaisse/php-json-schema-generator

A JSON Schema Generator.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Emmanuel VAÏSSE
by Steven Scott

schema parser php json validation generator

18/08 2017

v2.0.6

2.0.6.0 https://github.com/evaisse/php-json-schema-generator

A JSON Schema Generator.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Emmanuel VAÏSSE
by Steven Scott

schema parser php json validation generator

17/08 2017

v2.0.5

2.0.5.0 https://github.com/evaisse/php-json-schema-generator

A JSON Schema Generator.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Emmanuel VAÏSSE
by Steven Scott

schema parser php json validation generator

16/08 2017

v2.0.4

2.0.4.0 https://github.com/evaisse/php-json-schema-generator

A JSON Schema Generator.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Emmanuel VAÏSSE
by Steven Scott

schema parser php json validation generator

16/08 2017

v2.0.3

2.0.3.0 https://github.com/evaisse/php-json-schema-generator

A JSON Schema Generator.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Emmanuel VAÏSSE
by Steven Scott

schema parser php json validation generator

16/08 2017

v2.0.2

2.0.2.0 https://github.com/evaisse/php-json-schema-generator

A JSON Schema Generator.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Emmanuel VAÏSSE
by Steven Scott

schema parser php json validation generator

16/08 2017

v2.0.1

2.0.1.0 https://github.com/evaisse/php-json-schema-generator

A JSON Schema Generator.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Emmanuel VAÏSSE
by Steven Scott

schema parser php json validation generator

16/08 2017

v2.0.0

2.0.0.0 https://github.com/evaisse/php-json-schema-generator

A JSON Schema Generator.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Emmanuel VAÏSSE
by Steven Scott

schema parser php json validation generator

16/08 2017

v1.0.0

1.0.0.0 https://github.com/evaisse/php-json-schema-generator

A JSON Schema Generator.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Emmanuel VAÏSSE
by Steven Scott

schema parser php json validation generator

15/08 2017

v0.1.1

0.1.1.0 https://github.com/evaisse/php-json-schema-generator

A JSON Schema Generator.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Emmanuel VAÏSSE
by Steven Scott

schema parser php json validation generator