Documentation
¶
Overview ¶
Package validate provides pure-function validators that return "" on success or a human-readable error message string on failure.
Index ¶
- Constants
- func Email(value string) string
- func EmailMsg(value, msg string) string
- func EmptyOr(fn func(string) string) func(string) string
- func HasDigit(value string) string
- func HasDigitMsg(value, msg string) string
- func HasLower(value string) string
- func HasLowerMsg(value, msg string) string
- func HasSpecial(value string) string
- func HasSpecialMsg(value, msg string) string
- func HasUpper(value string) string
- func HasUpperMsg(value, msg string) string
- func IntRange(value int, min, max int) string
- func IntRangeMsg(value, min, max int, msg string) string
- func MaxAge(birthDate string, maxAge int) string
- func MaxAgeMsg(birthDate string, maxAge int, msg string) string
- func MaxLength(value string, max int) string
- func MaxLengthMsg(value string, max int, msg string) string
- func MinAge(birthDate string, minAge int) string
- func MinAgeMsg(birthDate string, minAge int, msg string) string
- func MinLength(value string, min int) string
- func MinLengthMsg(value string, min int, msg string) string
- func NormalizeURL(value string) string
- func OneOf(value string, options ...string) string
- func OneOfMsg(value, msg string, options ...string) string
- func PasswordStrength(password string) string
- func PasswordStrengthMsg(password, msg string) string
- func Phone(value string) string
- func PhoneMsg(value, msg string) string
- func Register(name string, fn func(string) string)
- func Required(value string) string
- func RequiredMsg(value, msg string) string
- func Run(name, value string) string
- func RunRules(value string, rules ...Rule) string
- func URL(value string) string
- func URLMsg(value, msg string) string
- type CtxRule
- type FieldBuilder
- type FieldRenderer
- type Form
- type FormOption
- type PasswordRequirement
- type Rule
Constants ¶
const ( MsgRequired = "This field is required" MsgEmailInvalid = "Invalid email address" MsgPhoneInvalid = "Invalid phone number" MsgURLInvalid = "Invalid URL" MsgMinLength = "Too short" MsgMaxLength = "Too long" MsgOneOf = "Invalid selection" MsgIntRange = "Value out of range" MsgMinAge = "Does not meet minimum age requirement" MsgMaxAge = "Exceeds maximum age" MsgPasswordWeak = "Password is too weak" MsgHasUpper = "Must contain an uppercase letter" MsgHasLower = "Must contain a lowercase letter" MsgHasDigit = "Must contain a digit" MsgHasSpecial = "Must contain a special character" MsgValidatorNotFound = "Unknown validator" )
Exported message constants used by the built-in validators. Override individual messages by using the *Msg function variants.
Variables ¶
This section is empty.
Functions ¶
func EmptyOr ¶
EmptyOr wraps a validator function so that empty (whitespace-only) values pass validation. Use this when a field is optional but must be valid if provided.
func HasDigitMsg ¶
HasDigitMsg is like HasDigit with a custom message.
func HasLower ¶
HasLower returns "" if value contains at least one lowercase letter, or MsgHasLower.
func HasLowerMsg ¶
HasLowerMsg is like HasLower with a custom message.
func HasSpecial ¶
HasSpecial returns "" if value contains at least one special character (punctuation or symbol), or MsgHasSpecial.
func HasSpecialMsg ¶
HasSpecialMsg is like HasSpecial with a custom message.
func HasUpper ¶
HasUpper returns "" if value contains at least one uppercase letter, or MsgHasUpper.
func HasUpperMsg ¶
HasUpperMsg is like HasUpper with a custom message.
func IntRangeMsg ¶
IntRangeMsg is like IntRange with a custom message.
func MaxAge ¶
MaxAge returns "" if the person with the given birthDate (YYYY-MM-DD) is at most maxAge years old, or MsgMaxAge.
func MaxLengthMsg ¶
MaxLengthMsg is like MaxLength with a custom message.
func MinAge ¶
MinAge returns "" if the person with the given birthDate (YYYY-MM-DD) is at least minAge years old, or MsgMinAge.
func MinLengthMsg ¶
MinLengthMsg is like MinLength with a custom message.
func NormalizeURL ¶
NormalizeURL prepends "https://" when value has no scheme. Returns the original value unchanged if it is empty or already has a scheme.
func PasswordStrength ¶
PasswordStrength returns "" if all password requirements are met, or MsgPasswordWeak.
func PasswordStrengthMsg ¶
PasswordStrengthMsg is like PasswordStrength with a custom message.
func Phone ¶
Phone returns "" if value is a plausible phone number, or MsgPhoneInvalid. It accepts optional leading '+' followed by 7-15 digits.
func RequiredMsg ¶
RequiredMsg is like Required with a custom message.
func Run ¶
Run executes a registered validator by name. If the name is not found it returns MsgValidatorNotFound.
func RunRules ¶
RunRules executes rules in order, returning the first error message. Returns "" if all rules pass.
func URLMsg ¶
URLMsg is like URL with a custom message. Only http/https URLs are accepted: a validated URL frequently lands in an href/src, and schemes like javascript:, data:, and vbscript: are XSS vectors. Note "javascript://x/..." parses with both a scheme and a host, so a scheme allowlist (not just a non-empty-scheme check) is required to reject it.
Types ¶
type CtxRule ¶
CtxRule is a context-aware validation function that receives the Echo context alongside the value. Useful for cross-field comparisons (e.g. password confirmation) or checking request-scoped data.
type FieldBuilder ¶
type FieldBuilder struct {
// contains filtered or unexported fields
}
FieldBuilder defines a field's validation rules and satisfies FormOption. Use Field or FieldMsg to create one.
func Field ¶
func Field(name string, rules ...Rule) FieldBuilder
Field defines a field with rules that use each rule's default error message.
func FieldMsg ¶
func FieldMsg(name, msg string, rules ...Rule) FieldBuilder
FieldMsg defines a field where any rule failure produces the given message, regardless of which rule failed.
func (FieldBuilder) WithCtx ¶
func (fb FieldBuilder) WithCtx(rules ...CtxRule) FieldBuilder
WithCtx adds context-aware rules to the field. These run after standard rules and only if all standard rules pass.
Note: WithTrim only trims the current field's value before it is passed to rules. Values read from echo.Context inside a CtxRule (e.g. c.FormValue("other_field")) are untrimmed. Apply strings.TrimSpace manually if cross-field comparisons need consistent trimming.
func (FieldBuilder) WithRenderer ¶
func (fb FieldBuilder) WithRenderer(fn FieldRenderer) FieldBuilder
WithRenderer sets a custom renderer for this field's per-field validation. Falls back to the form-level WithOOBRenderer if not set.
type FieldRenderer ¶
FieldRenderer renders a per-field validation result. Used by ValidationHandler to produce the HTTP response (typically an OOB swap).
type Form ¶
type Form struct {
// contains filtered or unexported fields
}
Form holds field rule definitions for both full-form validation and HTMX per-field validation. Define rules once in the constructor; use Validate for full-form checks and ValidationHandler for per-field HTMX endpoints.
func NewForm ¶
func NewForm(opts ...FormOption) Form
NewForm creates a Form from the given options and field definitions.
func (Form) Validate ¶
Validate runs all field rules against form values from the Echo context. Returns a map of field name to error message, or nil if all fields pass.
Per-field short-circuit is always on: rules for a single field stop at the first failure. Per-form short-circuit is controlled by WithShortCircuit.
func (Form) ValidationHandler ¶
func (f Form) ValidationHandler(paramName string) echo.HandlerFunc
ValidationHandler returns an echo.HandlerFunc that validates a single field. The paramName argument is the Echo route parameter name that contains the field name (e.g. "field" for ":field").
Unknown fields are silently ignored — the handler returns an empty/no-error response via the renderer.
type FormOption ¶
type FormOption interface {
// contains filtered or unexported methods
}
FormOption configures a Form via NewForm.
func WithGeneralError ¶
func WithGeneralError(msg string) FormOption
WithGeneralError adds a message under the "general" key in the error map when any field fails validation. Templates display it via form.GetError(errs, "general").
func WithOOBRenderer ¶
func WithOOBRenderer(fn FieldRenderer) FormOption
WithOOBRenderer sets the default renderer used by ValidationHandler to produce per-field error responses.
func WithShortCircuit ¶
func WithShortCircuit(on bool) FormOption
WithShortCircuit stops validating after the first field that fails. Default is false — all fields are validated and all errors returned.
func WithTrim ¶
func WithTrim(on bool) FormOption
WithTrim enables automatic whitespace trimming of form values before validation.
type PasswordRequirement ¶
PasswordRequirement describes a single password rule and whether it is met.
func CheckPasswordRequirements ¶
func CheckPasswordRequirements(password string) []PasswordRequirement
CheckPasswordRequirements evaluates password against common strength rules and returns the status of each requirement.
type Rule ¶
Rule is a pure validation function: returns "" on success or an error message on failure. This is a type alias so existing validators like Required and Email satisfy it without casting.
func AgeMax ¶
AgeMax returns a rule that checks a birth date (YYYY-MM-DD) does not exceed a maximum age.
func AgeMin ¶
AgeMin returns a rule that checks a birth date (YYYY-MM-DD) meets a minimum age requirement.