Documentation
¶
Index ¶
- func SetDB(fn func() *lucid.DB)
- func ValidateRequestJSON(body io.Reader, v any) error
- func ValidateStruct(s any) error
- type ArrayRule
- type Authorizer
- type BaseFormRequest
- type BoolRule
- type DateRule
- func (r *DateRule) After(t time.Time) *DateRule
- func (r *DateRule) AfterOrEqual(t time.Time) *DateRule
- func (r *DateRule) Before(t time.Time) *DateRule
- func (r *DateRule) BeforeOrEqual(t time.Time) *DateRule
- func (r *DateRule) DateOnly() *DateRule
- func (r *DateRule) IsRequired() bool
- func (r *DateRule) Layout(layout string) *DateRule
- func (r *DateRule) Required() *DateRule
- func (r *DateRule) TypeScriptType() string
- type ExistsOpts
- type FileRule
- func (r *FileRule) Extensions(exts ...string) *FileRule
- func (r *FileRule) Image() *FileRule
- func (r *FileRule) IsRequired() bool
- func (r *FileRule) MaxSize(bytes int64) *FileRule
- func (r *FileRule) MaxSizeMB(mb int) *FileRule
- func (r *FileRule) MimeTypes(types ...string) *FileRule
- func (r *FileRule) Required() *FileRule
- func (r *FileRule) TypeScriptType() string
- type FormRequest
- type MapRule
- type MessageProvider
- type NumberRule
- func (r *NumberRule) Between(a, b float64) *NumberRule
- func (r *NumberRule) IsRequired() bool
- func (r *NumberRule) Max(n float64) *NumberRule
- func (r *NumberRule) Min(n float64) *NumberRule
- func (r *NumberRule) Positive() *NumberRule
- func (r *NumberRule) Required() *NumberRule
- func (r *NumberRule) TypeScriptType() string
- type Preparer
- type Rule
- type Schema
- type SchemaProvider
- type StringRule
- func (r *StringRule) Alpha() *StringRule
- func (r *StringRule) AlphaNum() *StringRule
- func (r *StringRule) Confirmed() *StringRule
- func (r *StringRule) Email() *StringRule
- func (r *StringRule) Exists(opts ExistsOpts) *StringRule
- func (r *StringRule) In(values ...string) *StringRule
- func (r *StringRule) IsRequired() bool
- func (r *StringRule) Max(n int) *StringRule
- func (r *StringRule) Min(n int) *StringRule
- func (r *StringRule) Regex(pattern string) *StringRule
- func (r *StringRule) Required() *StringRule
- func (r *StringRule) Trim() *StringRule
- func (r *StringRule) TypeScriptType() string
- func (r *StringRule) URL() *StringRule
- func (r *StringRule) Unique(opts UniqueOpts) *StringRule
- type UIntRule
- type UniqueOpts
- type ValidationErrors
- type WhenRule
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetDB ¶
SetDB sets the database provider for unique/exists rules. Call this in your app bootstrap: validation.SetDB(func() *lucid.DB { return database.DB })
func ValidateRequestJSON ¶
ValidateRequestJSON decodes JSON from body into v. No validation. Prefer BindAndValidateSchema or BindAndValidate instead.
func ValidateStruct ¶
ValidateStruct validates a struct that implements SchemaProvider using its Rules() schema. Returns nil if valid, or a ValidationErrors (which also implements error) when validation fails.
Usage:
v := &validators.Todo{Title: strings.TrimSpace(input)}
if err := validation.ValidateStruct(v); err != nil {
// err is a ValidationErrors — render form with errors
}
Types ¶
type ArrayRule ¶
type ArrayRule struct {
// contains filtered or unexported fields
}
ArrayRule validates slice/array fields.
func (*ArrayRule) Each ¶
Each validates each element with the given rule (e.g., Array().Each(String().Email())).
func (*ArrayRule) IsRequired ¶
IsRequired reports whether this rule requires a non-empty slice.
func (*ArrayRule) TypeScriptType ¶
TypeScriptType returns the TypeScript type string for this rule.
type Authorizer ¶
Authorizer is implemented by request types that perform authorization.
type BaseFormRequest ¶
type BaseFormRequest[T any] struct{}
BaseFormRequest provides a no-op Authorize implementation. Embed it in your request type to only implement Payload().
type BoolRule ¶
type BoolRule struct{}
BoolRule validates boolean fields.
func (*BoolRule) IsRequired ¶
IsRequired always returns false for BoolRule (presence is validated by type, not value).
func (*BoolRule) TypeScriptType ¶
TypeScriptType returns the TypeScript type string for this rule.
type DateRule ¶
type DateRule struct {
// contains filtered or unexported fields
}
DateRule validates string or time.Time fields as dates.
func (*DateRule) AfterOrEqual ¶
AfterOrEqual validates the date is after or equal to the given time.
func (*DateRule) BeforeOrEqual ¶
BeforeOrEqual validates the date is before or equal to the given time.
func (*DateRule) IsRequired ¶
IsRequired reports whether this rule requires a non-zero date.
func (*DateRule) TypeScriptType ¶
TypeScriptType returns the TypeScript type string for this rule (dates are strings in JSON).
type ExistsOpts ¶
type ExistsOpts struct {
// Table is the database table to check against.
Table string
// Column overrides the column name (defaults to the field name).
Column string
// Filter adds extra conditions to the query.
Filter func(db *lucid.DB, value string, field string) *lucid.DB
}
ExistsOpts configures the exists database rule.
Example:
validation.String().Required().Exists(validation.ExistsOpts{
Table: "categories",
Column: "slug",
})
type FileRule ¶
type FileRule struct {
// contains filtered or unexported fields
}
FileRule validates *multipart.FileHeader fields.
func (*FileRule) Extensions ¶
Extensions restricts allowed file extensions (without dot).
func (*FileRule) IsRequired ¶
IsRequired reports whether this rule requires a file to be present.
func (*FileRule) TypeScriptType ¶
TypeScriptType returns the TypeScript type string for this rule.
type FormRequest ¶
type FormRequest[T any] interface { // Payload returns a pointer to the payload struct with validator tags. Payload() *T // Authorize returns an error if the request is not authorized. Authorize(c *reqctx.Context) error }
FormRequest defines a JSON form request with validation + authorization. T is the payload type that carries validator tags (legacy) or is validated via a typed Schema using BindAndValidateSchema.
Example:
type LoginPayload struct {
Email string `json:"email" validate:"required,email"`
Password string `json:"password" validate:"required"`
}
type LoginRequest struct {
validation.BaseFormRequest[LoginPayload]
}
func (r *LoginRequest) Payload() *LoginPayload { return &LoginPayload{} }
func (r *LoginRequest) Authorize(c *http.Context) error {
return nil // or return an error to deny
}
// In handler:
// req := &LoginRequest{}
// payload, ve, err := validation.BindAndValidate(c, req)
// if ve != nil { return c.JSON(422, ve.ToMap()) }
// if err != nil { return err } // auth failure or other error
// use payload.Email, payload.Password
type MapRule ¶
type MapRule struct {
// contains filtered or unexported fields
}
MapRule validates map[string]any fields.
func (*MapRule) IsRequired ¶
IsRequired reports whether this rule requires a non-empty map.
func (*MapRule) TypeScriptType ¶
TypeScriptType returns the TypeScript type string for this rule.
type MessageProvider ¶
MessageProvider is implemented by request types that provide custom messages.
type NumberRule ¶
type NumberRule struct {
// contains filtered or unexported fields
}
NumberRule validates numeric fields (int, uint, float).
func (*NumberRule) Between ¶
func (r *NumberRule) Between(a, b float64) *NumberRule
func (*NumberRule) IsRequired ¶
func (r *NumberRule) IsRequired() bool
IsRequired reports whether this rule requires a non-zero value.
func (*NumberRule) Max ¶
func (r *NumberRule) Max(n float64) *NumberRule
func (*NumberRule) Min ¶
func (r *NumberRule) Min(n float64) *NumberRule
func (*NumberRule) Positive ¶
func (r *NumberRule) Positive() *NumberRule
func (*NumberRule) Required ¶
func (r *NumberRule) Required() *NumberRule
func (*NumberRule) TypeScriptType ¶
func (r *NumberRule) TypeScriptType() string
TypeScriptType returns the TypeScript type string for this rule.
type Preparer ¶
type Preparer interface {
Prepare()
}
Preparer is implemented by request types that need pre-validation sanitization.
type Rule ¶
type Rule interface {
// contains filtered or unexported methods
}
Rule is implemented by all typed rules.
type Schema ¶
Schema defines typed validation rules for a request payload. Keys are field names (preferring json tag names).
type SchemaProvider ¶
type SchemaProvider interface {
Rules() Schema
}
SchemaProvider is implemented by request types that provide typed rules.
type StringRule ¶
type StringRule struct {
// contains filtered or unexported fields
}
StringRule validates string fields with a chainable, VineJS-style API.
func (*StringRule) Alpha ¶
func (r *StringRule) Alpha() *StringRule
Alpha validates the field contains only letters.
func (*StringRule) AlphaNum ¶
func (r *StringRule) AlphaNum() *StringRule
AlphaNum validates the field contains only letters and digits.
func (*StringRule) Confirmed ¶
func (r *StringRule) Confirmed() *StringRule
Confirmed validates that a matching {field}_confirmation field exists and has the same value. The confirmation field is looked up in the parent struct.
func (*StringRule) Email ¶
func (r *StringRule) Email() *StringRule
Email validates the field as an email address.
func (*StringRule) Exists ¶
func (r *StringRule) Exists(opts ExistsOpts) *StringRule
Exists validates that the value exists in the database. See ExistsOpts.
func (*StringRule) In ¶
func (r *StringRule) In(values ...string) *StringRule
In validates the field value is one of the allowed values.
func (*StringRule) IsRequired ¶
func (r *StringRule) IsRequired() bool
IsRequired reports whether this rule requires a non-empty value.
func (*StringRule) Regex ¶
func (r *StringRule) Regex(pattern string) *StringRule
Regex validates the field matches the given pattern.
func (*StringRule) Required ¶
func (r *StringRule) Required() *StringRule
Required marks the field as required (non-empty).
func (*StringRule) Trim ¶
func (r *StringRule) Trim() *StringRule
Trim trims whitespace before validation.
func (*StringRule) TypeScriptType ¶
func (r *StringRule) TypeScriptType() string
TypeScriptType returns the TypeScript type string for this rule.
func (*StringRule) URL ¶
func (r *StringRule) URL() *StringRule
URL validates the field as an absolute URL.
func (*StringRule) Unique ¶
func (r *StringRule) Unique(opts UniqueOpts) *StringRule
Unique validates uniqueness in the database. See UniqueOpts.
type UIntRule ¶
type UIntRule struct {
// contains filtered or unexported fields
}
UIntRule validates unsigned integer fields.
func (*UIntRule) IsRequired ¶
IsRequired reports whether this rule requires a non-zero value.
func (*UIntRule) TypeScriptType ¶
TypeScriptType returns the TypeScript type string for this rule.
type UniqueOpts ¶
type UniqueOpts struct {
// Table is the database table to check against.
Table string
// Column overrides the column name (defaults to the field name).
Column string
// Filter adds extra conditions to the query (e.g. exclude current record).
Filter func(db *lucid.DB, value string, field string) *lucid.DB
}
UniqueOpts configures the unique database rule.
Example (AdonisJS VineJS style):
validation.String().Required().Email().Unique(validation.UniqueOpts{
Table: "users",
Filter: func(db *lucid.DB, value, field string) *lucid.DB {
return db.Where("id != ?", currentUserID)
},
})
type ValidationErrors ¶
ValidationErrors holds field-level validation errors for API responses.
func BindAndValidate ¶
func BindAndValidate[T any](c *reqctx.Context, fr FormRequest[T]) (*T, ValidationErrors, error)
BindAndValidate binds JSON body into the payload, validates it, and runs Authorize. It returns:
- payload (*T) on success
- ve (ValidationErrors) when validation fails (for 422 responses)
- err for non-validation errors (e.g. JSON decode, authorization).
func BindAndValidateSchema ¶
func BindAndValidateSchema(c *reqctx.Context, req any) (ValidationErrors, error)
BindAndValidateSchema binds JSON body into req, runs Prepare (if present), validates using typed Schema rules, and then calls Authorize (if present).
func FormatValidationError ¶
func FormatValidationError(err error) ValidationErrors
FormatValidationError is kept for backward compatibility.
func (ValidationErrors) Error ¶
func (e ValidationErrors) Error() string
Error implements the error interface.
func (ValidationErrors) ToMap ¶
func (e ValidationErrors) ToMap() map[string][]string
ToMap returns the errors as map[string][]string for JSON responses.
type WhenRule ¶
type WhenRule struct {
// contains filtered or unexported fields
}
WhenRule applies a sub-rule only when a condition is met, providing conditional validation (similar to VineJS's when/otherwise pattern).
Usage:
schema := validation.Schema{
"role": validation.String().Required(),
"company_id": validation.When("role", "business", validation.String().Required()),
"bio": validation.WhenFn(func(data map[string]any) bool {
return data["role"] == "creator"
}, validation.String().Min(10)),
}
func When ¶
When returns a conditional rule: apply the given rule only when the specified field equals the expected value.
validation.When("type", "premium", validation.String().Required().Min(10))