Documentation
¶
Index ¶
- func ParseStruct(schema Schema, data any, target any) error
- func ParseTyped[T any](schema Schema, value any) (T, error)
- type AnySchema
- type ArraySchema
- func (s *ArraySchema) Custom(fn func([]any) error) *ArraySchema
- func (s *ArraySchema) IsOptional() bool
- func (s *ArraySchema) MaxLength(n int) *ArraySchema
- func (s *ArraySchema) MinLength(n int) *ArraySchema
- func (s *ArraySchema) Optional() *ArraySchema
- func (s *ArraySchema) Parse(value any) (any, error)
- func (s *ArraySchema) Validate(value any) []ValidationError
- type BoolSchema
- type CoerceBoolSchema
- type CoerceFloatSchema
- func (s *CoerceFloatSchema) Custom(fn func(float64) error) *CoerceFloatSchema
- func (s *CoerceFloatSchema) Default(v float64) *CoerceFloatSchema
- func (s *CoerceFloatSchema) Max(n float64) *CoerceFloatSchema
- func (s *CoerceFloatSchema) Min(n float64) *CoerceFloatSchema
- func (s *CoerceFloatSchema) Negative() *CoerceFloatSchema
- func (s *CoerceFloatSchema) Optional() *CoerceFloatSchema
- func (s *CoerceFloatSchema) Parse(value any) (any, error)
- func (s *CoerceFloatSchema) Positive() *CoerceFloatSchema
- func (s *CoerceFloatSchema) Validate(value any) []ValidationError
- type CoerceIntSchema
- func (s *CoerceIntSchema) Custom(fn func(int) error) *CoerceIntSchema
- func (s *CoerceIntSchema) Default(v int) *CoerceIntSchema
- func (s *CoerceIntSchema) Max(n int) *CoerceIntSchema
- func (s *CoerceIntSchema) Min(n int) *CoerceIntSchema
- func (s *CoerceIntSchema) Negative() *CoerceIntSchema
- func (s *CoerceIntSchema) NonZero() *CoerceIntSchema
- func (s *CoerceIntSchema) OneOf(vals ...int) *CoerceIntSchema
- func (s *CoerceIntSchema) Optional() *CoerceIntSchema
- func (s *CoerceIntSchema) Parse(value any) (any, error)
- func (s *CoerceIntSchema) Positive() *CoerceIntSchema
- func (s *CoerceIntSchema) Validate(value any) []ValidationError
- type FloatSchema
- func (s *FloatSchema) Custom(fn func(float64) error) *FloatSchema
- func (s *FloatSchema) Default(v float64) *FloatSchema
- func (s *FloatSchema) IsOptional() bool
- func (s *FloatSchema) Max(n float64) *FloatSchema
- func (s *FloatSchema) Min(n float64) *FloatSchema
- func (s *FloatSchema) Negative() *FloatSchema
- func (s *FloatSchema) Optional() *FloatSchema
- func (s *FloatSchema) Parse(value any) (any, error)
- func (s *FloatSchema) Positive() *FloatSchema
- func (s *FloatSchema) Validate(value any) []ValidationError
- type IntSchema
- func (s *IntSchema) Custom(fn func(int) error) *IntSchema
- func (s *IntSchema) Default(v int) *IntSchema
- func (s *IntSchema) IsOptional() bool
- func (s *IntSchema) Max(n int) *IntSchema
- func (s *IntSchema) Min(n int) *IntSchema
- func (s *IntSchema) Negative() *IntSchema
- func (s *IntSchema) NonZero() *IntSchema
- func (s *IntSchema) OneOf(vals ...int) *IntSchema
- func (s *IntSchema) Optional() *IntSchema
- func (s *IntSchema) Parse(value any) (any, error)
- func (s *IntSchema) Positive() *IntSchema
- func (s *IntSchema) Validate(value any) []ValidationError
- type MapSchema
- type ObjectSchema
- type Schema
- type StringSchema
- func (s *StringSchema) Contains(sub string) *StringSchema
- func (s *StringSchema) Custom(fn func(string) error) *StringSchema
- func (s *StringSchema) Default(val string) *StringSchema
- func (s *StringSchema) Email() *StringSchema
- func (s *StringSchema) HasPrefix(pre string) *StringSchema
- func (s *StringSchema) HasSuffix(suf string) *StringSchema
- func (s *StringSchema) IsOptional() bool
- func (s *StringSchema) Max(n int) *StringSchema
- func (s *StringSchema) Min(n int) *StringSchema
- func (s *StringSchema) OneOf(values ...string) *StringSchema
- func (s *StringSchema) Optional() *StringSchema
- func (s *StringSchema) Parse(value any) (any, error)
- func (s *StringSchema) Pattern(re *regexp.Regexp, msg string) *StringSchema
- func (s *StringSchema) ToLower() *StringSchema
- func (s *StringSchema) ToUpper() *StringSchema
- func (s *StringSchema) Trim() *StringSchema
- func (s *StringSchema) URL() *StringSchema
- func (s *StringSchema) UUID() *StringSchema
- func (s *StringSchema) Validate(value any) []ValidationError
- type TimeSchema
- func (s *TimeSchema) After(t time.Time) *TimeSchema
- func (s *TimeSchema) Before(t time.Time) *TimeSchema
- func (s *TimeSchema) Custom(fn func(time.Time) error) *TimeSchema
- func (s *TimeSchema) Default(v time.Time) *TimeSchema
- func (s *TimeSchema) IsOptional() bool
- func (s *TimeSchema) Layout(layouts ...string) *TimeSchema
- func (s *TimeSchema) Optional() *TimeSchema
- func (s *TimeSchema) Parse(value any) (any, error)
- func (s *TimeSchema) Validate(value any) []ValidationError
- type TransformSchema
- type UnionSchema
- type ValidationError
- type ValidationErrors
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseStruct ¶
ParseStruct validates data against a schema, then maps the validated result into a Go struct. The target must be a pointer to a struct.
Field mapping uses the "json" struct tag (or lowercase field name if no tag).
Usage:
type User struct {
Name string `json:"name"`
Email string `json:"email"`
Age int `json:"age"`
Tags []string `json:"tags"`
}
var user User
err := check.ParseStruct(schema, data, &user)
func ParseTyped ¶
ParseTyped validates a value against a schema and returns a typed result. This avoids the need to type-assert the result of Parse.
Usage:
name, err := check.ParseTyped[string](z.String().Min(3), "Alice") age, err := check.ParseTyped[int](z.Int().Positive(), 25) users, err := check.ParseTyped[[]any](z.Array(z.String()), data)
Types ¶
type AnySchema ¶
type AnySchema struct {
// contains filtered or unexported fields
}
AnySchema accepts any value.
func (*AnySchema) IsOptional ¶
func (*AnySchema) Validate ¶
func (s *AnySchema) Validate(value any) []ValidationError
type ArraySchema ¶
type ArraySchema struct {
// contains filtered or unexported fields
}
ArraySchema validates slices/arrays where each element matches an inner schema.
func NewArray ¶
func NewArray(element Schema) *ArraySchema
func (*ArraySchema) Custom ¶
func (s *ArraySchema) Custom(fn func([]any) error) *ArraySchema
func (*ArraySchema) IsOptional ¶
func (s *ArraySchema) IsOptional() bool
func (*ArraySchema) MaxLength ¶
func (s *ArraySchema) MaxLength(n int) *ArraySchema
func (*ArraySchema) MinLength ¶
func (s *ArraySchema) MinLength(n int) *ArraySchema
func (*ArraySchema) Optional ¶
func (s *ArraySchema) Optional() *ArraySchema
func (*ArraySchema) Validate ¶
func (s *ArraySchema) Validate(value any) []ValidationError
type BoolSchema ¶
type BoolSchema struct {
// contains filtered or unexported fields
}
BoolSchema validates boolean values.
func NewBool ¶
func NewBool() *BoolSchema
func (*BoolSchema) Default ¶
func (s *BoolSchema) Default(v bool) *BoolSchema
func (*BoolSchema) IsOptional ¶
func (s *BoolSchema) IsOptional() bool
func (*BoolSchema) Optional ¶
func (s *BoolSchema) Optional() *BoolSchema
func (*BoolSchema) Validate ¶
func (s *BoolSchema) Validate(value any) []ValidationError
type CoerceBoolSchema ¶
type CoerceBoolSchema struct {
BoolSchema
}
CoerceBoolSchema wraps BoolSchema but coerces strings to bool before validation.
z.CoerceBool().Parse("true") // returns true, nil
z.CoerceBool().Parse("1") // returns true, nil
z.CoerceBool().Parse("false") // returns false, nil
z.CoerceBool().Parse("0") // returns false, nil
func NewCoerceBool ¶
func NewCoerceBool() *CoerceBoolSchema
func (*CoerceBoolSchema) Default ¶
func (s *CoerceBoolSchema) Default(v bool) *CoerceBoolSchema
func (*CoerceBoolSchema) Optional ¶
func (s *CoerceBoolSchema) Optional() *CoerceBoolSchema
func (*CoerceBoolSchema) Validate ¶
func (s *CoerceBoolSchema) Validate(value any) []ValidationError
type CoerceFloatSchema ¶
type CoerceFloatSchema struct {
FloatSchema
}
CoerceFloatSchema wraps FloatSchema but coerces strings to float64 before validation.
z.CoerceFloat().Min(0).Parse("3.14") // returns 3.14, nil
func NewCoerceFloat ¶
func NewCoerceFloat() *CoerceFloatSchema
func (*CoerceFloatSchema) Custom ¶
func (s *CoerceFloatSchema) Custom(fn func(float64) error) *CoerceFloatSchema
func (*CoerceFloatSchema) Default ¶
func (s *CoerceFloatSchema) Default(v float64) *CoerceFloatSchema
func (*CoerceFloatSchema) Max ¶
func (s *CoerceFloatSchema) Max(n float64) *CoerceFloatSchema
func (*CoerceFloatSchema) Min ¶
func (s *CoerceFloatSchema) Min(n float64) *CoerceFloatSchema
func (*CoerceFloatSchema) Negative ¶
func (s *CoerceFloatSchema) Negative() *CoerceFloatSchema
func (*CoerceFloatSchema) Optional ¶
func (s *CoerceFloatSchema) Optional() *CoerceFloatSchema
func (*CoerceFloatSchema) Positive ¶
func (s *CoerceFloatSchema) Positive() *CoerceFloatSchema
func (*CoerceFloatSchema) Validate ¶
func (s *CoerceFloatSchema) Validate(value any) []ValidationError
type CoerceIntSchema ¶
type CoerceIntSchema struct {
IntSchema
}
CoerceIntSchema wraps IntSchema but coerces strings and floats to int before validation.
z.CoerceInt().Min(0).Parse("42") // returns 42, nil
func NewCoerceInt ¶
func NewCoerceInt() *CoerceIntSchema
func (*CoerceIntSchema) Custom ¶
func (s *CoerceIntSchema) Custom(fn func(int) error) *CoerceIntSchema
func (*CoerceIntSchema) Default ¶
func (s *CoerceIntSchema) Default(v int) *CoerceIntSchema
func (*CoerceIntSchema) Max ¶
func (s *CoerceIntSchema) Max(n int) *CoerceIntSchema
func (*CoerceIntSchema) Min ¶
func (s *CoerceIntSchema) Min(n int) *CoerceIntSchema
func (*CoerceIntSchema) Negative ¶
func (s *CoerceIntSchema) Negative() *CoerceIntSchema
func (*CoerceIntSchema) NonZero ¶
func (s *CoerceIntSchema) NonZero() *CoerceIntSchema
func (*CoerceIntSchema) OneOf ¶
func (s *CoerceIntSchema) OneOf(vals ...int) *CoerceIntSchema
func (*CoerceIntSchema) Optional ¶
func (s *CoerceIntSchema) Optional() *CoerceIntSchema
func (*CoerceIntSchema) Positive ¶
func (s *CoerceIntSchema) Positive() *CoerceIntSchema
func (*CoerceIntSchema) Validate ¶
func (s *CoerceIntSchema) Validate(value any) []ValidationError
type FloatSchema ¶
type FloatSchema struct {
// contains filtered or unexported fields
}
FloatSchema validates float64 values.
func NewFloat ¶
func NewFloat() *FloatSchema
func (*FloatSchema) Custom ¶
func (s *FloatSchema) Custom(fn func(float64) error) *FloatSchema
func (*FloatSchema) Default ¶
func (s *FloatSchema) Default(v float64) *FloatSchema
func (*FloatSchema) IsOptional ¶
func (s *FloatSchema) IsOptional() bool
func (*FloatSchema) Max ¶
func (s *FloatSchema) Max(n float64) *FloatSchema
func (*FloatSchema) Min ¶
func (s *FloatSchema) Min(n float64) *FloatSchema
func (*FloatSchema) Negative ¶
func (s *FloatSchema) Negative() *FloatSchema
func (*FloatSchema) Optional ¶
func (s *FloatSchema) Optional() *FloatSchema
func (*FloatSchema) Positive ¶
func (s *FloatSchema) Positive() *FloatSchema
func (*FloatSchema) Validate ¶
func (s *FloatSchema) Validate(value any) []ValidationError
type IntSchema ¶
type IntSchema struct {
// contains filtered or unexported fields
}
IntSchema validates integer values.
func (*IntSchema) IsOptional ¶
func (*IntSchema) Validate ¶
func (s *IntSchema) Validate(value any) []ValidationError
type MapSchema ¶
type MapSchema struct {
// contains filtered or unexported fields
}
MapSchema validates map[string]any where all keys match a key schema and all values match a value schema.
func (*MapSchema) IsOptional ¶
func (*MapSchema) Validate ¶
func (s *MapSchema) Validate(value any) []ValidationError
type ObjectSchema ¶
type ObjectSchema struct {
// contains filtered or unexported fields
}
ObjectSchema validates map[string]any objects with named fields.
func NewObject ¶
func NewObject(fields map[string]Schema) *ObjectSchema
func (*ObjectSchema) IsOptional ¶
func (s *ObjectSchema) IsOptional() bool
func (*ObjectSchema) Optional ¶
func (s *ObjectSchema) Optional() *ObjectSchema
func (*ObjectSchema) Strict ¶
func (s *ObjectSchema) Strict() *ObjectSchema
Strict rejects objects with keys not defined in the schema.
func (*ObjectSchema) Validate ¶
func (s *ObjectSchema) Validate(value any) []ValidationError
type Schema ¶
type Schema interface {
// Parse validates and returns the (possibly transformed) value, or an error.
Parse(value any) (any, error)
// Validate returns all validation errors (not just the first).
Validate(value any) []ValidationError
// IsOptional reports whether this schema accepts nil values.
IsOptional() bool
}
Schema is the core interface all schemas implement.
type StringSchema ¶
type StringSchema struct {
// contains filtered or unexported fields
}
StringSchema validates string values.
func (*StringSchema) Contains ¶
func (s *StringSchema) Contains(sub string) *StringSchema
func (*StringSchema) Custom ¶
func (s *StringSchema) Custom(fn func(string) error) *StringSchema
func (*StringSchema) Default ¶
func (s *StringSchema) Default(val string) *StringSchema
func (*StringSchema) Email ¶
func (s *StringSchema) Email() *StringSchema
func (*StringSchema) HasPrefix ¶
func (s *StringSchema) HasPrefix(pre string) *StringSchema
func (*StringSchema) HasSuffix ¶
func (s *StringSchema) HasSuffix(suf string) *StringSchema
func (*StringSchema) IsOptional ¶
func (s *StringSchema) IsOptional() bool
func (*StringSchema) Max ¶
func (s *StringSchema) Max(n int) *StringSchema
func (*StringSchema) Min ¶
func (s *StringSchema) Min(n int) *StringSchema
func (*StringSchema) OneOf ¶
func (s *StringSchema) OneOf(values ...string) *StringSchema
func (*StringSchema) Optional ¶
func (s *StringSchema) Optional() *StringSchema
func (*StringSchema) Pattern ¶
func (s *StringSchema) Pattern(re *regexp.Regexp, msg string) *StringSchema
func (*StringSchema) ToLower ¶
func (s *StringSchema) ToLower() *StringSchema
func (*StringSchema) ToUpper ¶
func (s *StringSchema) ToUpper() *StringSchema
func (*StringSchema) Trim ¶
func (s *StringSchema) Trim() *StringSchema
func (*StringSchema) URL ¶
func (s *StringSchema) URL() *StringSchema
func (*StringSchema) UUID ¶
func (s *StringSchema) UUID() *StringSchema
func (*StringSchema) Validate ¶
func (s *StringSchema) Validate(value any) []ValidationError
type TimeSchema ¶
type TimeSchema struct {
// contains filtered or unexported fields
}
TimeSchema validates time.Time values. It can also parse time strings using configurable layouts (defaults to RFC3339).
func NewTime ¶
func NewTime() *TimeSchema
func (*TimeSchema) After ¶
func (s *TimeSchema) After(t time.Time) *TimeSchema
After requires the time to be after the given time.
func (*TimeSchema) Before ¶
func (s *TimeSchema) Before(t time.Time) *TimeSchema
Before requires the time to be before the given time.
func (*TimeSchema) Custom ¶
func (s *TimeSchema) Custom(fn func(time.Time) error) *TimeSchema
Custom adds a custom validation function.
func (*TimeSchema) Default ¶
func (s *TimeSchema) Default(v time.Time) *TimeSchema
func (*TimeSchema) IsOptional ¶
func (s *TimeSchema) IsOptional() bool
func (*TimeSchema) Layout ¶
func (s *TimeSchema) Layout(layouts ...string) *TimeSchema
Layout sets the time layout(s) used when parsing strings. Replaces the default RFC3339. Multiple layouts are tried in order.
func (*TimeSchema) Optional ¶
func (s *TimeSchema) Optional() *TimeSchema
func (*TimeSchema) Validate ¶
func (s *TimeSchema) Validate(value any) []ValidationError
type TransformSchema ¶
type TransformSchema struct {
// contains filtered or unexported fields
}
Transform adds a generic transformation step to any schema. The transform runs after validation succeeds.
func Transform ¶
func Transform(inner Schema, fn func(any) (any, error)) *TransformSchema
Transform wraps a schema and applies a transformation after successful validation.
// Parse a string, then convert to uppercase
upper := check.Transform(check.NewString(), func(v any) (any, error) {
return strings.ToUpper(v.(string)), nil
})
func (*TransformSchema) IsOptional ¶
func (s *TransformSchema) IsOptional() bool
func (*TransformSchema) Validate ¶
func (s *TransformSchema) Validate(value any) []ValidationError
type UnionSchema ¶
type UnionSchema struct {
// contains filtered or unexported fields
}
UnionSchema validates that a value matches at least one of the given schemas.
func NewUnion ¶
func NewUnion(schemas ...Schema) *UnionSchema
func (*UnionSchema) IsOptional ¶
func (s *UnionSchema) IsOptional() bool
func (*UnionSchema) Optional ¶
func (s *UnionSchema) Optional() *UnionSchema
func (*UnionSchema) Validate ¶
func (s *UnionSchema) Validate(value any) []ValidationError
type ValidationError ¶
type ValidationError struct {
Path string // dot-separated path, e.g. "address.city"
Message string // human-readable message
Value any // the offending value
}
ValidationError represents a single validation failure.
func (ValidationError) Error ¶
func (e ValidationError) Error() string
type ValidationErrors ¶
type ValidationErrors []ValidationError
ValidationErrors is a collection of validation errors that implements error.
func (ValidationErrors) Error ¶
func (ve ValidationErrors) Error() string