Generate JSON Schema out of Golang schema
First install the package
go get -u github.com/urakozz/go-json-schema-generator
Then create your generator file (see Example folder)
package main
import (
"fmt"
"github.com/urakozz/go-json-schema-generator"
)
type Domain struct {
Data string `json:"data"`
}
func main(){
fmt.Println(generator.Generate(&Domain{}))
}
required:"true"
- field will be marked as requireddescription:"description"
- description will be added
If struct field is pointer to the primitive type, then schema will allow this typa and null. E.g.:
type Domain struct {
NullableData *string `json:"nullableData"`
}
Output
{
"$schema": "http://json-schema.org/schema#",
"type": "object",
"properties": {
"nullableData": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
]
}
}
}