validation

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2023 License: MIT Imports: 9 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Required      = &Invalid{Code: utils.VAL_REQUIRED, Error: "required"}
	TypeInt       = &Invalid{Code: utils.VAL_INT_TYPE, Error: "must be an integer"}
	TypeUUID      = &Invalid{Code: utils.VAL_UUID_TYPE, Error: "must be a uuid"}
	TypeBool      = &Invalid{Code: utils.VAL_BOOL_TYPE, Error: "must be a boolean"}
	TypeArray     = &Invalid{Code: utils.VAL_ARRAY_TYPE, Error: "must be an array"}
	TypeString    = &Invalid{Code: utils.VAL_STRING_TYPE, Error: "must be a string"}
	TypeFloat     = &Invalid{Code: utils.VAL_FLOAT_TYPE, Error: "must be a number"}
	TypeObject    = &Invalid{Code: utils.VAL_OBJECT_TYPE, Error: "must be an object"}
	StringPattern = &Invalid{Code: utils.VAL_STRING_PATTERN, Error: "is not valid"}
)

Functions

func ChoiceData added in v0.0.7

func ChoiceData[T any](valid []T) choiceData[T]

func MaxData added in v0.0.7

func MaxData(max any) maxData

func MinData added in v0.0.7

func MinData(min any) minData

func RangeData added in v0.0.7

func RangeData(min any, max any) rangeData

func ValueData added in v0.0.7

func ValueData(value any) valueData

Types

type AnyFuncValidator added in v0.0.7

type AnyFuncValidator[T any] func(value any, ctx *Context[T]) any

type AnyValidator added in v0.0.5

type AnyValidator[T any] struct {
	// contains filtered or unexported fields
}

func Any added in v0.0.5

func Any[T any]() *AnyValidator[T]

func (*AnyValidator[T]) Clone added in v0.0.7

func (v *AnyValidator[T]) Clone() *AnyValidator[T]

func (*AnyValidator[T]) Default added in v0.0.5

func (v *AnyValidator[T]) Default(dflt any) *AnyValidator[T]

func (*AnyValidator[T]) Func added in v0.0.5

func (v *AnyValidator[T]) Func(fn AnyFuncValidator[T]) *AnyValidator[T]

func (*AnyValidator[T]) NotRequired added in v0.0.7

func (v *AnyValidator[T]) NotRequired() *AnyValidator[T]

Meant to be used in conjunction with Clone(). Maybe on create, the field is required, but on update, it isn't.

func (*AnyValidator[T]) Required added in v0.0.5

func (v *AnyValidator[T]) Required() *AnyValidator[T]

func (*AnyValidator[T]) Validate added in v0.0.7

func (v *AnyValidator[T]) Validate(raw any, ctx *Context[T]) any

type ArrayFuncValidator added in v0.0.7

type ArrayFuncValidator[T any] func(value []any, ctx *Context[T]) any

type ArrayValidator added in v0.0.5

type ArrayValidator[T any] struct {
	// contains filtered or unexported fields
}

func Array added in v0.0.5

func Array[T any]() *ArrayValidator[T]

func (*ArrayValidator[T]) ConvertToType added in v0.0.7

func (v *ArrayValidator[T]) ConvertToType() *ArrayValidator[T]

func (*ArrayValidator[T]) Default added in v0.0.5

func (v *ArrayValidator[T]) Default(dflt any) *ArrayValidator[T]

func (*ArrayValidator[T]) Func added in v0.0.7

func (v *ArrayValidator[T]) Func(fn ArrayFuncValidator[T]) *ArrayValidator[T]

func (*ArrayValidator[T]) Max added in v0.0.5

func (v *ArrayValidator[T]) Max(max int) *ArrayValidator[T]

func (*ArrayValidator[T]) Min added in v0.0.5

func (v *ArrayValidator[T]) Min(min int) *ArrayValidator[T]

func (*ArrayValidator[T]) Range added in v0.0.5

func (v *ArrayValidator[T]) Range(min int, max int) *ArrayValidator[T]

func (*ArrayValidator[T]) Required added in v0.0.5

func (v *ArrayValidator[T]) Required() *ArrayValidator[T]

func (*ArrayValidator[T]) Validate added in v0.0.7

func (v *ArrayValidator[T]) Validate(raw any, ctx *Context[T]) any

func (*ArrayValidator[T]) Validator added in v0.0.5

func (v *ArrayValidator[T]) Validator(validator Validator[T]) *ArrayValidator[T]

type BoolFuncValidator added in v0.0.7

type BoolFuncValidator[T any] func(value bool, ctx *Context[T]) any

type BoolValidator

type BoolValidator[T any] struct {
	// contains filtered or unexported fields
}

func Bool

func Bool[T any]() *BoolValidator[T]

func (*BoolValidator[T]) Clone added in v0.0.7

func (v *BoolValidator[T]) Clone() *BoolValidator[T]

func (*BoolValidator[T]) Default added in v0.0.5

func (v *BoolValidator[T]) Default(dflt any) *BoolValidator[T]

func (*BoolValidator[T]) Func added in v0.0.5

func (v *BoolValidator[T]) Func(fn BoolFuncValidator[T]) *BoolValidator[T]

func (*BoolValidator[T]) NotRequired added in v0.0.7

func (v *BoolValidator[T]) NotRequired() *BoolValidator[T]

Meant to be used in conjunction with Clone(). Maybe on create, the field is required, but on update, it isn't.

func (*BoolValidator[T]) Nullable added in v0.0.7

func (v *BoolValidator[T]) Nullable() *BoolValidator[T]

func (*BoolValidator[T]) Required added in v0.0.5

func (v *BoolValidator[T]) Required() *BoolValidator[T]

func (*BoolValidator[T]) Validate

func (v *BoolValidator[T]) Validate(raw any, ctx *Context[T]) any

type Context added in v0.0.7

type Context[T any] struct {

	// The first currently being validated
	Field *Field

	// the full user input
	Input typed.Typed

	// app-specific data that we want to make available to validation callbacks
	Env T
	// contains filtered or unexported fields
}

func NewContext added in v0.0.7

func NewContext[T any](maxErrors uint16) *Context[T]

func (*Context[T]) ArrayIndex added in v0.0.7

func (c *Context[T]) ArrayIndex(i int)

Sets the index array for the current array

func (*Context[T]) CurrentObject added in v0.0.7

func (c *Context[T]) CurrentObject() typed.Typed

func (*Context[T]) EndArray added in v0.0.7

func (c *Context[T]) EndArray()

Removes an array from the stack

func (*Context[T]) EndObject added in v0.0.7

func (c *Context[T]) EndObject()

func (*Context[T]) ErrorCount added in v0.0.7

func (c *Context[T]) ErrorCount() int

func (*Context[T]) Errors added in v0.0.7

func (c *Context[T]) Errors() []any

func (*Context[T]) Invalid added in v0.0.7

func (c *Context[T]) Invalid(invalid *Invalid)

func (*Context[T]) InvalidField added in v0.0.7

func (c *Context[T]) InvalidField(invalid *Invalid)

func (*Context[T]) InvalidWithField added in v0.0.7

func (c *Context[T]) InvalidWithField(invalid *Invalid, field *Field)

func (*Context[T]) IsValid added in v0.0.7

func (c *Context[T]) IsValid() bool

func (*Context[T]) Objects added in v0.0.7

func (c *Context[T]) Objects() []typed.Typed

func (*Context[T]) Release added in v0.0.7

func (c *Context[T]) Release()

func (*Context[T]) ResumeArray added in v0.0.7

func (c *Context[T]) ResumeArray(arrayDepth int)

See SuspendArray

func (*Context[T]) StartArray added in v0.0.7

func (c *Context[T]) StartArray()

Arrays can be deeply nested. We keep a stack of values indicating what array index we're currently at, at each level. StartArray adds a new arrayDepth to the stack

func (*Context[T]) StartObject added in v0.0.7

func (c *Context[T]) StartObject(o typed.Typed)

func (*Context[T]) SuspendArray added in v0.0.7

func (c *Context[T]) SuspendArray() int

This is an advanced and very hacky thing. It's generally a "good thing" that this context is stateful. But in some cases, a caller might need more flexibility. This came up because within the process of validating an array, we needed to validate some other data which would not inherit the array index. This call erases the array state of the context. It returns this state (which is just the arrayDepth) to the caller, so that the caller can pass it back to ResumeArray when it's time to go back to the normal valiation

func (*Context[T]) Validate added in v0.0.7

func (c *Context[T]) Validate(field *Field, value any, validator Validator[T]) (any, bool)

Directly executes a validator with the given value and for the given field. This is generally called when validation of fields is dynamic, often in a a Func callbac(like Object().Func) where the fields to validate are dependent on other parts of the data

type Field added in v0.0.5

type Field struct {
	// The name of the actual field that we need to look up. This is
	// always equal to the last element in Path
	Name string

	// strings.Join(Path, ".")
	Flat string

	// The full path of the field.
	// Could be a single value, like: ["name"],
	// Could be nested, like ["user", "name"],
	// Could contain placeholders for array indexex, like: ["users", "", name]
	Path []string
}

func BuildField added in v0.0.7

func BuildField(flat string) *Field

func SimpleField added in v0.0.7

func SimpleField(name string) *Field

type FloatFuncValidator added in v0.0.7

type FloatFuncValidator[T any] func(value float64, ctx *Context[T]) any

type FloatValidator added in v0.0.5

type FloatValidator[T any] struct {
	// contains filtered or unexported fields
}

func Float added in v0.0.5

func Float[T any]() *FloatValidator[T]

func (*FloatValidator[T]) Clone added in v0.0.7

func (v *FloatValidator[T]) Clone() *FloatValidator[T]

func (*FloatValidator[T]) Default added in v0.0.5

func (v *FloatValidator[T]) Default(dflt any) *FloatValidator[T]

func (*FloatValidator[T]) Func added in v0.0.5

func (v *FloatValidator[T]) Func(fn FloatFuncValidator[T]) *FloatValidator[T]

func (*FloatValidator[T]) Max added in v0.0.5

func (v *FloatValidator[T]) Max(max float64) *FloatValidator[T]

func (*FloatValidator[T]) Min added in v0.0.5

func (v *FloatValidator[T]) Min(min float64) *FloatValidator[T]

func (*FloatValidator[T]) NotRequired added in v0.0.7

func (v *FloatValidator[T]) NotRequired() *FloatValidator[T]

Meant to be used in conjunction with Clone(). Maybe on create, the field is required, but on update, it isn't.

func (*FloatValidator[T]) Nullable added in v0.0.7

func (v *FloatValidator[T]) Nullable() *FloatValidator[T]

func (*FloatValidator[T]) Range added in v0.0.5

func (v *FloatValidator[T]) Range(min float64, max float64) *FloatValidator[T]

func (*FloatValidator[T]) Required added in v0.0.5

func (v *FloatValidator[T]) Required() *FloatValidator[T]

func (*FloatValidator[T]) Validate added in v0.0.7

func (v *FloatValidator[T]) Validate(raw any, ctx *Context[T]) any

type IntFuncValidator added in v0.0.7

type IntFuncValidator[T any] func(value int, ctx *Context[T]) any

type IntValidator

type IntValidator[T any] struct {
	// contains filtered or unexported fields
}

func Int

func Int[T any]() *IntValidator[T]

func (*IntValidator[T]) Clone added in v0.0.7

func (v *IntValidator[T]) Clone() *IntValidator[T]

func (*IntValidator[T]) Default added in v0.0.5

func (v *IntValidator[T]) Default(dflt any) *IntValidator[T]

func (*IntValidator[T]) Func added in v0.0.5

func (v *IntValidator[T]) Func(fn IntFuncValidator[T]) *IntValidator[T]

func (*IntValidator[T]) Max added in v0.0.5

func (v *IntValidator[T]) Max(max int) *IntValidator[T]

func (*IntValidator[T]) Min added in v0.0.5

func (v *IntValidator[T]) Min(min int) *IntValidator[T]

func (*IntValidator[T]) NotRequired added in v0.0.7

func (v *IntValidator[T]) NotRequired() *IntValidator[T]

Meant to be used in conjunction with Clone(). Maybe on create, the field is required, but on update, it isn't.

func (*IntValidator[T]) Nullable added in v0.0.7

func (v *IntValidator[T]) Nullable() *IntValidator[T]

func (*IntValidator[T]) Range added in v0.0.5

func (v *IntValidator[T]) Range(min int, max int) *IntValidator[T]

func (*IntValidator[T]) Required added in v0.0.5

func (v *IntValidator[T]) Required() *IntValidator[T]

func (*IntValidator[T]) Validate

func (v *IntValidator[T]) Validate(raw any, ctx *Context[T]) any

type Invalid

type Invalid struct {
	Data  any    `json:"data"` //TODO: add omitempty https://github.com/goccy/go-json/issues/391
	Error string `json:"error"`
	Code  uint32 `json:"code"`
}

func InvalidArrayLen added in v0.0.7

func InvalidArrayLen(min optional.Int, max optional.Int) *Invalid

func InvalidFloatRange added in v0.0.5

func InvalidFloatRange(min optional.Float, max optional.Float) *Invalid

func InvalidIntRange

func InvalidIntRange(min optional.Int, max optional.Int) *Invalid

func InvalidStringChoice added in v0.0.5

func InvalidStringChoice(choices []string) *Invalid

func InvalidStringLength

func InvalidStringLength(min int, max int) *Invalid

func InvalidStringPattern

func InvalidStringPattern(message ...string) *Invalid

type InvalidField

type InvalidField struct {
	*Invalid
	Field string `json:"field"`
}

type NoopValidator added in v0.0.7

type NoopValidator[T any] struct {
	// contains filtered or unexported fields
}

func Noop added in v0.0.7

func Noop[T any]() *NoopValidator[T]

func (*NoopValidator[T]) Validate added in v0.0.7

func (v *NoopValidator[T]) Validate(value any, ctx *Context[T]) any

type ObjectField added in v0.0.7

type ObjectField[T any] struct {
	// contains filtered or unexported fields
}

type ObjectFuncValidator added in v0.0.7

type ObjectFuncValidator[T any] func(value map[string]any, ctx *Context[T]) any

type ObjectValidator added in v0.0.5

type ObjectValidator[T any] struct {
	// contains filtered or unexported fields
}

func Object added in v0.0.5

func Object[T any]() *ObjectValidator[T]

func (*ObjectValidator[T]) Default added in v0.0.7

func (v *ObjectValidator[T]) Default(dflt any) *ObjectValidator[T]

func (*ObjectValidator[T]) Field added in v0.0.5

func (v *ObjectValidator[T]) Field(name string, validator Validator[T]) *ObjectValidator[T]

ObjectValidator is the only validator that holds on to a Field (i.e. it's the only validator that knows what things are called). You'd think the field is just something like "name" or "password", but a field also contains the whole path for nested objects, like "user.name" and "user.password". So, when an ObjectValidator is nested in another ObjectValidator, we need to build up the field paths throughout the graph. While an ArrayValidator doesn't hold a Field directly, its validator can be an ObjectValidator (which does hold Fields), so we also have to inform the ArrayValidator about any nesting so that it can inform its validator (if that validator is an ObjectValidator)

func (*ObjectValidator[T]) ForceField added in v0.0.7

func (v *ObjectValidator[T]) ForceField(field *Field) *ObjectValidator[T]

func (*ObjectValidator[T]) Func added in v0.0.7

func (v *ObjectValidator[T]) Func(fn ObjectFuncValidator[T]) *ObjectValidator[T]

func (*ObjectValidator[T]) Required added in v0.0.7

func (v *ObjectValidator[T]) Required() *ObjectValidator[T]

func (*ObjectValidator[T]) Validate added in v0.0.5

func (v *ObjectValidator[T]) Validate(raw any, ctx *Context[T]) any

func (*ObjectValidator[T]) ValidateInput added in v0.0.7

func (v *ObjectValidator[T]) ValidateInput(input typed.Typed, ctx *Context[T]) bool

While Validate(raw, ctx) can be called on any validator, including this ObjectValidator, the ValidateInput is a speciality function meant to be called at the top level. (ValidateInput is really just a micro-optimization that skips some checks that we know we don't need at the top level, like a type check on the input)

type Pool

type Pool[T any] struct {
	*concurrent.Pool[*Context[T]]
}

func NewPool

func NewPool[T any](count uint16, maxErrors uint16) Pool[T]

func (Pool[T]) Checkout

func (p Pool[T]) Checkout(env T) *Context[T]

type StringFuncValidator

type StringFuncValidator[T any] func(value string, ctx *Context[T]) any

type StringTransform added in v0.0.7

type StringTransform func(value string) string

type StringValidator

type StringValidator[T any] struct {
	// contains filtered or unexported fields
}

func String

func String[T any]() *StringValidator[T]

func (*StringValidator[T]) Choice added in v0.0.5

func (v *StringValidator[T]) Choice(choices ...string) *StringValidator[T]

func (*StringValidator[T]) Clone

func (v *StringValidator[T]) Clone() *StringValidator[T]

func (*StringValidator[T]) Default added in v0.0.5

func (v *StringValidator[T]) Default(dflt any) *StringValidator[T]

func (*StringValidator[T]) Func added in v0.0.5

func (v *StringValidator[T]) Func(fn StringFuncValidator[T]) *StringValidator[T]

func (*StringValidator[T]) Length added in v0.0.5

func (v *StringValidator[T]) Length(min int, max int) *StringValidator[T]

func (*StringValidator[T]) Max added in v0.0.7

func (v *StringValidator[T]) Max(max int) *StringValidator[T]

func (*StringValidator[T]) Min added in v0.0.7

func (v *StringValidator[T]) Min(min int) *StringValidator[T]

func (*StringValidator[T]) NotRequired added in v0.0.7

func (v *StringValidator[T]) NotRequired() *StringValidator[T]

Meant to be used in conjunction with Clone(). Maybe on create, the field is required, but on update, it isn't.

func (*StringValidator[T]) Nullable added in v0.0.7

func (v *StringValidator[T]) Nullable() *StringValidator[T]

func (*StringValidator[T]) Pattern added in v0.0.5

func (v *StringValidator[T]) Pattern(pattern string, message ...string) *StringValidator[T]

func (*StringValidator[T]) Regexp added in v0.0.7

func (v *StringValidator[T]) Regexp(pattern *regexp.Regexp, message ...string) *StringValidator[T]

func (*StringValidator[T]) Required added in v0.0.5

func (v *StringValidator[T]) Required() *StringValidator[T]

func (*StringValidator[T]) Transform added in v0.0.7

func (v *StringValidator[T]) Transform(tx StringTransform) *StringValidator[T]

func (*StringValidator[T]) Validate

func (v *StringValidator[T]) Validate(raw any, ctx *Context[T]) any

type UUIDValidator added in v0.0.5

type UUIDValidator[T any] struct {
	// contains filtered or unexported fields
}

func UUID added in v0.0.5

func UUID[T any]() *UUIDValidator[T]

func (*UUIDValidator[T]) Clone added in v0.0.7

func (v *UUIDValidator[T]) Clone() *UUIDValidator[T]

func (*UUIDValidator[T]) Default added in v0.0.5

func (v *UUIDValidator[T]) Default(dflt any) *UUIDValidator[T]

func (*UUIDValidator[T]) NotRequired added in v0.0.7

func (v *UUIDValidator[T]) NotRequired() *UUIDValidator[T]

Meant to be used in conjunction with Clone(). Maybe on create, the field is required, but on update, it isn't.

func (*UUIDValidator[T]) Required added in v0.0.5

func (v *UUIDValidator[T]) Required() *UUIDValidator[T]

func (*UUIDValidator[T]) Validate added in v0.0.7

func (v *UUIDValidator[T]) Validate(raw any, ctx *Context[T]) any

type Validator added in v0.0.7

type Validator[T any] interface {
	Validate(value any, ctx *Context[T]) any
}

Jump to

Keyboard shortcuts

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