Basic json-schema generator based on Go types, for easy interchange of Go structures across languages.
Fork of github.com/mcuadros/go-jsonschema-generator. Thank you very much. Updated and refactored to handle null from pointer fields.
- uses
[]string
instead ofstring
for thetype
field, and append "null" in case the field is a pointer. - adds format for known sized numbers,
{"type":"integer", "format": "i16"}
. Supported formats:u8,u16,u32,u64,i8,i16,i32,i64,f32,f64
.
The recommended way to install go-jsonschema-generator
go get github.com/ajzo90/go-jsonschema-generator
A basic example:
package main
import (
"fmt"
"time"
"github.com/ajzo90/go-jsonschema-generator"
)
type EmbeddedType struct {
Zoo *string
}
type Item struct {
Value string
}
type ExampleBasic struct {
Foo bool `json:"foo"`
Bar string `json:",omitempty"`
Qux *int8
Baz []string
EmbeddedType
List []Item
MyTime time.Time
MapWithUnknownValueType map[string]interface{}
MapWithKnownValueType map[string]uint64
}
func main() {
fmt.Println(jsonschema.New(ExampleBasic{}).Indented())
}
{
"$schema": "http://json-schema.org/schema#",
"type": [
"object"
],
"properties": {
"Bar": {
"type": [
"string"
]
},
"Baz": {
"type": [
"array"
],
"items": {
"type": [
"string"
]
}
},
"List": {
"type": [
"array"
],
"items": {
"type": [
"object"
],
"properties": {
"Value": {
"type": [
"string"
]
}
},
"required": [
"Value"
]
}
},
"MapWithKnownValueType": {
"type": [
"object"
],
"properties": {
".*": {
"type": [
"integer"
],
"format": "u64"
}
}
},
"MapWithUnknownValueType": {
"type": [
"object"
],
"additionalProperties": true
},
"MyTime": {
"type": [
"string"
],
"format": "date-time"
},
"Qux": {
"type": [
"integer",
"null"
],
"format": "i8"
},
"Zoo": {
"type": [
"string",
"null"
]
},
"foo": {
"type": [
"boolean"
]
}
},
"required": [
"foo",
"Qux",
"Baz",
"Zoo",
"List",
"MyTime",
"MapWithUnknownValueType",
"MapWithKnownValueType"
]
}
MIT, see LICENSE