validation

package
v0.0.0-20260608 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAttributeName

func GetAttributeName(field string) string

GetAttributeName returns the human-readable name for a field.

func SetAttributeName

func SetAttributeName(field, name string)

SetAttributeName registers a human-readable name for a field.

func SetAttributeNames

func SetAttributeNames(names map[string]string)

SetAttributeNames registers multiple field names at once.

func ValidateSometimes

func ValidateSometimes(data map[string]any, rules map[string][]string, conditions ...struct {
	Field   string
	When    SometimesCondition
	RuleStr string
}) map[string][]error

ValidateSometimes validates with conditional (sometimes) rules. Usage: ValidateSometimes(data, rules, Sometimes("role", "equals", "admin", "bio", "required"))

func ValidateWithHooks

func ValidateWithHooks(data map[string]any, rules map[string][]string, db *sql.DB, before BeforeValidationFunc, after AfterValidationFunc) map[string][]error

ValidateWithHooks executes validation with optional lifecycle hooks.

func ValidateWithRules

func ValidateWithRules(data map[string]any, fieldRules map[string][]Rule) map[string][]error

ValidateWithRules validates using Rule objects instead of string rules.

Types

type AcceptedRule

type AcceptedRule struct{}

AcceptedRule validates that a field is "yes", "on", 1, or true.

func (*AcceptedRule) Message

func (r *AcceptedRule) Message(field string, parameters []string) string

func (*AcceptedRule) Passes

func (r *AcceptedRule) Passes(field string, value any, parameters []string) bool

type ActiveURLRule

type ActiveURLRule struct{}

ActiveURLRule validates that a field has a valid A or AAAA record.

func (*ActiveURLRule) Message

func (r *ActiveURLRule) Message(field string, parameters []string) string

func (*ActiveURLRule) Passes

func (r *ActiveURLRule) Passes(field string, value any, parameters []string) bool

type AfterDateRule

type AfterDateRule struct{}

AfterDateRule validates that a date is after another field.

func (*AfterDateRule) Message

func (r *AfterDateRule) Message(field string, parameters []string) string

func (*AfterDateRule) Passes

func (r *AfterDateRule) Passes(field string, value any, parameters []string) bool

type AfterValidationFunc

type AfterValidationFunc func(data map[string]any) error

AfterValidationFunc is a callback that runs after validation passes.

type BeforeDateRule

type BeforeDateRule struct{}

BeforeDateRule validates that a date is before another field.

func (*BeforeDateRule) Message

func (r *BeforeDateRule) Message(field string, parameters []string) string

func (*BeforeDateRule) Passes

func (r *BeforeDateRule) Passes(field string, value any, parameters []string) bool

type BeforeValidationFunc

type BeforeValidationFunc func(data map[string]any) error

BeforeValidationFunc is a callback that runs before validation.

type BooleanRule

type BooleanRule struct{}

BooleanRule validates boolean values.

func (*BooleanRule) RuleName

func (r *BooleanRule) RuleName() string

func (*BooleanRule) Validate

func (r *BooleanRule) Validate(value any) error

type ConfirmedRule

type ConfirmedRule struct{}

ConfirmedRule validates that two fields match.

func (*ConfirmedRule) Message

func (r *ConfirmedRule) Message(field string, parameters []string) string

func (*ConfirmedRule) Passes

func (r *ConfirmedRule) Passes(field string, value any, parameters []string) bool

type CurrentPasswordRule

type CurrentPasswordRule struct{}

CurrentPasswordRule validates that the field matches the authenticated user's password.

func (*CurrentPasswordRule) Message

func (r *CurrentPasswordRule) Message(field string, parameters []string) string

func (*CurrentPasswordRule) Passes

func (r *CurrentPasswordRule) Passes(field string, value any, parameters []string) bool

type CustomExistsRule

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

CustomExistsRule validates that a field exists in the database.

func NewCustomExistsRule

func NewCustomExistsRule(table, column string) *CustomExistsRule

NewCustomExistsRule creates a new exists rule.

func (*CustomExistsRule) Message

func (r *CustomExistsRule) Message(field string, parameters []string) string

func (*CustomExistsRule) Passes

func (r *CustomExistsRule) Passes(field string, value any, parameters []string) bool

type CustomRule

type CustomRule interface {
	Passes(field string, value any, parameters []string) bool
	Message(field string, parameters []string) string
}

CustomRule defines a custom validation rule interface (Laravel InvokableRule equivalent).

type CustomUniqueRule

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

CustomUniqueRule validates that a field is unique in the database.

func NewCustomUniqueRule

func NewCustomUniqueRule(table, column string) *CustomUniqueRule

NewCustomUniqueRule creates a new unique rule.

func (*CustomUniqueRule) Message

func (r *CustomUniqueRule) Message(field string, parameters []string) string

func (*CustomUniqueRule) Passes

func (r *CustomUniqueRule) Passes(field string, value any, parameters []string) bool

type DeclinedRule

type DeclinedRule struct{}

DeclinedRule validates that a field is "no", "off", 0, or false.

func (*DeclinedRule) Message

func (r *DeclinedRule) Message(field string, parameters []string) string

func (*DeclinedRule) Passes

func (r *DeclinedRule) Passes(field string, value any, parameters []string) bool

type DifferentRule

type DifferentRule struct{}

DifferentRule validates that two fields have different values.

func (*DifferentRule) Message

func (r *DifferentRule) Message(field string, parameters []string) string

func (*DifferentRule) Passes

func (r *DifferentRule) Passes(field string, value any, parameters []string) bool

type DistinctRule

type DistinctRule struct{}

DistinctRule validates that all array values are unique.

func (*DistinctRule) Message

func (r *DistinctRule) Message(field string, parameters []string) string

func (*DistinctRule) Passes

func (r *DistinctRule) Passes(field string, value any, parameters []string) bool

type DoesntEndWithRule

type DoesntEndWithRule struct{}

DoesntEndWithRule validates that a field does not end with a specific value.

func (*DoesntEndWithRule) Message

func (r *DoesntEndWithRule) Message(field string, parameters []string) string

func (*DoesntEndWithRule) Passes

func (r *DoesntEndWithRule) Passes(field string, value any, parameters []string) bool

type DoesntStartWithRule

type DoesntStartWithRule struct{}

DoesntStartWithRule validates that a field does not start with a specific value.

func (*DoesntStartWithRule) Message

func (r *DoesntStartWithRule) Message(field string, parameters []string) string

func (*DoesntStartWithRule) Passes

func (r *DoesntStartWithRule) Passes(field string, value any, parameters []string) bool

type EmailRule

type EmailRule struct{}

EmailRule validates email format.

func (*EmailRule) RuleName

func (r *EmailRule) RuleName() string

func (*EmailRule) Validate

func (r *EmailRule) Validate(value any) error

type EnumRule

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

EnumRule validates that a field is a valid enum value.

func NewEnumRule

func NewEnumRule(enumType reflect.Type) *EnumRule

NewEnumRule creates a new enum rule.

func (*EnumRule) Message

func (r *EnumRule) Message(field string, parameters []string) string

func (*EnumRule) Passes

func (r *EnumRule) Passes(field string, value any, parameters []string) bool

type ExcludeRule

type ExcludeRule struct{}

ExcludeRule always passes (used to exclude fields from validation).

func (*ExcludeRule) Message

func (r *ExcludeRule) Message(field string, parameters []string) string

func (*ExcludeRule) Passes

func (r *ExcludeRule) Passes(field string, value any, parameters []string) bool

type InRule

type InRule struct{ Allowed []string }

InRule validates that the value is in a list of allowed values.

func (*InRule) RuleName

func (r *InRule) RuleName() string

func (*InRule) Validate

func (r *InRule) Validate(value any) error

type IntegerRule

type IntegerRule struct{}

IntegerRule validates integer values.

func (*IntegerRule) RuleName

func (r *IntegerRule) RuleName() string

func (*IntegerRule) Validate

func (r *IntegerRule) Validate(value any) error

type MaxRule

type MaxRule struct{ Max float64 }

MaxRule validates maximum length (strings) or value (numbers).

func (*MaxRule) RuleName

func (r *MaxRule) RuleName() string

func (*MaxRule) Validate

func (r *MaxRule) Validate(value any) error

type MinRule

type MinRule struct{ Min float64 }

MinRule validates minimum length (strings) or value (numbers).

func (*MinRule) RuleName

func (r *MinRule) RuleName() string

func (*MinRule) Validate

func (r *MinRule) Validate(value any) error

type MissingRule

type MissingRule struct{}

MissingRule validates that a field is not present.

func (*MissingRule) Message

func (r *MissingRule) Message(field string, parameters []string) string

func (*MissingRule) Passes

func (r *MissingRule) Passes(field string, value any, parameters []string) bool

type PresentRule

type PresentRule struct{}

PresentRule validates that a field is present.

func (*PresentRule) Message

func (r *PresentRule) Message(field string, parameters []string) string

func (*PresentRule) Passes

func (r *PresentRule) Passes(field string, value any, parameters []string) bool

type ProhibitedRule

type ProhibitedRule struct{}

ProhibitedRule validates that a field is not present.

func (*ProhibitedRule) Message

func (r *ProhibitedRule) Message(field string, parameters []string) string

func (*ProhibitedRule) Passes

func (r *ProhibitedRule) Passes(field string, value any, parameters []string) bool

type RegexRule

type RegexRule struct{ Pattern string }

RegexRule validates against a regex pattern.

func (*RegexRule) RuleName

func (r *RegexRule) RuleName() string

func (*RegexRule) Validate

func (r *RegexRule) Validate(value any) error

type RequiredRule

type RequiredRule struct{}

RequiredRule validates that a value is not empty.

func (*RequiredRule) RuleName

func (r *RequiredRule) RuleName() string

func (*RequiredRule) Validate

func (r *RequiredRule) Validate(value any) error

type Rule

type Rule interface {
	Validate(value any) error
	RuleName() string
}

Rule is an interface for reusable validation rules.

type SometimesCondition

type SometimesCondition struct {
	Field    string
	Operator string // "filled", "not_filled", "equals", "not_equals"
	Value    any
}

SometimesCondition defines when a rule should be applied.

func When

func When(field, operator string, value any) SometimesCondition

When creates a SometimesCondition for use with Sometimes.

type URLRule

type URLRule struct{}

URLRule validates URL format.

func (*URLRule) RuleName

func (r *URLRule) RuleName() string

func (*URLRule) Validate

func (r *URLRule) Validate(value any) error

type UUIDRule

type UUIDRule struct{}

UUIDRule validates UUID format.

func (*UUIDRule) RuleName

func (r *UUIDRule) RuleName() string

func (*UUIDRule) Validate

func (r *UUIDRule) Validate(value any) error

type Validator

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

Validator handles struct and rule-based validation.

func NewValidator

func NewValidator(data map[string]any, rules map[string][]string) *Validator

NewValidator creates a new validator instance.

func (*Validator) Validate

func (v *Validator) Validate() map[string][]error

Validate executes the validation rules.

func (*Validator) WithDB

func (v *Validator) WithDB(db *sql.DB) *Validator

WithDB attaches a database connection for DB-aware rules.

Jump to

Keyboard shortcuts

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