validation

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package validation checks values against the constraints of a spec and collects what fails.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnyValid

func AnyValid(variants ...VariantErrors) error

AnyValid is the check of an anyOf union: nil when a set variant passes, else all their errors.

func AtLeastOne

func AtLeastOne(set ...bool) error

AtLeastOne is the check of an anyOf union.

func AtLeastOneOf

func AtLeastOneOf(members string, has ...bool) error

AtLeastOneOf is the check of an anyOf of required lists: the value has one or more of them.

func AtMostOne

func AtMostOne(set ...bool) error

AtMostOne is the check of a nullable oneOf union, where nothing set is null.

func Base64

func Base64(b []byte) string

Base64 is the text encoding/json writes for b, which the length and pattern of format byte count.

func Const

func Const[T comparable](v, want T) error

Const checks that v is want.

func Enum

func Enum[T comparable](v T, values ...T) error

Enum checks that v is one of values.

func EnumJSON

func EnumJSON[T any](v T, values ...string) error

EnumJSON checks that v is one of values, each read into a T, by comparing both written as JSON.

func ExactlyOne

func ExactlyOne(set ...bool) error

ExactlyOne is the check of a oneOf union: one variant set.

func ExactlyOneOf

func ExactlyOneOf(members string, has ...bool) error

ExactlyOneOf is the check of a oneOf of required lists: the value has exactly one of them.

func Failed

func Failed(err error) bool

Failed reports whether err holds Errors.

func Format

func Format[S ~string](s S, format string) error

Format checks that s is written in format. Formats it does not know pass.

func Index

func Index(path string, i int) string

Index is the path of item i under path: items[2].

func IsZero

func IsZero(v any) bool

IsZero reports whether v is nil or its zero value, by its own IsZero method too, as omitzero sees it.

func Join

func Join(prefix, field string) string

Join is the path of field under prefix: pet.name, or pet[0] for an index.

func Key

func Key(path, key string) string

Key is the path of the value under key: labels["team"], or team at the top.

func MaxItems

func MaxItems[T any](s []T, n int) error

MaxItems checks that s has at most n items.

func MaxLength

func MaxLength[S ~string](s S, n int) error

MaxLength checks that s has at most n characters.

func MaxProperties

func MaxProperties[V any](m map[string]V, n int) error

MaxProperties checks that m has at most n keys.

func Maximum

func Maximum[T Number](v T, bound float64, isExclusive bool) error

Maximum checks that v is at most bound, or below it when isExclusive.

func MinItems

func MinItems[T any](s []T, n int) error

MinItems checks that s has at least n items.

func MinLength

func MinLength[S ~string](s S, n int) error

MinLength checks that s has at least n characters.

func MinProperties

func MinProperties[V any](m map[string]V, n int) error

MinProperties checks that m has at least n keys.

func Minimum

func Minimum[T Number](v T, bound float64, isExclusive bool) error

Minimum checks that v is at least bound, or above it when isExclusive.

func MultipleOf

func MultipleOf[T Number](v T, factor float64) error

MultipleOf checks that v, as the decimal JSON carries, is a whole multiple of a positive factor.

func NotNull

func NotNull[N interface{ IsNull() bool }](n N) error

NotNull checks that n, a value the spec does not let be null, is not null.

func OneValid

func OneValid(members ...VariantErrors) error

OneValid is the check of oneOf members that share a variant: nil when exactly one passes.

func Pattern

func Pattern[S ~string](s S, re *regexp.Regexp, pattern string) error

Pattern checks that re, compiled from the spec's pattern, matches s.

func SortedKeys

func SortedKeys[V any](m map[string]V) []string

SortedKeys returns the keys of m in order, so errors come out the same on every run.

func Unique

func Unique[T comparable](s []T) error

Unique checks that no two items of s are equal.

func UniqueJSON

func UniqueJSON[T any](s []T) error

UniqueJSON checks that no two items of s have the same JSON, for items Go cannot compare.

Types

type Error

type Error struct {
	Field   string
	Message string
	Rule    Rule
	Limit   any
}

Error is one failed check. Field is a path like items[2].name, empty for the value itself. Limit is the value of Rule in the spec, nil when the rule has none.

func (Error) Error

func (e Error) Error() string

type Errors

type Errors []Error

Errors collects failed checks. errors.As finds it as a *Errors, and each Error in it.

func (*Errors) Add

func (es *Errors) Add(field, message string)

Add adds a failed check that names no rule.

func (*Errors) Append

func (es *Errors) Append(prefix string, err error)

Append adds err under prefix. Nested validation errors are flattened with their paths joined.

func (*Errors) Err

func (es *Errors) Err() error

Err returns nil when nothing failed, so a Validate method can end with return errs.Err().

func (*Errors) Error

func (es *Errors) Error() string

func (*Errors) Required

func (es *Errors) Required(field string)

Required adds a required field that is not set.

func (*Errors) Unwrap

func (es *Errors) Unwrap() []error

type Number

type Number interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
		~float32 | ~float64
}

Number is every Go number type.

type Rule

type Rule string

Rule is the keyword of the spec a value fails, as JSON Schema names it.

const (
	RuleRequired             Rule = "required"
	RuleType                 Rule = "type"
	RuleAdditionalProperties Rule = "additionalProperties"
	RuleMinLength            Rule = "minLength"
	RuleMaxLength            Rule = "maxLength"
	RulePattern              Rule = "pattern"
	RuleFormat               Rule = "format"
	RuleMinimum              Rule = "minimum"
	RuleExclusiveMinimum     Rule = "exclusiveMinimum"
	RuleMaximum              Rule = "maximum"
	RuleExclusiveMaximum     Rule = "exclusiveMaximum"
	RuleMultipleOf           Rule = "multipleOf"
	RuleMinItems             Rule = "minItems"
	RuleMaxItems             Rule = "maxItems"
	RuleUniqueItems          Rule = "uniqueItems"
	RuleMinProperties        Rule = "minProperties"
	RuleMaxProperties        Rule = "maxProperties"
	RuleConst                Rule = "const"
	RuleEnum                 Rule = "enum"
	RuleOneOf                Rule = "oneOf"
	RuleAnyOf                Rule = "anyOf"
	RuleDiscriminator        Rule = "discriminator"
)

The rules of Error.

type VariantErrors

type VariantErrors struct {
	IsSet bool
	Errs  Errors
}

VariantErrors are the failed checks of one union member, and whether its variant is set.

Jump to

Keyboard shortcuts

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