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
On string fields:
minLength:"5"
- Set the minimum length of the valuemaxLength:"5"
- Set the maximum length of the valueenum:"apple|banana|pear"
- Limit the available values to a defined set, separated by vertical barsconst:"I need to be there"
- Require the field to have a specific value.
On numeric types (strings and floats)
min:"-4.141592"
- Set a minimum valuemax:"123456789"
- Set a maximum valueexclusiveMin:"0"
- Values must be strictly greater than this valueexclusiveMax:"11"
- Values must be strictly smaller than this valueconst:"42"
- Property must have exactly this value.
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"
}
]
}
}
}