Documentation ¶
Index ¶
- Variables
- func ChoiceData[T any](valid []T) choiceData[T]
- func MaxData(max any) maxData
- func MinData(min any) minData
- func RangeData(min any, max any) rangeData
- func ValueData(value any) valueData
- type AnyFuncValidator
- type AnyValidator
- func (v *AnyValidator[T]) Clone() *AnyValidator[T]
- func (v *AnyValidator[T]) Default(dflt any) *AnyValidator[T]
- func (v *AnyValidator[T]) Func(fn AnyFuncValidator[T]) *AnyValidator[T]
- func (v *AnyValidator[T]) NotRequired() *AnyValidator[T]
- func (v *AnyValidator[T]) Required() *AnyValidator[T]
- func (v *AnyValidator[T]) Validate(raw any, ctx *Context[T]) any
- type ArrayFuncValidator
- type ArrayValidator
- func (v *ArrayValidator[T]) ConvertToType() *ArrayValidator[T]
- func (v *ArrayValidator[T]) Default(dflt any) *ArrayValidator[T]
- func (v *ArrayValidator[T]) Func(fn ArrayFuncValidator[T]) *ArrayValidator[T]
- func (v *ArrayValidator[T]) Max(max int) *ArrayValidator[T]
- func (v *ArrayValidator[T]) Min(min int) *ArrayValidator[T]
- func (v *ArrayValidator[T]) Range(min int, max int) *ArrayValidator[T]
- func (v *ArrayValidator[T]) Required() *ArrayValidator[T]
- func (v *ArrayValidator[T]) Validate(raw any, ctx *Context[T]) any
- func (v *ArrayValidator[T]) Validator(validator Validator[T]) *ArrayValidator[T]
- type BoolFuncValidator
- type BoolValidator
- func (v *BoolValidator[T]) Clone() *BoolValidator[T]
- func (v *BoolValidator[T]) Default(dflt any) *BoolValidator[T]
- func (v *BoolValidator[T]) Func(fn BoolFuncValidator[T]) *BoolValidator[T]
- func (v *BoolValidator[T]) NotRequired() *BoolValidator[T]
- func (v *BoolValidator[T]) Nullable() *BoolValidator[T]
- func (v *BoolValidator[T]) Required() *BoolValidator[T]
- func (v *BoolValidator[T]) Validate(raw any, ctx *Context[T]) any
- type Context
- func (c *Context[T]) ArrayIndex(i int)
- func (c *Context[T]) CurrentObject() typed.Typed
- func (c *Context[T]) EndArray()
- func (c *Context[T]) EndObject()
- func (c *Context[T]) ErrorCount() int
- func (c *Context[T]) Errors() []any
- func (c *Context[T]) Invalid(invalid *Invalid)
- func (c *Context[T]) InvalidField(invalid *Invalid)
- func (c *Context[T]) InvalidWithField(invalid *Invalid, field *Field)
- func (c *Context[T]) IsValid() bool
- func (c *Context[T]) Objects() []typed.Typed
- func (c *Context[T]) Release()
- func (c *Context[T]) ResumeArray(arrayDepth int)
- func (c *Context[T]) StartArray()
- func (c *Context[T]) StartObject(o typed.Typed)
- func (c *Context[T]) SuspendArray() int
- func (c *Context[T]) Validate(field *Field, value any, validator Validator[T]) (any, bool)
- type Field
- type FloatFuncValidator
- type FloatValidator
- func (v *FloatValidator[T]) Clone() *FloatValidator[T]
- func (v *FloatValidator[T]) Default(dflt any) *FloatValidator[T]
- func (v *FloatValidator[T]) Func(fn FloatFuncValidator[T]) *FloatValidator[T]
- func (v *FloatValidator[T]) Max(max float64) *FloatValidator[T]
- func (v *FloatValidator[T]) Min(min float64) *FloatValidator[T]
- func (v *FloatValidator[T]) NotRequired() *FloatValidator[T]
- func (v *FloatValidator[T]) Nullable() *FloatValidator[T]
- func (v *FloatValidator[T]) Range(min float64, max float64) *FloatValidator[T]
- func (v *FloatValidator[T]) Required() *FloatValidator[T]
- func (v *FloatValidator[T]) Validate(raw any, ctx *Context[T]) any
- type IntFuncValidator
- type IntValidator
- func (v *IntValidator[T]) Clone() *IntValidator[T]
- func (v *IntValidator[T]) Default(dflt any) *IntValidator[T]
- func (v *IntValidator[T]) Func(fn IntFuncValidator[T]) *IntValidator[T]
- func (v *IntValidator[T]) Max(max int) *IntValidator[T]
- func (v *IntValidator[T]) Min(min int) *IntValidator[T]
- func (v *IntValidator[T]) NotRequired() *IntValidator[T]
- func (v *IntValidator[T]) Nullable() *IntValidator[T]
- func (v *IntValidator[T]) Range(min int, max int) *IntValidator[T]
- func (v *IntValidator[T]) Required() *IntValidator[T]
- func (v *IntValidator[T]) Validate(raw any, ctx *Context[T]) any
- type Invalid
- func InvalidArrayLen(min optional.Int, max optional.Int) *Invalid
- func InvalidFloatRange(min optional.Float, max optional.Float) *Invalid
- func InvalidIntRange(min optional.Int, max optional.Int) *Invalid
- func InvalidStringChoice(choices []string) *Invalid
- func InvalidStringLength(min int, max int) *Invalid
- func InvalidStringPattern(message ...string) *Invalid
- type InvalidField
- type NoopValidator
- type ObjectField
- type ObjectFuncValidator
- type ObjectValidator
- func (v *ObjectValidator[T]) Default(dflt any) *ObjectValidator[T]
- func (v *ObjectValidator[T]) Field(name string, validator Validator[T]) *ObjectValidator[T]
- func (v *ObjectValidator[T]) ForceField(field *Field) *ObjectValidator[T]
- func (v *ObjectValidator[T]) Func(fn ObjectFuncValidator[T]) *ObjectValidator[T]
- func (v *ObjectValidator[T]) Required() *ObjectValidator[T]
- func (v *ObjectValidator[T]) Validate(raw any, ctx *Context[T]) any
- func (v *ObjectValidator[T]) ValidateInput(input typed.Typed, ctx *Context[T]) bool
- type Pool
- type StringFuncValidator
- type StringTransform
- type StringValidator
- func (v *StringValidator[T]) Choice(choices ...string) *StringValidator[T]
- func (v *StringValidator[T]) Clone() *StringValidator[T]
- func (v *StringValidator[T]) Default(dflt any) *StringValidator[T]
- func (v *StringValidator[T]) Func(fn StringFuncValidator[T]) *StringValidator[T]
- func (v *StringValidator[T]) Length(min int, max int) *StringValidator[T]
- func (v *StringValidator[T]) Max(max int) *StringValidator[T]
- func (v *StringValidator[T]) Min(min int) *StringValidator[T]
- func (v *StringValidator[T]) NotRequired() *StringValidator[T]
- func (v *StringValidator[T]) Nullable() *StringValidator[T]
- func (v *StringValidator[T]) Pattern(pattern string, message ...string) *StringValidator[T]
- func (v *StringValidator[T]) Regexp(pattern *regexp.Regexp, message ...string) *StringValidator[T]
- func (v *StringValidator[T]) Required() *StringValidator[T]
- func (v *StringValidator[T]) Transform(tx StringTransform) *StringValidator[T]
- func (v *StringValidator[T]) Validate(raw any, ctx *Context[T]) any
- type UUIDValidator
- type Validator
Constants ¶
This section is empty.
Variables ¶
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]
Types ¶
type AnyFuncValidator ¶ added in v0.0.7
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]
type ArrayFuncValidator ¶ added in v0.0.7
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 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]
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 (*Context[T]) ArrayIndex ¶ added in v0.0.7
Sets the index array for the current array
func (*Context[T]) CurrentObject ¶ added in v0.0.7
func (*Context[T]) EndArray ¶ added in v0.0.7
func (c *Context[T]) EndArray()
Removes an array from the stack
func (*Context[T]) ErrorCount ¶ added in v0.0.7
func (*Context[T]) InvalidField ¶ added in v0.0.7
func (*Context[T]) InvalidWithField ¶ added in v0.0.7
func (*Context[T]) ResumeArray ¶ added in v0.0.7
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 (*Context[T]) SuspendArray ¶ added in v0.0.7
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
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 SimpleField ¶ added in v0.0.7
type FloatFuncValidator ¶ added in v0.0.7
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]
type IntFuncValidator ¶ added in v0.0.7
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]
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 InvalidFloatRange ¶ added in v0.0.5
func InvalidStringChoice ¶ added in v0.0.5
func InvalidStringLength ¶
func InvalidStringPattern ¶
type InvalidField ¶
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]
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 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 StringTransform ¶ added in v0.0.7
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]
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]