validate

package
v0.85.0-pre.7 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2026 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package validate provides validation and parsing for HTTP request data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsValidationError

func IsValidationError(err error) bool

IsValidationError reports whether err is or wraps a ValidationError.

func JSONBodyInto

func JSONBodyInto(r *http.Request, dest any) error

JSONBodyInto decodes an HTTP request body into a struct and validates it.

func JSONBytesInto

func JSONBytesInto(data []byte, dest any) error

JSONBytesInto decodes JSON bytes into a struct and validates it.

func JSONStrInto

func JSONStrInto(data string, dest any) error

JSONStrInto decodes a JSON string into a struct and validates it.

func URLSearchParamsInto

func URLSearchParamsInto(r *http.Request, dest any) error

URLSearchParamsInto parses URL query parameters into a struct and validates it.

Types

type AnyChecker

type AnyChecker struct {
	// contains filtered or unexported fields
}

AnyChecker validates a single value.

func Any

func Any(label string, anything any) *AnyChecker

Any creates a checker for any 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) 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

type Validator

type Validator interface{ Validate() error }

Validator is implemented by types that can self-validate.

Jump to

Keyboard shortcuts

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