Documentation
¶
Overview ¶
Package validate provides validation and parsing for HTTP request data.
Index ¶
- func IsValidationError(err error) bool
- func JSONBodyInto(r *http.Request, dest any) error
- func JSONBytesInto(data []byte, dest any) error
- func JSONStrInto(data string, dest any) error
- func URLSearchParamsInto(r *http.Request, dest any) error
- type AnyChecker
- func (c *AnyChecker) Email() *AnyChecker
- func (c *AnyChecker) EndsWith(suffix string) *AnyChecker
- func (c *AnyChecker) Error() error
- func (c *AnyChecker) If(condition bool, f func(*AnyChecker) *AnyChecker) *AnyChecker
- func (c *AnyChecker) In(permitted any) *AnyChecker
- func (c *AnyChecker) InEnum(enum any) *AnyChecker
- func (c *AnyChecker) Max(max float64) *AnyChecker
- func (c *AnyChecker) Min(min float64) *AnyChecker
- func (c *AnyChecker) NotIn(prohibited any) *AnyChecker
- func (c *AnyChecker) Optional() *AnyChecker
- func (c *AnyChecker) PermittedChars(allowed string) *AnyChecker
- func (c *AnyChecker) RangeExclusive(min, max float64) *AnyChecker
- func (c *AnyChecker) RangeInclusive(min, max float64) *AnyChecker
- func (c *AnyChecker) Regex(pattern *regexp.Regexp) *AnyChecker
- func (c *AnyChecker) Required() *AnyChecker
- func (c *AnyChecker) StartsWith(prefix string) *AnyChecker
- func (c *AnyChecker) URL() *AnyChecker
- type ObjectChecker
- func (oc *ObjectChecker) Error() error
- func (oc *ObjectChecker) MutuallyExclusive(label string, fields ...string) *ObjectChecker
- func (oc *ObjectChecker) MutuallyRequired(label string, fields ...string) *ObjectChecker
- func (oc *ObjectChecker) Optional(field string) *AnyChecker
- func (oc *ObjectChecker) Required(field string) *AnyChecker
- type ValidationError
- type Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsValidationError ¶
IsValidationError reports whether err is or wraps a ValidationError.
func JSONBodyInto ¶
JSONBodyInto decodes an HTTP request body into a struct and validates it.
func JSONBytesInto ¶
JSONBytesInto decodes JSON bytes into a struct and validates it.
func JSONStrInto ¶
JSONStrInto decodes a JSON string into a struct and validates it.
Types ¶
type AnyChecker ¶
type AnyChecker struct {
// contains filtered or unexported fields
}
AnyChecker validates a single value.
func (*AnyChecker) Email ¶
func (c *AnyChecker) Email() *AnyChecker
Email validates the string is a valid email address.
func (*AnyChecker) EndsWith ¶
func (c *AnyChecker) EndsWith(suffix string) *AnyChecker
EndsWith validates the string ends with suffix.
func (*AnyChecker) Error ¶
func (c *AnyChecker) Error() error
Error returns a ValidationError if any checks failed.
func (*AnyChecker) If ¶
func (c *AnyChecker) If( condition bool, f func(*AnyChecker) *AnyChecker, ) *AnyChecker
If applies f only when condition is true.
func (*AnyChecker) In ¶
func (c *AnyChecker) In(permitted any) *AnyChecker
In validates the value is in the permitted slice.
func (*AnyChecker) InEnum ¶
func (c *AnyChecker) InEnum(enum any) *AnyChecker
InEnum validates the value is one of the exported values in an enum struct.
func (*AnyChecker) Max ¶
func (c *AnyChecker) Max(max float64) *AnyChecker
Max validates the numeric value or length is <= max.
func (*AnyChecker) Min ¶
func (c *AnyChecker) Min(min float64) *AnyChecker
Min validates the numeric value or length is >= min.
func (*AnyChecker) NotIn ¶
func (c *AnyChecker) NotIn(prohibited any) *AnyChecker
NotIn validates the value is not in the prohibited slice.
func (*AnyChecker) Optional ¶
func (c *AnyChecker) Optional() *AnyChecker
Optional marks the value as optional and triggers validation only if present.
func (*AnyChecker) PermittedChars ¶
func (c *AnyChecker) PermittedChars(allowed string) *AnyChecker
PermittedChars ensures the string contains only characters in allowed.
func (*AnyChecker) RangeExclusive ¶
func (c *AnyChecker) RangeExclusive(min, max float64) *AnyChecker
RangeExclusive validates the numeric value or length is in (min, max).
func (*AnyChecker) RangeInclusive ¶
func (c *AnyChecker) RangeInclusive(min, max float64) *AnyChecker
RangeInclusive validates the numeric value or length is in [min, max].
func (*AnyChecker) Regex ¶
func (c *AnyChecker) Regex(pattern *regexp.Regexp) *AnyChecker
Regex validates the string matches the pattern.
func (*AnyChecker) Required ¶
func (c *AnyChecker) Required() *AnyChecker
Required marks the value as required and triggers validation.
func (*AnyChecker) StartsWith ¶
func (c *AnyChecker) StartsWith(prefix string) *AnyChecker
StartsWith validates the string starts with prefix.
func (*AnyChecker) URL ¶
func (c *AnyChecker) URL() *AnyChecker
URL validates the string is a valid URL.
type ObjectChecker ¶
type ObjectChecker struct {
AnyChecker
ChildCheckers []*AnyChecker
}
ObjectChecker validates structs and string-keyed maps with field-level checks.
func Object ¶
func Object(object any) *ObjectChecker
Object creates a checker for a struct or string-keyed map.
func (*ObjectChecker) Error ¶
func (oc *ObjectChecker) Error() error
Error returns a ValidationError combining object-level and field-level errors.
func (*ObjectChecker) MutuallyExclusive ¶
func (oc *ObjectChecker) MutuallyExclusive( label string, fields ...string, ) *ObjectChecker
MutuallyExclusive ensures at most one of the named fields is set.
func (*ObjectChecker) MutuallyRequired ¶
func (oc *ObjectChecker) MutuallyRequired( label string, fields ...string, ) *ObjectChecker
MutuallyRequired ensures all named fields are set if any is set.
func (*ObjectChecker) Optional ¶
func (oc *ObjectChecker) Optional(field string) *AnyChecker
Optional validates a field only if it is present.
func (*ObjectChecker) Required ¶
func (oc *ObjectChecker) Required(field string) *AnyChecker
Required validates that a field is present and valid.
type ValidationError ¶
type ValidationError struct{ Err error }
ValidationError wraps validation failures.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
func (*ValidationError) Unwrap ¶
func (e *ValidationError) Unwrap() error