Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/holgerjh/genjsonschema
Go module to generate JSON Schemas from JSON and YAML files.
https://github.com/holgerjh/genjsonschema
Last synced: about 1 month ago
JSON representation
Go module to generate JSON Schemas from JSON and YAML files.
- Host: GitHub
- URL: https://github.com/holgerjh/genjsonschema
- Owner: holgerjh
- License: apache-2.0
- Created: 2022-03-29T17:56:42.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-04-21T19:21:01.000Z (over 2 years ago)
- Last Synced: 2023-11-16T17:28:17.727Z (about 1 year ago)
- Language: Go
- Homepage:
- Size: 30.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# genjsonschema
![Test & Lint](https://github.com/holgerjh/genjsonschema/actions/workflows/go.yml/badge.svg)
[![Go Report Card](https://goreportcard.com/badge/github.com/holgerjh/genjsonschema)](https://goreportcard.com/report/github.com/holgerjh/genjsonschema)
![Coverage](https://img.shields.io/badge/Coverage-93.5%25-brightgreen)Genjsonschema is a simple [JSON Schema generator](https://json-schema.org).
It generates schemas in accordance with from JSON and YAML (some restrictions apply, see below).
See also the documentation at [pkg.go.dev](https://pkg.go.dev/github.com/holgerjh/genjsonschema).
## Install
```bash
go get github.com/holgerjh/genjsonschema
```## Examples
Gnerate and print a JSON Schema from a simple JSON object:
```go
package mainimport (
"fmt""github.com/holgerjh/genjsonschema"
)func main() {
from := []byte("{'foo': 'bar'}")
schema, err := genjsonschema.GenerateFromJSON(from, nil)
if err != nil {
panic(err)
}
fmt.Printf("%s", schema)
}
```This will print the following (without whitespace):
```JSON
{
"$schema": "http://json-schema.org/draft-07/schema",
"additionalProperties": false,
"properties": {
"foo": {
"type": "string"
}
},
"type": "object",
"required": [
"foo"
]
}
```For YAML input, there exists an equivalent function named `GenerateFromYAML`.
## Restrictions \& Peculiarities
### Restrictions on YAML 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.
### List interpretation
The schema generated by Genjsonschema always defines a list 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.
#### Example
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"]
```