Documentation ¶
Overview ¶
Package schema implements OpenAPI 3 compatible JSON Schema which can be generated from structs.
Example ¶
type MyObject struct { ID string `doc:"Object ID" readOnly:"true"` Rate float64 `doc:"Rate of change" minimum:"0"` Coords []int `doc:"X,Y coordinates" minItems:"2" maxItems:"2"` } generated, err := Generate(reflect.TypeOf(MyObject{})) if err != nil { panic(err) } fmt.Println(generated.Properties["id"].ReadOnly)
Output: true
Index ¶
Examples ¶
Constants ¶
const ( TypeBoolean = "boolean" TypeInteger = "integer" TypeNumber = "number" TypeString = "string" TypeArray = "array" TypeObject = "object" )
JSON Schema type constants
Variables ¶
var ErrSchemaInvalid = errors.New("schema is invalid")
ErrSchemaInvalid is sent when there is a problem building the schema.
Functions ¶
func F ¶
F returns a pointer to the given float64. Useful helper function for pointer schema validators like Maximum or Minimum.
func I ¶
I returns a pointer to the given int. Useful helper function for pointer schema validators like MaxLength or MinItems.
func IsAnonymous ¶
func IsAnonymous(field reflect.StructField) bool
Types ¶
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 { Type string `yaml:"type,omitempty"` Description string `yaml:"description,omitempty"` Items *Schema `yaml:"items,omitempty"` Properties map[string]*Schema `yaml:"properties,omitempty"` AdditionalProperties interface{} `yaml:"additionalProperties,omitempty"` PatternProperties map[string]*Schema `yaml:"patternProperties,omitempty"` Required []string `yaml:"required,omitempty"` Format string `yaml:"format,omitempty"` Enum []interface{} `yaml:"enum,omitempty"` Default interface{} `yaml:"default,omitempty"` Examples []interface{} `yaml:"examples,omitempty"` Minimum *float64 `yaml:"minimum,omitempty"` ExclusiveMinimum *bool `yaml:"exclusiveMinimum,omitempty"` Maximum *float64 `yaml:"maximum,omitempty"` ExclusiveMaximum *bool `yaml:"exclusiveMaximum,omitempty"` MultipleOf float64 `yaml:"multipleOf,omitempty"` MinLength *uint64 `yaml:"minLength,omitempty"` MaxLength *uint64 `yaml:"maxLength,omitempty"` Pattern string `yaml:"pattern,omitempty"` MinItems *uint64 `yaml:"minItems,omitempty"` MaxItems *uint64 `yaml:"maxItems,omitempty"` UniqueItems bool `yaml:"uniqueItems,omitempty"` MinProperties *uint64 `yaml:"minProperties,omitempty"` MaxProperties *uint64 `yaml:"maxProperties,omitempty"` AllOf []*Schema `yaml:"allOf,omitempty"` AnyOf []*Schema `yaml:"anyOf,omitempty"` OneOf []*Schema `yaml:"oneOf,omitempty"` Not *Schema `yaml:"not,omitempty"` Nullable bool `yaml:"nullable,omitempty"` ReadOnly bool `yaml:"readOnly,omitempty"` WriteOnly bool `yaml:"writeOnly,omitempty"` Deprecated bool `yaml:"deprecated,omitempty"` ContentEncoding string `yaml:"contentEncoding,omitempty"` Ref string `yaml:"$ref,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 GenerateFromField ¶
GenerateFromField generates a schema for a single struct field. It returns the computed field name, whether it is optional, its schema, and any error which may have occurred.
func GenerateWithMode ¶
GenerateWithMode creates a JSON schema for a Go type. Struct field tags can be used to provide additional metadata such as descriptions and validation. The mode can be all, read, or write. In read or write mode any field that is marked as the opposite will be excluded, e.g. a write-only field would not be included in read mode. If a schema is given as input, add to it, otherwise creates a new schema.
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.
func (*Schema) RemoveProperty ¶
RemoveProperty removes a property by name from the schema, making sure to also remove it from the required property set if present.