schema

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToJSONString

func ToJSONString(rule *SchemaRule) (string, error)

ToJSONString converts a SchemaRule to a pretty-printed JSON Schema string

Types

type FileValidator

type FileValidator interface {
	Filename() string
	Size() int64
	MimeType() string
}

FileValidator is the interface required for multipart file validation

type JSONSchemaNode

type JSONSchemaNode struct {
	Type                 string                     `json:"type,omitempty"`
	Description          string                     `json:"description,omitempty"`
	Format               string                     `json:"format,omitempty"`
	Properties           map[string]*JSONSchemaNode `json:"properties,omitempty"`
	Required             []string                   `json:"required,omitempty"`
	Items                *JSONSchemaNode            `json:"items,omitempty"`
	AdditionalProperties *bool                      `json:"additionalProperties,omitempty"`
	Default              any                        `json:"default,omitempty"`
	MinLength            *int                       `json:"minLength,omitempty"`
	MaxLength            *int                       `json:"maxLength,omitempty"`
	Minimum              *float64                   `json:"minimum,omitempty"`
	Maximum              *float64                   `json:"maximum,omitempty"`
}

JSONSchemaNode represents standard JSON Schema Draft 7/2020-12 structure

func ToJSONSchema

func ToJSONSchema(rule *SchemaRule) *JSONSchemaNode

ToJSONSchema converts a SchemaRule into a standard JSON Schema node

type RuleType

type RuleType string

RuleType denotes the schema data type

const (
	TypeString  RuleType = "string"
	TypeNumber  RuleType = "number"
	TypeInteger RuleType = "integer"
	TypeBoolean RuleType = "boolean"
	TypeArray   RuleType = "array"
	TypeObject  RuleType = "object"
	TypeFile    RuleType = "file"
)

type SchemaRule

type SchemaRule struct {
	Type         RuleType               `json:"type"`
	Optional     bool                   `json:"optional,omitempty"`
	Description  string                 `json:"description,omitempty"`
	MinLength    *int                   `json:"minLength,omitempty"`
	MaxLength    *int                   `json:"maxLength,omitempty"`
	Pattern      string                 `json:"pattern,omitempty"`
	Format       string                 `json:"format,omitempty"` // e.g. "email", "binary"
	Minimum      *float64               `json:"minimum,omitempty"`
	Maximum      *float64               `json:"maximum,omitempty"`
	Properties   map[string]*SchemaRule `json:"properties,omitempty"`
	Items        *SchemaRule            `json:"items,omitempty"`
	MaxSizeBytes int64                  `json:"maxSizeBytes,omitempty"`
	MimeTypes    []string               `json:"mimeTypes,omitempty"`
	Required     []string               `json:"required,omitempty"`
	Default      any                    `json:"default,omitempty"`
}

SchemaRule defines a single validation constraint & documentation node

func Array

func Array(itemRule *SchemaRule) *SchemaRule

func Boolean

func Boolean() *SchemaRule

func File

func File() *SchemaRule

func FromStruct

func FromStruct(s any) *SchemaRule

FromStruct derives a SchemaRule automatically from a standard Go struct definition

func Integer

func Integer() *SchemaRule

func Number

func Number() *SchemaRule

func Object

func Object(props map[string]*SchemaRule) *SchemaRule

func Optional

func Optional(rule *SchemaRule) *SchemaRule

func String

func String() *SchemaRule

func (*SchemaRule) AllowedMimes

func (r *SchemaRule) AllowedMimes(mimes ...string) *SchemaRule

func (*SchemaRule) Describe

func (r *SchemaRule) Describe(desc string) *SchemaRule

func (*SchemaRule) Email

func (r *SchemaRule) Email() *SchemaRule

func (*SchemaRule) Max

func (r *SchemaRule) Max(max int) *SchemaRule

func (*SchemaRule) MaxSize

func (r *SchemaRule) MaxSize(bytes int64) *SchemaRule

func (*SchemaRule) MaxValue

func (r *SchemaRule) MaxValue(max float64) *SchemaRule

func (*SchemaRule) Min

func (r *SchemaRule) Min(min int) *SchemaRule

func (*SchemaRule) MinValue

func (r *SchemaRule) MinValue(min float64) *SchemaRule

func (*SchemaRule) WithDefault

func (r *SchemaRule) WithDefault(val any) *SchemaRule

type ValidationErrorDetail

type ValidationErrorDetail struct {
	Path    string `json:"path"`
	Message string `json:"message"`
}

ValidationErrorDetail represents a single field-level schema validation failure

func Validate

func Validate(rule *SchemaRule, data any, currentPath string) []ValidationErrorDetail

Validate checks a generic payload map against a SchemaRule, enforcing strict contracts

Jump to

Keyboard shortcuts

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