schema

package
v0.6.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package schema reflects Go values into a version-agnostic JSON Schema representation consumed by the OpenAPI emitters in internal/spec. The three emitters (3.0, 3.1, and 3.2) share the same intermediate Schema type; differences in how nullable, $ref, and other version-specific bits are rendered live in the emitter code, not in this package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateDefault added in v0.6.2

func ValidateDefault(s *Schema, fieldName string)

validateDefault panics when a field's default value does not satisfy its own enum, numeric-bound, length, or pattern constraints. Default and the enum members share one parsed type (parseScalar), so the comparison is exact. ValidateDefault is exported so the functional-option params path (routeopt.go) can run the same default-vs-constraints check the struct-tag path gets through applyFieldTags.

Types

type ParamField added in v0.3.0

type ParamField struct {
	// Name is the wire name, taken from the location tag's value.
	Name string
	// In is the parameter location: "query", "header", or "cookie".
	In string
	// Required is the value of the field's required tag (default
	// false — query, header, and cookie parameters are optional by
	// convention).
	Required bool
	// Description is the field's doc/description tag.
	Description string
	// Schema is the parameter's schema, with constraint tags applied.
	Schema *Schema
}

ParamField is one parameter reflected from a params struct; see ParamFields.

func ParamFields added in v0.3.0

func ParamFields(v any) []ParamField

ParamFields reflects a flat struct into parameter declarations. Every exported field must carry exactly one location tag (query, header, or cookie) whose value is the wire name; a value of "-" skips the field. The field's Go type provides the schema (scalars and slices of scalars only), the constraint tag vocabulary applies as on body fields, and a required:"true" tag marks the parameter required. Violations panic, consistent with the module's fail-fast policy: a parameter that silently failed to apply would publish a wrong contract.

type Reflector

type Reflector struct {

	// NoAutoDescriptions suppresses the "Generated from Go type ..."
	// fallback descriptions on named components. User-supplied doc:
	// tags are unaffected.
	NoAutoDescriptions bool
	// contains filtered or unexported fields
}

Reflector accumulates named component schemas across multiple Reflect calls. A single Reflector must be used for one OpenAPI document build so that component-name collisions between same-named types from different packages are detected and suffixed (User, User_2, ...) consistently with the $ref strings handed out at use sites.

A Reflector is not safe for concurrent use.

The Schema model is version-agnostic (the emitters render Nullable and $ref wrappers per OpenAPI version), so a Reflector needs no version.

func NewReflector

func NewReflector() *Reflector

NewReflector returns an empty Reflector.

func (*Reflector) Components

func (r *Reflector) Components() map[string]*Schema

Components returns the accumulated named component schemas. The map is the Reflector's own; callers must not mutate it while still reflecting.

func (*Reflector) Reflect

func (r *Reflector) Reflect(value any) *Schema

Reflect produces a JSON Schema for the dynamic type of value. Named structs are registered as components on the Reflector and returned as $ref schemas. The zero value of value is fine — only the type is used.

func (*Reflector) Renamed added in v0.4.0

func (r *Reflector) Renamed() map[string]bool

Renamed reports the component names that took a collision suffix during reflection — genuine renames, not types that merely contain an underscore-digit pattern.

type Schema

type Schema struct {
	// Type is the JSON Schema type: "object", "array", "string", "number",
	// "integer", "boolean", or "null". Empty means "no type constraint"
	// (matches anything). For interfaces, Type is left empty.
	Type string
	// Format is the JSON Schema format hint: "int32", "int64", "float",
	// "double", "date-time", "date", "time", "byte", "binary", "email", etc.
	Format string
	// Description is a human-readable description of this value.
	Description string
	// Properties maps JSON field name to its Schema. Only for Type=="object".
	Properties map[string]*Schema
	// Required is the list of property names that must be present.
	Required []string
	// Items is the element schema. Only for Type=="array".
	Items *Schema
	// AdditionalProperties constrains map values. Only for Type=="object"
	// when used as a map type.
	AdditionalProperties *Schema
	// Ref is a $ref pointer into the components.schemas section. When set,
	// the emitter emits a $ref object (wrapped when Nullable, Description,
	// or Example are also set) and ignores the structural fields.
	Ref string
	// Enum, when non-empty, restricts the value to a fixed set.
	Enum []any
	// Default is the default value, if any.
	Default any
	// Example is an example value, if any.
	Example any
	// Minimum and Maximum bound numeric values (inclusive). They are
	// kept as json.Number so the document shows the literal the user
	// wrote ("10" stays an integer, "0.5" a decimal). Empty means
	// unset.
	Minimum json.Number
	Maximum json.Number
	// ExclusiveMinimum and ExclusiveMaximum bound numeric values
	// exclusively. The emitters render them per version: a numeric
	// keyword in 3.1/3.2, the 3.0 boolean form (minimum +
	// exclusiveMinimum: true) in 3.0. Mutually exclusive with
	// Minimum/Maximum on the same bound. Empty means unset.
	ExclusiveMinimum json.Number
	ExclusiveMaximum json.Number
	// MinLength and MaxLength bound string lengths. Nil means unset.
	MinLength *uint64
	MaxLength *uint64
	// Pattern is an ECMA-262 regular expression strings must match.
	Pattern string
	// MinItems and MaxItems bound array lengths. Nil means unset.
	MinItems *uint64
	MaxItems *uint64
	// UniqueItems requires array elements to be unique.
	UniqueItems bool
	// Nullable is true when the value may be null (Go: a pointer or interface).
	// In OpenAPI 3.0 this is emitted as `"nullable": true`. In 3.1/3.2 it is
	// expressed by adding "null" to the type array.
	Nullable bool
	// Extensions is a map of x-stdocs-* and similar extension fields. The
	// keys are emitted as-is (e.g. "x-stdocs-type"); values must be
	// JSON-serializable.
	Extensions map[string]any
}

Schema is a version-agnostic JSON Schema (Draft 2020-12 / OpenAPI 3.1 flavour) value. The OpenAPI emitters serialize the same Schema value into their respective JSON representations. The model is version-agnostic: Nullable is rendered differently in 3.0 ("nullable": true) and 3.1/3.2 (a "type" array including "null") at serialization time, without mutating the model.

func ReflectSchema

func ReflectSchema(value any) (*Schema, map[string]*Schema)

ReflectSchema produces a JSON Schema for the given Go value plus the named components it references. It is a convenience wrapper around a single-use Reflector; for whole-document builds use NewReflector so all values share one component namespace.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL