Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

CLI tool to generate JSON Schemas from JSON and YAML files.
https://github.com/holgerjh/genjsonschema-cli

cli json json-schema yaml

Last synced: 4 days ago
JSON representation

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

Awesome Lists containing this project

README

        

# genjsonschema-cli
![Coverage](https://img.shields.io/badge/Coverage-61.7%25-yellow)
![CI](https://github.com/holgerjh/genjsonschema/actions/workflows/go.yml/badge.svg)
[![Go Report Card](https://goreportcard.com/badge/github.com/holgerjh/genjsonschema-cli)](https://goreportcard.com/report/github.com/holgerjh/genjsonschema-cli)

Genjsonschema is a simple CLI for generating [JSON Schemas](https://json-schema.org) 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](https://github.com/holgerjh/genjsonschema-cli/releases) and grab the latest release!

Alternatively, do one of the following:

Using `go install`:

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

Manually (Linux):

```bash
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

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

will output

```JSON
{
"$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

```json
[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:

```YAML
[true, 2, false]
```

But the following is **not** accepted:

```YAML
[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:

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

The following is **not** fine:

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

Providing the above YAML will raise an error.