Documentation ¶
Overview ¶
Package schema implements OpenAPI 3 compatible JSON Schema which can be generated from structs.
Example ¶
type myDate struct { Day string `json:"day,omitempty"` Month string `json:"month,omitempty"` Year string `json:"year,omitempty"` } type MyObject struct { ID string `json:"id,omitempty" doc:"Object ID" readOnly:"true"` Rate float64 `doc:"Rate of change" minimum:"0"` Coords []int `doc:"X,Y coordinates" minItems:"2" maxItems:"2"` myDate `json:"date,omitempty" dependencies:"day:month;year month:year"` Bucket map[string]interface{} `json:"bucket,omitempty" dependencies:"apple:banana;peach banana:melon"` Target RequireId `json:"target" skill:"github.com/eolinker/apinto/service.service.IService"` } generated, err := Generate(reflect.TypeOf(MyObject{}), nil) if err != nil { panic(err) } bytes, _ := json.MarshalIndent(generated, "", "\t") log.Debug(string(bytes))
Output: {"type":"object","properties":{"bucket":{"type":"object","additionalProperties":{},"dependencies":{"apple":["banana","peach"],"banana":["melon"]}},"coords":{"type":"array","description":"X,Y coordinates","items":{"type":"integer","format":"int32"},"minItems":2,"maxItems":2},"date":{"type":"object","properties":{"day":{"type":"string"},"month":{"type":"string"},"year":{"type":"string"}},"additionalProperties":false,"dependencies":{"day":["month","year"],"month":["year"]}},"id":{"type":"string","description":"Object ID","readOnly":true},"rate":{"type":"number","description":"Rate of change","format":"double","minimum":0}},"additionalProperties":false,"required":["rate","coords"],"dependencies":{"id":["rate"],"rate":["coords","date"]}}
Index ¶
Examples ¶
Constants ¶
const ( TypeBoolean = "boolean" TypeInteger = "integer" TypeNumber = "number" TypeString = "string" TypeArray = "array" TypeObject = "object" TypeMap = "map" TypeRequireId = "require" TypeFileList = "eofiles" TypeFile = "eofile" TypeFormatter = "formatter" )
JSON Schema type constants
Variables ¶
var ErrSchemaInvalid = errors.New("schema is invalid")
ErrSchemaInvalid is sent when there is a problem building the schema.
Functions ¶
Types ¶
type EoFileList ¶
type FormatterConfigType ¶
type FormatterConfigType = eosc.FormatterConfig
type Mode ¶
type Mode int
Mode defines whether the schema is being generated for read or write mode. Read-only fields are dropped when in write mode, for example.
type Schema ¶
type Schema struct { //Name string `json:"name,omitempty"` Type string `json:"type,omitempty"` EOType string `json:"eo:type,omitempty"` Description string `json:"description,omitempty"` Items *Schema `json:"items,omitempty"` Properties map[string]*Schema `json:"properties,omitempty"` AdditionalProperties *Schema `json:"additionalProperties,omitempty"` UISort []string `json:"ui:sort,omitempty"` Required []string `json:"required,omitempty"` EmptyLabel string `json:"empty_label,omitempty"` Format string `json:"format,omitempty"` Enum []interface{} `json:"enum,omitempty"` Default interface{} `json:"default,omitempty"` Example interface{} `json:"example,omitempty"` Minimum *float64 `json:"minimum,omitempty"` ExclusiveMinimum *bool `json:"exclusiveMinimum,omitempty"` Maximum *float64 `json:"maximum,omitempty"` ExclusiveMaximum *bool `json:"exclusiveMaximum,omitempty"` MultipleOf float64 `json:"multipleOf,omitempty"` MinLength *uint64 `json:"minLength,omitempty"` MaxLength *uint64 `json:"maxLength,omitempty"` Pattern string `json:"pattern,omitempty"` MinItems *uint64 `json:"minItems,omitempty"` MaxItems *uint64 `json:"maxItems,omitempty"` UniqueItems bool `json:"uniqueItems,omitempty"` MinProperties *uint64 `json:"minProperties,omitempty"` MaxProperties *uint64 `json:"maxProperties,omitempty"` AllOf []*Schema `json:"allOf,omitempty"` AnyOf []*Schema `json:"anyOf,omitempty"` OneOf []*Schema `json:"oneOf,omitempty"` Not *Schema `json:"not,omitempty"` Nullable bool `json:"nullable,omitempty"` ReadOnly bool `json:"readOnly,omitempty"` WriteOnly bool `json:"writeOnly,omitempty"` Deprecated bool `json:"deprecated,omitempty"` Ref string `json:"$ref,omitempty"` Dependencies map[string][]string `json:"dependencies,omitempty"` Skill string `json:"skill,omitempty"` Switch string `json:"switch,omitempty"` Label string `json:"label,omitempty"` }
Schema represents a JSON Schema which can be generated from Go structs
func Generate ¶
Generate creates a JSON schema for a Go type. Struct field tags can be used to provide additional metadata such as descriptions and validation.
func (*Schema) HasValidation ¶
HasValidation returns true if at least one validator is set on the schema. This excludes the schema's type but includes most other fields and can be used to trigger additional slow validation steps when needed.