Documentation
¶
Overview ¶
Package schema provides JSON schema generation for Go structs with support for custom field properties, validation constraints, and tag-based configuration.
nolint:goconst,mnd
Index ¶
Constants ¶
This section is empty.
Variables ¶
var CustomGenerators = &customSchema{ mapSchema: make(map[string]CustomGenerator), }
CustomGenerators is the global registry for custom schema generators. Use Register to add custom generators and Remove to delete them.
Functions ¶
func GetFieldName ¶
func GetFieldName(fieldType reflect.StructField) (string, bool)
GetFieldName extracts the JSON field name from a struct field. It checks the "json" tag for an explicit name; if absent, it uses the field name converted to camelCase (first letter lowercase). Returns the field name and true if the field should be included, or empty string and false if not.
func GetNameAndRequiredFlag ¶
func GetNameAndRequiredFlag(field reflect.StructField) (string, bool)
GetNameAndRequiredFlag extracts the JSON field name and determines if the field is required. It checks the struct field's json tag for the name and validate tag for the "required" constraint. Returns the field name and a boolean indicating if the field is required. Unexported fields (those with non-empty PkgPath) are ignored.
func SetProperties ¶
func SetProperties(field reflect.StructField, s *jsonschema.Schema)
SetProperties populates a JSON schema with properties from struct field tags. It processes the following tags:
- "schema": sets Title and Description (format: "title,description")
- "default": sets the Default value
- "validate": sets validation constraints (min, max, oneof, etc.)
- "schemaGen": applies a custom generator by name
Types ¶
type CustomGenerator ¶
type CustomGenerator func(field reflect.StructField, s *jsonschema.Schema)
CustomGenerator is a function type for customizing JSON schema generation. It receives the struct field and the schema to modify.
type Generator ¶
type Generator struct {
Reflector *jsonschema.Reflector
}
Generator creates JSON schemas from Go types with custom field reflection logic. It uses a jsonschema.Reflector configured with custom field name and property handlers.
func NewGenerator ¶
func NewGenerator() *Generator
NewGenerator creates a new Generator instance with default configuration. The reflector is set to expand structs and avoid using references ($ref).
type Schema ¶
type Schema *jsonschema.Schema
Schema is a pointer to a jsonschema.Schema, representing a JSON Schema document.