Instance to JSON schema and back

v0.3.7 2021-10-28 21:58 UTC

This package is auto-updated.

Last update: 2021-11-28 22:11:08 UTC


Build Status Code Climate codecov

Create JSON Schema from instance values and vice versa.


composer require swaggest/json-schema-maker



json-cli build-schema

v1.7.8 json-cli build-schema
JSON CLI tool, https://github.com/swaggest/json-cli
   json-cli build-schema <data> [schema]
   data     Path to data (JSON/YAML)
   schema   Path to parent schema   
   --ptr-in-schema <ptrInSchema>           JSON pointer to structure in root schema, default #                      
   --ptr-in-data <ptrInData>               JSON pointer to structure in data, default #                             
   --jsonl                                 Data is a stream of JSON Lines                                           
   --use-nullable                          Use `nullable: true` instead of `type: null`, OAS 3.0 compatibility      
   --use-xnullable                         Use `x-nullable: true` instead of `type: null`, Swagger 2.0 compatibility
   --defs-ptr <defsPtr>                    Location to put new definitions. default: "#/definitions/"               
   --collect-examples                      Collect scalar values example                                            
   --heuristic-required                    Mark properties that are available in all samples as `required`.         
   --additional-data <additionalData...>   Additional paths to data                                                 
   --pretty                                Pretty-print result JSON                                                 
   --output <output>                       Path to output result, default STDOUT                                    
   --to-yaml                               Output in YAML format                                                    
   --to-serialized                         Output in PHP serialized format                                          

Basic example:

json-cli build-schema tests/assets/original.json 


Advanced example:

json-cli build-schema dump-responses.jsonl ./acme-service/swagger.json --ptr-in-schema "#/definitions/Orders" --jsonl --ptr-in-data "#/responseValue" --pretty --output swagger.json

Updates swagger.json with actual response samples provided in dump-responses.jsonl.

Generating JSON schema based on instance values

$instanceValue = json_decode(file_get_contents(__DIR__ . '/../resources/github-example.json'));
$schema = new \Swaggest\JsonSchema\Schema();
$f = new \Swaggest\JsonSchemaMaker\SchemaMaker($schema);
$f->options->upgradeIntToNumber = true; // Use `type: number` instead of `type: [integer, number]`.


$schemaJson = json_encode(\Swaggest\JsonSchema\Schema::export($schema));     // With object schemas extracted as definitions.
$schemaJsonInline = json_encode($schema);               // With inline object schemas.

See available options.

See example schemas.

Generating fake instance value based on JSON schema

mt_srand(1); // Optionally seed random generator for reproducible results.
$schema = \Swaggest\JsonSchema\Schema::import(json_decode(file_get_contents(__DIR__ . '/../resources/github-example-schema-with-examples.json')));

$instanceFaker = new \Swaggest\JsonSchemaMaker\InstanceFaker($schema);
$value = $instanceFaker->makeValue();
$anotherValue = $instanceFaker->makeValue();

See available options.

See example value.

See also