Documentation
¶
Overview ¶
Package validation checks values against the constraints of a spec and collects what fails.
Index ¶
- func AnyValid(variants ...VariantErrors) error
- func AtLeastOne(set ...bool) error
- func AtLeastOneOf(members string, has ...bool) error
- func AtMostOne(set ...bool) error
- func Base64(b []byte) string
- func Const[T comparable](v, want T) error
- func Enum[T comparable](v T, values ...T) error
- func EnumJSON[T any](v T, values ...string) error
- func ExactlyOne(set ...bool) error
- func ExactlyOneOf(members string, has ...bool) error
- func Failed(err error) bool
- func Format[S ~string](s S, format string) error
- func Index(path string, i int) string
- func IsZero(v any) bool
- func Join(prefix, field string) string
- func Key(path, key string) string
- func MaxItems[T any](s []T, n int) error
- func MaxLength[S ~string](s S, n int) error
- func MaxProperties[V any](m map[string]V, n int) error
- func Maximum[T Number](v T, bound float64, isExclusive bool) error
- func MinItems[T any](s []T, n int) error
- func MinLength[S ~string](s S, n int) error
- func MinProperties[V any](m map[string]V, n int) error
- func Minimum[T Number](v T, bound float64, isExclusive bool) error
- func MultipleOf[T Number](v T, factor float64) error
- func NotNull[N interface{ ... }](n N) error
- func OneValid(members ...VariantErrors) error
- func Pattern[S ~string](s S, re *regexp.Regexp, pattern string) error
- func SortedKeys[V any](m map[string]V) []string
- func Unique[T comparable](s []T) error
- func UniqueJSON[T any](s []T) error
- type Error
- type Errors
- type Number
- type Rule
- type VariantErrors
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 AtLeastOneOf ¶
AtLeastOneOf is the check of an anyOf of required lists: the value has one or more of them.
func Base64 ¶
Base64 is the text encoding/json writes for b, which the length and pattern of format byte count.
func EnumJSON ¶
EnumJSON checks that v is one of values, each read into a T, by comparing both written as JSON.
func ExactlyOne ¶
ExactlyOne is the check of a oneOf union: one variant set.
func ExactlyOneOf ¶
ExactlyOneOf is the check of a oneOf of required lists: the value has exactly one of them.
func IsZero ¶
IsZero reports whether v is nil or its zero value, by its own IsZero method too, as omitzero sees it.
func MaxProperties ¶
MaxProperties checks that m has at most n keys.
func MinProperties ¶
MinProperties checks that m has at least n keys.
func MultipleOf ¶
MultipleOf checks that v, as the decimal JSON carries, is a whole multiple of a positive factor.
func OneValid ¶
func OneValid(members ...VariantErrors) error
OneValid is the check of oneOf members that share a variant: nil when exactly one passes.
func SortedKeys ¶
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 ¶
UniqueJSON checks that no two items of s have the same JSON, for items Go cannot compare.
Types ¶
type Error ¶
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.
type Errors ¶
type Errors []Error
Errors collects failed checks. errors.As finds it as a *Errors, and each Error in it.
func (*Errors) Append ¶
Append adds err under prefix. Nested validation errors are flattened with their paths joined.
func (*Errors) Err ¶
Err returns nil when nothing failed, so a Validate method can end with return errs.Err().
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 ¶
VariantErrors are the failed checks of one union member, and whether its variant is set.