validation

package
v1.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package validation provides shared rule parsing and runtime validation helpers used by server request parsing, OpenAPI generation, and servergen.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FieldRequired

func FieldRequired(f reflect.StructField, tagKey string) bool

FieldRequired reports whether a bound field must be present in the request. Query, header, and cookie parameters are optional unless marked required:"true" or validate:"required" (matching OpenAPI's default for non-path parameters). Body, form, and file fields are required unless the field is a pointer or opts out via omitempty / required:"false".

func FieldRequiredFromTag

func FieldRequiredFromTag(tag string, typ reflect.Type) bool

func HasMultipartTag

func HasMultipartTag(f reflect.StructField) bool

func HasParamTag

func HasParamTag(f reflect.StructField) bool

func HasTagOption

func HasTagOption(tag, option string) bool

func IndirectType

func IndirectType(t reflect.Type) reflect.Type

func IsJSONBodyField

func IsJSONBodyField(f reflect.StructField) bool

func JSONFieldName

func JSONFieldName(f reflect.StructField) (string, bool)

func JSONPointer

func JSONPointer(parts ...string) string

func JoinPointer

func JoinPointer(base string, parts ...string) string

func JoinPointerWithIndex

func JoinPointerWithIndex(base string, index int) string

func StringLength

func StringLength(value string) int

func TagName

func TagName(tag string) string

func ValidateStructValue

func ValidateStructValue(v reflect.Value, spec *StructSpec, scope string) error

func ValidateUUID

func ValidateUUID(value string) bool

Types

type Error

type Error struct {
	Problems []Problem `json:"errors"`
}

func (*Error) Add

func (e *Error) Add(pointer, code, message string)

func (*Error) AddInvalidRule

func (e *Error) AddInvalidRule(pointer string, err error)

func (*Error) AddInvalidType

func (e *Error) AddInvalidType(pointer string)

func (*Error) AddProblem

func (e *Error) AddProblem(problem Problem)

func (*Error) AddRequired

func (e *Error) AddRequired(pointer string)

func (*Error) AddRule

func (e *Error) AddRule(pointer string, kind RuleKind, subject Subject, value int)

func (*Error) Empty

func (e *Error) Empty() bool

func (*Error) Error

func (e *Error) Error() string

func (*Error) Merge

func (e *Error) Merge(other *Error)

type FieldRules

type FieldRules struct {
	Rules     []Rule
	ItemRules []Rule
	Required  bool
	OmitEmpty bool
}

func ParseFieldRules

func ParseFieldRules(tag reflect.StructTag) (FieldRules, error)

type FieldSpec

type FieldSpec struct {
	Type        reflect.Type
	ElementType reflect.Type
	Nested      *StructSpec
	ElemNested  *StructSpec
	Name        string
	Pointer     string
	FullPointer string
	Rules       FieldRules
	Index       int
	IndexPath   []int
	HasParam    bool
	Required    bool
}

type FlattenedField added in v1.1.0

type FlattenedField struct {
	Field reflect.StructField
	Index []int
}

func FlattenFields added in v1.1.0

func FlattenFields(t reflect.Type) ([]FlattenedField, error)

type Problem

type Problem struct {
	Pointer string `json:"pointer"`
	Code    string `json:"code"`
	Message string `json:"message"`
}

func InvalidRuleProblem

func InvalidRuleProblem(pointer string, err error) Problem

func InvalidTypeProblem

func InvalidTypeProblem(pointer string) Problem

func RequiredProblem

func RequiredProblem(pointer string) Problem

func RuleProblem

func RuleProblem(pointer string, kind RuleKind, subject Subject, value int) Problem

func (Problem) MarshalJSON

func (p Problem) MarshalJSON() ([]byte, error)

type Rule

type Rule struct {
	Kind   RuleKind
	String string
	List   []string
	Int    int
}

type RuleKind

type RuleKind string
const (
	RuleMin       RuleKind = "min"
	RuleMax       RuleKind = "max"
	RuleLen       RuleKind = "len"
	RuleMinItems  RuleKind = "minItems"
	RuleMaxItems  RuleKind = "maxItems"
	RuleOneOf     RuleKind = "oneof"
	RulePattern   RuleKind = "pattern"
	RuleEmail     RuleKind = "email"
	RuleURL       RuleKind = "url"
	RuleUUID      RuleKind = "uuid"
	RuleRequired  RuleKind = "required"
	RuleOmitEmpty RuleKind = "omitempty"
)

type SchemaConstraints

type SchemaConstraints struct {
	Format    string
	MinLength *int
	MaxLength *int
	Minimum   *float64
	Maximum   *float64
	Pattern   string
	MinItems  *int
	MaxItems  *int
	Enum      []any
}

func ConstraintsForField

func ConstraintsForField(f reflect.StructField, schemaType string) (SchemaConstraints, error)

type StructSpec

type StructSpec struct {
	Type   reflect.Type
	Fields []FieldSpec
}

func Struct

func Struct(t reflect.Type) (*StructSpec, error)

type Subject

type Subject string
const (
	SubjectString     Subject = "string"
	SubjectNumber     Subject = "number"
	SubjectCollection Subject = "collection"
)

Jump to

Keyboard shortcuts

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