Documentation
¶
Overview ¶
Package validx provides composable, pure-function field validators that return *errx.Error with domain VALIDATION.
No reflection, no struct tags. Callers compose validators explicitly:
if err := validx.Collect(
validx.Required("email", req.Email),
validx.Email("email", req.Email),
validx.MinLen("password", req.Password, 8),
); err != nil {
// err is *errx.MultiError with all validation failures
}
Index ¶
- Constants
- func Between(field string, value, min, max int) *errx.Error
- func BetweenTime(field string, t, min, max time.Time) *errx.Error
- func Clamp[T cmp.Ordered](field string, val *T, min, max T) *errx.Error
- func ClampTime(field string, val *time.Time, min, max time.Time) *errx.Error
- func Collect(errs ...*errx.Error) error
- func Default[T comparable](field string, val *T, def T) *errx.Error
- func DefaultOneOf[T comparable](field string, val *T, allowed []T, def T) *errx.Error
- func DefaultStr(field string, val *string, def string) *errx.Error
- func DefaultTime(field string, val *time.Time, def time.Time) *errx.Error
- func Email(field, value string) *errx.Error
- func Match(field, value, pattern string) *errx.Error
- func MaxLen(field, value string, max int) *errx.Error
- func MinLen(field, value string, min int) *errx.Error
- func OneOf(field, value string, allowed []string) *errx.Error
- func Required(field, value string) *errx.Error
- func URL(field, value string) *errx.Error
Constants ¶
const ( // CodeRequired indicates a required field was empty. CodeRequired = "REQUIRED" // CodeTooShort indicates the value is shorter than the minimum length. CodeTooShort = "TOO_SHORT" // CodeTooLong indicates the value exceeds the maximum length. CodeTooLong = "TOO_LONG" // CodeOutOfRange indicates the value is outside the allowed numeric or time range. CodeOutOfRange = "OUT_OF_RANGE" // CodeInvalidFormat indicates the value does not match the required pattern. CodeInvalidFormat = "INVALID_FORMAT" // CodeInvalidValue indicates the value is not among the allowed set. CodeInvalidValue = "INVALID_VALUE" // CodeFixed indicates the value was auto-corrected (informational, not a failure). CodeFixed = "FIXED" )
const CodeNilPointer = "NIL_POINTER"
CodeNilPointer indicates a nil pointer was passed to a fix function.
const DomainValidation = "VALIDATION"
DomainValidation is the errx domain for all validation errors.
Variables ¶
This section is empty.
Functions ¶
func BetweenTime ¶
BetweenTime checks that t is within [min, max] inclusive. Returns CodeOutOfRange if out of range, nil otherwise. This is the reject-only counterpart to ClampTime.
func Clamp ¶
Clamp ensures *val is within [min, max]. If out of range, *val is clamped to the nearest bound and an informational CodeFixed error is returned. Returns nil if *val is already within range.
validx.Clamp("port", &cfg.Port, 1024, 65535)
validx.Clamp("rate", &cfg.Rate, 0.1, 100.0)
func ClampTime ¶
ClampTime ensures *val is within [min, max]. If out of range, *val is clamped to the nearest bound and an informational CodeFixed error is returned. Returns nil if *val is already within range.
validx.ClampTime("start", &cfg.Start, earliest, latest)
func Collect ¶
Collect gathers all non-nil validation errors into a *errx.MultiError. Returns nil if all validations passed.
func Default ¶
func Default[T comparable](field string, val *T, def T) *errx.Error
Default sets *val to def if *val is the zero value for its type. Returns an informational CodeFixed error when defaulted, nil if *val was already non-zero.
validx.Default("timeout", &cfg.Timeout, 30*time.Second)
validx.Default("retries", &cfg.Retries, 3)
func DefaultOneOf ¶
func DefaultOneOf[T comparable](field string, val *T, allowed []T, def T) *errx.Error
DefaultOneOf sets *val to def if *val is not in the allowed set. Returns an informational CodeFixed error when defaulted, nil if *val was already valid.
validx.DefaultOneOf("level", &cfg.Level, []string{"debug","info","warn","error"}, "info")
func DefaultStr ¶
DefaultStr sets *val to def if *val is empty or whitespace-only. Returns an informational CodeFixed error when defaulted, nil if *val was already non-empty. Mirrors Required semantics.
validx.DefaultStr("env", &cfg.Env, "production")
func DefaultTime ¶
DefaultTime sets *val to def if *val is the zero time. Returns an informational CodeFixed error when defaulted, nil if *val was already set.
validx.DefaultTime("created_at", &cfg.CreatedAt, time.Now())
Types ¶
This section is empty.