Skip to content

CLI tool to generate JSON Schemas from JSON and YAML files.

License

Notifications You must be signed in to change notification settings

holgerjh/genjsonschema-cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

genjsonschema-cli

Coverage CI Go Report Card

Genjsonschema is a simple CLI for generating JSON Schemas from YAML and JSON documents. It supports the generation of one schema from multiple input files. If multiple files are given, schema generation only succeeds if the resulting schema would be valid for all input files at once.

Installation

Go to the releases page and grab the latest release!

Alternatively, do one of the following:

Using go install:

 go install https://github.com/holgerjh/genjsonschema-cli

Manually (Linux):

 make build && install -m 755 genjsonschema-cli ~/.local/bin && make clean  # assuming ~/.local/bin is in PATH

Usage

genjsonschema-cli create [-f file2] [-f file3...] file1 [flags]

Arguments:

Arguments Description
file1 ... fileN Input file(s). Use '-' to read from STDIN.
-a, --allow-additional Generates a schema that allows unknown object properties that were not encountered during schema generation. Default: false
-f, --file stringArray Additional file that will be merged into main file before creating the schema. Can be specified mulitple times.
-h, --help help for create
-d, --id string Fill the schema $id field.
-m, --merge-only Do not generate a schema. Instead, output the JSON result of the merge operation. Default: false
-o, --output string Output file. Default is STDOUT.
-r, --require-all Generates a schema that requires all object properties to be set. Default: false

Example

echo '{"foo": "bar"}' | genjsonschema-cli create - | jq

will output

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "additionalProperties": false,
  "properties": {
    "foo": {
      "type": "string"
    }
  },
  "type": "object"
}

Multiple files

The aim of genjsonschema is to guarantee that the resulting schema is valid for every input file it was generated from.

Therefore, the following semantics apply:

  • Objects are deep-merged

  • Lists are merged constructively (i.e. added together)

  • Scalar values are rejected, if they are not of the same type. Numbers and integers are considered the same type, and mixing them results in genjsonschema choosing the number over the integer

To inspect the output of the merge operation, provide -m. Note that list order is not preserved and duplicate elements are removed.

List Handling

The schema generated by genjsonschema always defines lists using the anyOf keyword for its items. In addition, lists won't be limited on length.

A schema generated from

[1, true]

will thus accept a list with an undefined number of integers, booleans, and combinations thereof, but will reject other element types such as string.

Given the schema generated from the JSON above, the following is accepted:

[true, 2, false]

But the following is not accepted:

[1, "foo"]

Restrictions on input

YAML is only supported as far as there exists an equivalent JSON expression. Notably, mappings may only use strings as keys.

The following is fine:

foo: "bar"  # ok because key "foo" is of type string

The following is not fine:

42: "bar"  # not ok because 42 is an integer

Providing the above YAML will raise an error.