validate

package
v1.0.0-alpha3 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UndTag = "und"
	// The field must be required(Some or Defined).
	// mutually exclusive to nullish, def, null, und.
	// UndTagValueRequired can be combined with len (there's no point though).
	UndTagValueRequired = "required"
	// The field must be nullish(None, Null, Undefined).
	// mutually exclusive to required, def, null, und.
	// UndTagValueNullish can be combined with len.
	UndTagValueNullish = "nullish"
	// The field is allowed to be Some or Defined.
	// can be combined with null, und or len.
	UndTagValueDef = "def"
	// The field is allowed to be None or Null.
	// can be combined with def, und or len.
	UndTagValueNull = "null"
	// The field is allowed to be None or Undefined.
	// can be combined with def, null or len.
	UndTagValueUnd = "und"
	// Only for elastic types.
	//
	// The value must be formatted as len==n, len>n, len>=n, len<n or len<=n,
	// where n is unsigned integer.
	// The field's length will be evaluated as (length) (comparison operator) (n),
	// e.g. if tag is len>12, field.Len() > 12 must return true.
	//
	// can be combined with other options.
	UndTagValueLen = "len"
	// Only for elastic types.
	//
	// The value must be formatted as values:nonnull.
	//
	// nonnull value means its internal value must not have null.
	UndTagValueValues = "values"
)

Variables

View Source
var (
	// ErrNotStruct would be returned by ValidateUnd and CheckUnd
	// if input is not a struct nor a pointer to a struct.
	ErrNotStruct = errors.New("not struct")
	// ErrMultipleOption would be returned by ValidateUnd and CheckUnd
	// if input's `und` struct tags have multiple mutually exclusive options.
	ErrMultipleOption = errors.New("multiple option")
	// ErrUnknownOption is an error value which will be returned by ValidateUnd and CheckUnd
	// if an input has unknown options in `und` struct tag.
	ErrUnknownOption = errors.New("unknown option")
	// ErrMalformedLen is an error which will be returned by ValidateUnd and CheckUnd
	// if an input has malformed len option in `und` struct tag.
	ErrMalformedLen = errors.New("malformed len")
	// ErrMalformedLen is an error which will be returned by ValidateUnd and CheckUnd
	// if an input has malformed values option in `und` struct tag.
	ErrMalformedValues = errors.New("malformed values")
)

Functions

func CheckUnd

func CheckUnd(s any) error

CheckUnd checks whether s is correctly configured with `und` struct tag option without validating it.

func ValidateUnd

func ValidateUnd(s any) error

ValidateUnd validates whether s is compliant to the constraint placed by `und` struct tag.

ValidateUnd only accepts struct or pointer to struct.

Only fields whose struct tag contains `und`, and whose type is implementor of OptionLike, UndLike or ElasticLike, are validated.

Types

type CheckerUnd

type CheckerUnd interface {
	CheckUnd() error
}

CheckerUnd wraps the CheckUnd method which is expected to be implemented on data container types like und.Und[T] and option.Option[T], etc.

CheckUnd must checks if its internal data type conforms the constraint which ValidateUnd or CheckUnd would checks.

type ElasticLike

type ElasticLike interface {
	UndLike
	Len() int
	HasNull() bool
}

type OptionLike

type OptionLike interface {
	IsNone() bool
	IsSome() bool
}

type UndLike

type UndLike interface {
	IsDefined() bool
	IsNull() bool
	IsUndefined() bool
}

type ValidatorUnd

type ValidatorUnd interface {
	ValidateUnd() error
}

ValidatorUnd wraps the ValidateUnd method.

ValidateUnd method is implemented on data container types, und.Und[T] and option.Option[T], etc. It only validates its underlying T's compliance for constraints placed by `und` struct tag options.

Jump to

Keyboard shortcuts

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