validation

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Required = Rule{
		Name:    "required",
		Message: "is required",
		Validate: func(v interface{}) bool {
			if v == nil {
				return false
			}
			if s, ok := v.(string); ok {
				return strings.TrimSpace(s) != ""
			}
			return true
		},
	}

	Email = Rule{
		Name:    "email",
		Message: "must be a valid email address",
		Validate: func(v interface{}) bool {
			s, ok := v.(string)
			if !ok {
				return false
			}
			pattern := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`
			matched, _ := regexp.MatchString(pattern, s)
			return matched
		},
	}
)

Common validation rules.

Functions

This section is empty.

Types

type Rule

type Rule struct {
	Name     string
	Message  string
	Validate func(interface{}) bool
}

Rule represents a validation rule.

func In

func In(valid ...string) Rule

func Max

func Max(n int) Rule

func MaxLength

func MaxLength(n int) Rule

func Min

func Min(n int) Rule

func MinLength

func MinLength(n int) Rule

func Pattern

func Pattern(p string, msg string) Rule

type Validator

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

Validator provides data validation.

func New

func New() *Validator

New creates a new validator.

func (*Validator) AddRule

func (v *Validator) AddRule(field string, rule Rule)

AddRule adds a validation rule for a field.

func (*Validator) Validate

func (v *Validator) Validate(data interface{}) (map[string][]string, bool)

Validate validates a struct.

Jump to

Keyboard shortcuts

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