Documentation ¶
Overview ¶
Package schema implements OpenAPI 3 compatible JSON Schema which can be generated from structs.
Index ¶
- Constants
- Variables
- func F(value float64) *float64
- func GetSchemaPath(configPath string) (schemaPath string)
- func I(value uint64) *uint64
- func WriteJsonConfig(configPath string, config any) (schemaPath string, err error)
- func WriteYamlConfig(configPath string, config any) (schemaPath string, err error)
- type Mode
- type Schema
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 GetSchemaPath ¶
func I ¶
I returns a pointer to the given int. Useful helper function for pointer schema validators like MaxLength or MinItems.
func WriteJsonConfig ¶
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 `json:"type,omitempty"` Description string `json:"description,omitempty"` Items *Schema `json:"items,omitempty"` Properties map[string]*Schema `json:"properties,omitempty"` AdditionalProperties interface{} `json:"additionalProperties,omitempty"` PatternProperties map[string]*Schema `json:"patternProperties,omitempty"` Required []string `json:"required,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"` PropertyNames interface{} `json:"propertyNames,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"` ContentEncoding string `json:"contentEncoding,omitempty"` Ref string `json:"$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 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.