Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/ryojerryyu/go-jsonschema

A JSON schema code generator for Go, supporting draft 2020-12, supporting reference resolving betewen multiple schema files.
https://github.com/ryojerryyu/go-jsonschema

json-schema

Last synced: about 2 months ago
JSON representation

A JSON schema code generator for Go, supporting draft 2020-12, supporting reference resolving betewen multiple schema files.

Awesome Lists containing this project

README

        

# go-jsonschema

[![Go Report Card](https://goreportcard.com/badge/github.com/RyoJerryYu/go-jsonschema)](https://goreportcard.com/report/github.com/RyoJerryYu/go-jsonschema) [![Go Reference](https://pkg.go.dev/badge/github.com/RyoJerryYu/go-jsonschema.svg)](https://pkg.go.dev/github.com/RyoJerryYu/go-jsonschema) ![Go version](https://img.shields.io/github/go-mod/go-version/RyoJerryYu/go-jsonschema/master) ![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/RyoJerryYu/go-jsonschema?sort=semver)

A [JSON schema] code generator for Go, supporting draft 2020-12,
supporting reference resolving between multiple schema files.

This module is a fork of [git.sr.ht/~emersion/go-jsonschema], with the following improvement:

- Support stdin and stdout for input and output.
- Support multiple schema files. Will resolve references between files correctly.
- Support base local directory for resolving relative references, and base URI for resolving downloaded references.
- Support special uppercase field names, such as `ID` and `URL`.
- Support additional properties and pattern properties.

For the above features, we introduce some breaking changes,
so I publish this module instead of raising a PR.

## Installation

```sh
go install github.com/RyoJerryYu/go-jsonschema/cmd/jsonschemagen
```

## Usage

```sh
jsonschemagen -s -o
```

Or, with the support of stdin/stdout and multiple schema files:

```sh
find schema -name '*.json' | xargs jsonschemagen --rootdir=$PWD -n structs -u id -u url > out/generated.go
```

Full usage:

```
Generate Go types and helpers for the specified JSON schema.
If no schema file is specified or specified to "-", read from stdin.

Usage:
jsonschemagen [flags] [schema file]...

Examples:
$ find schema -name '*.json' | xargs jsonschemagen --rootdir=$PWD -n out > out/generated.go

Flags:
--baseuri string base URI
-h, --help help for jsonschemagen
-o, --output string The output filename.
If not provided or specified to "-", output to stdout.
-n, --packagename string package name
--rootdir string root directory
-s, --schema string The schema filename, deprecated.
recommended to use positional argument.
"-" for stdin.
-u, --upper-property-names strings Apply full upper case to the property names.
e.g. given "id", "Id" or "ID" as flags, when a type or field name
parsed as "Id", would be converted as "ID"
--with-additional-properties Generate additional properties and pattern properties
```

One Go type per definition will be generated.

- `int64` is used for `"type": "integer"`.
- `json.Number` is used for `"type": "number"`.
- schemas with `"type": ["null", ]` and `{"oneOf": [{"type": "null"}, ]}` are considered as optional, will be generated as `*`.
- Resolvable references are generated as the corresponding Go type. Non-resolvable references are generated as `json.RawMessage`.
- Additional properties and pattern properties are not generated by default. Use `--with-additional-properties` to generate them as `map[string]json.RawMessage` .

## License

[MIT](./LICENSE)

[git.sr.ht/~emersion/go-jsonschema]: https://git.sr.ht/~emersion/go-jsonschema
[JSON schema]: https://json-schema.org/