validate

package
v0.2.0-alpha Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Validate

func Validate(v any) error

Validate checks if the provided value can be validated and reports any validation errors.

The validation process follows these steps:

  1. If v implements both TypeValidateable and Validateable interfaces, its [TypeValidateable.TypeValidate] method is called, followed by its [Validateable.Validate] method.
  2. If v only implements the TypeValidateable interface, its [TypeValidateable.TypeValidate] method is called.
  3. Otherwise, Validate traverses all fields in the struct pointed to by v, applying the same validation logic to each field.

A ValidationError is returned if any validation fails during any step.

If v is nil or not a pointer, Validate returns an InvalidValidateError.

Types

type And

type And[T any, A Validator[T], B Validator[T]] struct{}

And is a meta validator that combines other validators with AND operator. Validators are called in the same order as specified by type parameters.

See also Or, Not

func (And[T, A, B]) Validate

func (And[T, A, B]) Validate(value T) error

type Any

type Any[T any] struct{}

Any accepts any value of T.

func (Any[T]) Validate

func (Any[T]) Validate(T) error

type Base64

type Base64[T constraint.Text] struct{}

Base64 accepts valid base64 encoded strings.

func (Base64[T]) Validate

func (Base64[T]) Validate(value T) error

type CIDR

type CIDR[T constraint.Text] struct{}

CIDR accepts CIDR notation IP address and prefix length, like "192.0.2.0/24" or "2001:db8::/32", as defined in RFC 4632 and RFC 4291.

func (CIDR[T]) Validate

func (CIDR[T]) Validate(value T) error

type Charset

type Charset[T constraint.Text, F charset.Filter] struct{}

Charset accepts non-empty text which contains only runes acceptable by filter.

See also Charset0

func (Charset[T, F]) Validate

func (Charset[T, F]) Validate(value T) error

type Charset0

type Charset0[T constraint.Text, F charset.Filter] struct{}

Charset0 accepts (possibly empty) text which contains only runes acceptable by filter.

See Charset for a non-empty variant.

func (Charset0[T, F]) Validate

func (Charset0[T, F]) Validate(value T) error

type CountryAlpha

type CountryAlpha[T constraint.Text] struct {
	Or[T, CountryAlpha2[T], CountryAlpha3[T]]
}

CountryAlpha2 accepts either CountryAlpha2 or CountryAlpha3

type CountryAlpha2

type CountryAlpha2[T constraint.Text] struct{}

CountryAlpha2 accepts case-insensitive ISO 3166 2-letter country code

func (CountryAlpha2[T]) Validate

func (CountryAlpha2[T]) Validate(value T) error

type CountryAlpha3

type CountryAlpha3[T constraint.Text] struct{}

CountryAlpha2 accepts case-insensitive ISO 3166 3-letter country code

func (CountryAlpha3[T]) Validate

func (CountryAlpha3[T]) Validate(value T) error

type CurrencyAlpha

type CurrencyAlpha[T constraint.Text] struct{}

CurrencyAlpha accepts case-insensitive ISO 4217 alphabetic currency code

func (CurrencyAlpha[T]) Validate

func (CurrencyAlpha[T]) Validate(value T) error

type Email

type Email[T constraint.Text] struct{}

Email accepts a single RFC 5322 address, e.g. "Barry Gibbs <bg@example.com>".

func (Email[T]) Validate

func (Email[T]) Validate(value T) error

type Even

type Even[T constraint.Integer] struct{}

Even accepts integers divisible by two.

func (Even[T]) Validate

func (Even[T]) Validate(value T) error

type HTTPURL

type HTTPURL[T constraint.Text] struct{}

HTTPURL accepts a single http(s) url.

See also URL.

func (HTTPURL[T]) Validate

func (HTTPURL[T]) Validate(value T) error

type IP

type IP[T constraint.Text] struct{}

IP accepts an IP address. The address can be in dotted decimal ("192.0.2.1"), IPv6 ("2001:db8::68"), or IPv6 with a scoped addressing zone ("fe80::1cc0:3e8c:119f:c2e1%ens18").

func (IP[T]) Validate

func (IP[T]) Validate(value T) error

type IPV4

type IPV4[T constraint.Text] struct{}

IP accepts an IP V4 address (e.g. "192.0.2.1").

func (IPV4[T]) Validate

func (IPV4[T]) Validate(value T) error

type IPV6

type IPV6[T constraint.Text] struct{}

IP accepts an IP V6 address, including IPv4-mapped IPv6 addresses. The address can be regular IPv6 ("2001:db8::68"), or IPv6 with a scoped addressing zone ("fe80::1cc0:3e8c:119f:c2e1%ens18").

func (IPV6[T]) Validate

func (IPV6[T]) Validate(value T) error

type InFuture

type InFuture[T constraint.Time] struct{}

InFuture accepts any time after current timestamp

See also InPast

func (InFuture[T]) Validate

func (InFuture[T]) Validate(value T) error

type InPast

type InPast[T constraint.Time] struct{}

InPast accepts any time before current timestamp

See also InFuture

func (InPast[T]) Validate

func (InPast[T]) Validate(value T) error

type InvalidValidateError

type InvalidValidateError struct {
	Type reflect.Type
}

InvalidValidateError describes an invalid argument passed to Validate. (The argument to Validate must be a non-nil pointer.)

func (InvalidValidateError) Error

func (e InvalidValidateError) Error() string

type JSON

type JSON[T constraint.Text] struct{}

JSON accepts valid json encoded text

func (JSON[T]) Validate

func (JSON[T]) Validate(value T) error

type LangAlpha

type LangAlpha[T constraint.Text] struct {
	Or[T, LangAlpha2[T], LangAlpha3[T]]
}

LangAlpha accepts either LangAlpha2 or LangAlpha3

type LangAlpha2

type LangAlpha2[T constraint.Text] struct{}

LangAlpha2 accepts case-insesitive ISO 639 2-letter language code.

func (LangAlpha2[T]) Validate

func (LangAlpha2[T]) Validate(value T) error

type LangAlpha3

type LangAlpha3[T constraint.Text] struct{}

LangAlpha3 accepts case-insesitive ISO 639 3-letter language code.

func (LangAlpha3[T]) Validate

func (LangAlpha3[T]) Validate(value T) error

type Latitude

type Latitude[T constraint.Real] struct{}

Latitude accepts any number in the range [-90; 90]

See also Longitude

func (Latitude[T]) Validate

func (Latitude[T]) Validate(value T) error

type Longitude

type Longitude[T constraint.Real] struct{}

Longitude accepts any number in the range [-180; 180]

See also Latitude

func (Longitude[T]) Validate

func (Longitude[T]) Validate(value T) error

type MAC

type MAC[T constraint.Text] struct{}

MAC accepts an IEEE 802 MAC-48, EUI-48, EUI-64, or a 20-octet IP over InfiniBand link-layer address.

func (MAC[T]) Validate

func (MAC[T]) Validate(value T) error

type MIME

type MIME[T constraint.Text] struct{}

MIME accepts RFC 1521 mime type string

func (MIME[T]) Validate

func (MIME[T]) Validate(value T) error

type Negative

type Negative[T constraint.Real] struct{}

Negative accepts all negative real numbers excluding zero.

See Negative0 for zero including variant.

func (Negative[T]) Validate

func (Negative[T]) Validate(value T) error

type Negative0

type Negative0[T constraint.Real] struct {
	Or[T, Negative[T], Zero[T]]
}

Negative0 accepts all negative real numbers including zero.

See Negative for zero excluding variant.

type NonEmpty

type NonEmpty[S ~[]T, T any] struct{}

NonEmpty accepts a non-empty slice-like (len > 0)

See NonEmptySlice for a slice shortcut

func (NonEmpty[S, T]) Validate

func (NonEmpty[S, T]) Validate(value S) error

type NonEmptySlice

type NonEmptySlice[T any] struct {
	NonEmpty[[]T, T]
}

NonEmpty accepts a non-empty slice (len > 0)

See NonEmpty for a more generic version

type NonZero

type NonZero[T comparable] struct{}

NonZero accepts all non-zero values.

The zero value is: - 0 for numeric types, - false for the boolean type, and - "" (the empty string) for strings.

See Zero

func (NonZero[T]) Validate

func (NonZero[T]) Validate(value T) error

type Not

type Not[T any, V Validator[T]] struct{}

Not is a meta validator that inverts given validator.

See also And, Or

func (Not[T, V]) Validate

func (Not[T, V]) Validate(value T) error

type Odd

type Odd[T constraint.Integer] struct{}

Odd accepts integers not divisible by two.

func (Odd[T]) Validate

func (Odd[T]) Validate(value T) error

type Or

type Or[T any, A Validator[T], B Validator[T]] struct{}

And is a meta validator that combines other validators with OR operator. Validators are called in the same order as type parameters.

See also And, Not

func (Or[T, A, B]) Validate

func (Or[T, A, B]) Validate(value T) error

type Positive

type Positive[T constraint.Real] struct{}

Positive accepts all positive real numbers excluding zero.

See Positive0 for zero inlcuding variant.

func (Positive[T]) Validate

func (Positive[T]) Validate(value T) error

type Positive0

type Positive0[T constraint.Real] struct {
	Or[T, Positive[T], Zero[T]]
}

Positive0 accepts all positive real numbers including zero.

See Positive for zero excluding variant.

type TypeValidateable

type TypeValidateable interface {
	TypeValidate() error
}

TypeValidateable is an interface for types that can validate their types. This is used by required and optional fields so that they can validate if contained values satisfy the schema enforced by Validator backed type.

TL;DR: do not implement nor use this method directly (codegen is exception).

See Validateable interface if you want to implement custom validation

type URL

type URL[T constraint.Text] struct{}

URL accepts a single url. The url may be relative (a path, without a host) or absolute (starting with a scheme).

See also HTTPURL.

func (URL[T]) Validate

func (URL[T]) Validate(value T) error

type UUID

type UUID[T constraint.Text] struct{}

UUID accepts a properly formatted UUID in one of the following formats:

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}

func (UUID[T]) Validate

func (UUID[T]) Validate(value T) error

type Unique

type Unique[S ~[]T, T comparable] struct{}

Unique accepts a slice-like of unique values

See UniqueSlice for a slice shortcut

func (Unique[S, T]) Validate

func (Unique[S, T]) Validate(value S) error

type UniqueSlice

type UniqueSlice[T comparable] struct {
	Unique[[]T, T]
}

Unique accepts a slice of unique values

See Unique for a more generic version

type Validateable

type Validateable interface {
	Validate() error
}

Validateable is an interface for types that can perform validation logic after type validation (by TypeValidateable) has been called without errors.

Primary usecase is custom cross-field validation. E.g. if X is true then Y cannot be empty

type ValidationError

type ValidationError struct {
	Msg   string
	Inner error
	// contains filtered or unexported fields
}

ValidationError describes validation error occured at validate.Validate

func (ValidationError) Error

func (e ValidationError) Error() string

func (ValidationError) Path

func (e ValidationError) Path() string

Path returns the path to the value which raised this error

func (ValidationError) Unwrap

func (e ValidationError) Unwrap() error

func (ValidationError) WithPath

func (e ValidationError) WithPath(path string) ValidationError

WithPath returns a copy of ValidationError with the given path set

type Validator

type Validator[T any] interface {
	Validate(value T) error
}

Validator is an interface that validators must implement. It's a special empty (struct{}) type that is invoked in a form of (*new(V)).Validate(...). Therefore it should not depend on inner state (fields).

type Zero

type Zero[T comparable] struct{}

Zero accepts all zero values.

The zero value is: - 0 for numeric types, - false for the boolean type, and - "" (the empty string) for strings.

See NonZero

func (Zero[T]) Validate

func (Zero[T]) Validate(value T) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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