Documentation
¶
Overview ¶
Package validate is a dependency-free, struct-tag validator for GoWebComponents.
It is built for the framework's one-language advantage: the SAME Go struct, with the SAME `validate:"..."` tags, validates identically on the server (in an HTTP handler) and on the client (in ui.UseForm), so client and server validation can never drift. It uses only reflect/regexp/strconv/strings/fmt — no syscall, net, or filesystem — so it compiles to both `GOOS=js GOARCH=wasm` and native.
type Signup struct {
Email string `json:"email" validate:"required,email"`
Age int `json:"age" validate:"gte=13,lte=120"`
Plan string `json:"plan" validate:"oneof=free pro team"`
}
res := validate.Struct(signup)
if !res.Valid() { /* res.Fields() is map[string]string, ready for ui.FieldErrors */ }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisterRule ¶
RegisterRule adds (or replaces) a custom validation rule usable as validate:"name" or validate:"name=arg". It is safe for concurrent use. A blank name, a nil fn, or a name that collides with a built-in rule is ignored so the core rule set can never be shadowed.
validate.RegisterRule("phone", func(v any, _ string) (bool, string) {
s, _ := v.(string)
return s == "" || rePhone.MatchString(s), "must be a valid phone number"
})
Types ¶
type CrossRule ¶
type CrossRule[T any] func(parseValue T) *FieldError
CrossRule is a programmatic, struct-level validation rule for constraints that span more than one field — password==confirm, end>=start, "required if plan==team" — which per-field tags cannot express. Return nil when valid, or a *FieldError (use Fail) describing the failure.
type FieldError ¶
type FieldError struct {
Field string `json:"field"`
Rule string `json:"rule"`
Message string `json:"message"`
}
FieldError is one validation failure: the (possibly dotted) field name, the rule that failed, and a human-readable message.
func Fail ¶
func Fail(parseField, parseMessage string) *FieldError
Fail builds a *FieldError for a cross-field rule, attaching the message to field.
type Result ¶
type Result struct {
Errors []FieldError `json:"errors,omitempty"`
}
Result is the outcome of a validation pass.
func Check ¶
Check validates value's struct tags (exactly like Struct) AND the given cross-field rules, merging every failure into one Result. It keeps the single-schema guarantee for relationships between fields: the same Check call runs in ui.UseForm on the client and in the server handler.
res := validate.Check(signup,
func(s Signup) *validate.FieldError {
if s.Password != s.Confirm {
return validate.Fail("confirm", "must match password")
}
return nil
},
)
A nil rule is skipped. A rule that panics is contained (it records a failure rather than crashing the caller), mirroring custom tag rules.
func Struct ¶
Struct validates v — a struct or a (possibly nil) pointer to a struct — against the `validate` tags on its exported fields, recursing into nested struct fields with dotted field paths (e.g. "address.zip"). A nil pointer or non-struct value yields a valid (empty) result.
func (Result) Error ¶
Error implements the error interface so a Result can be returned as an error from a server handler. It renders "field: message; ..." or "" when valid.
type RuleFunc ¶
RuleFunc is a custom validation rule registered with RegisterRule. value is the field's value (type-assert it, commonly to string — use the comma-ok form, e.g. s, _ := value.(string)); arg is the tag argument after '=' (e.g. "5" in validate:"phone=5"), or "" when there is none. Return ok=false with a human-readable message to fail the field. A panic inside a RuleFunc is contained: it fails the field with a "validation rule panicked" message rather than crashing Struct.