Download the PHP package pxhub/php-json-schema-generator without Composer
On this page you can find all versions of the php package pxhub/php-json-schema-generator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pxhub/php-json-schema-generator
More information about pxhub/php-json-schema-generator
Files in pxhub/php-json-schema-generator
Package php-json-schema-generator
Short Description A JSON Schema Generator.
License MIT
Homepage https://github.com/Product-Experience-Solutions/php-json-schema-generator
Informations about the package php-json-schema-generator
PHP JSON Schema Generator
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"]}
All versions of php-json-schema-generator with dependencies
ext-json Version *