val

package
v0.6.45 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package val provides struct field validation utilities.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidatePassword

func ValidatePassword(password string, errs []string) []string

ValidatePassword validates a password string.

Types

type Error

type Error struct {
	Fields  []error
	Message string
	Type    string
}

Error is a top-level validation error.

func NewError

func NewError(message string) *Error

NewError creates a new validation error.

func (Error) Error

func (e Error) Error() string

func (Error) Messages

func (e Error) Messages() map[string]string

Messages returns a map of field error messages.

type FieldError

type FieldError struct {
	Field   string
	Message string
}

FieldError is a per-field validation error.

func NewFieldError

func NewFieldError(field string, message string) *FieldError

NewFieldError creates a new field error.

func (FieldError) Error

func (f FieldError) Error() string

type StringValidationContext

type StringValidationContext struct {
	IsValid bool
	// contains filtered or unexported fields
}

StringValidationContext provides fluent string validation.

func CheckString

func CheckString(str string) *StringValidationContext

CheckString creates a string validation context.

func (*StringValidationContext) IsPassword

IsPassword validates password requirements.

func (*StringValidationContext) LengthIsGreaterThanOrEqualTo

func (s *StringValidationContext) LengthIsGreaterThanOrEqualTo(n int) *StringValidationContext

LengthIsGreaterThanOrEqualTo validates minimum length.

type Validator

type Validator struct {
	Value reflect.Value
	// contains filtered or unexported fields
}

Validator validates struct fields using registered validation functions.

Example
type User struct {
	Name  string
	Email string
}

v := New()
v.Check("Name").Exists()
v.Check("Email").IsEmail()

// Failing input: blank name and a malformed email.
bad := &User{Name: "", Email: "nope"}
badErrs := v.Exec(bad)
msgs := make([]string, 0, len(badErrs))
for _, err := range badErrs {
	msgs = append(msgs, err.Error())
}
// Exec iterates the registered fields in map order, so sort to keep
// the example output stable.
slices.Sort(msgs)
for _, msg := range msgs {
	fmt.Println(msg)
}

// Passing input.
good := &User{Name: "Ada", Email: "ada@example.com"}
if errs := v.Exec(good); len(errs) == 0 {
	fmt.Println("no validation errors")
}
Output:
Field cannot be blank.
Field must be an email.
no validation errors

func New

func New() *Validator

New creates a new Validator.

func (*Validator) Add

Add registers a validation function for the current field.

func (*Validator) Check

func (v *Validator) Check(field string) *Validator

Check sets the current field to be validated.

func (*Validator) Ensure

func (v *Validator) Ensure(fn ValidatorFunction) *Validator

Ensure adds a custom validation function.

func (*Validator) Exec

func (v *Validator) Exec(value any) []error

Exec runs all validation functions and returns any errors.

func (*Validator) Exists

func (v *Validator) Exists() *Validator

Exists validates that the field is not empty.

func (*Validator) IsEmail

func (v *Validator) IsEmail() *Validator

IsEmail validates that the field looks like an email address.

func (*Validator) IsPassword

func (v *Validator) IsPassword() *Validator

IsPassword validates minimum password requirements.

func (*Validator) Matches

func (v *Validator) Matches(strs ...string) *Validator

Matches validates the field matches one of the given strings.

func (*Validator) MinLength

func (v *Validator) MinLength(minLength int) *Validator

MinLength validates minimum string length.

type ValidatorFunction

type ValidatorFunction any

ValidatorFunction is a function that validates a value.

Jump to

Keyboard shortcuts

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