pxhub / php-json-schema-generator
A JSON Schema Generator.
Installs: 775
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 22
pkg:composer/pxhub/php-json-schema-generator
Requires
- php: >=5.6.0
- ext-json: *
Requires (Dev)
- league/json-guard: 1.*
- league/json-reference: 1.*
- php-coveralls/php-coveralls: ^2.0.0
- phpunit/phpunit: 5.7.*
This package is not auto-updated.
Last update: 2025-10-23 05:38:02 UTC
README
Originaly forked from evaisse/php-json-schema-generator
List of changes:
- Collect examples for each scalar type.
- Items properties merged to single list instead of 'anyOf' list.
Introduction to json schema below (and tools) :
- 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 :
Quickstart
Install using composer
composer require evaisse/php-json-schema-generator
Most simple case
$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
[
    '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
$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 properties list
$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
Testing
just run phpunit through
composer test
debug with
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.
- 
provide an option to allow null values in most fields ("type": ["string", "null"]}