validator

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package validator provides input validation utilities with error collection

Index

Constants

This section is empty.

Variables

View Source
var (
	EmailRX = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+\\.[a-zA-Z]{2,}$")
)

Functions

func Matches

func Matches(value string, rx *regexp.Regexp) bool

Matches returns true if value matches the regex pattern Commonly used with EmailRX for email validation

func MaxChars added in v0.7.4

func MaxChars(value string, n int) bool

MaxChars returns true if value has n characters or fewer

func MinChars added in v0.7.4

func MinChars(value string, n int) bool

MinChars returns true if value has n characters or more

func NotBlank added in v0.7.4

func NotBlank(value string) bool

NotBlank returns true if value contains non-whitespace characters

func PermittedValue

func PermittedValue[T comparable](value T, permittedValues ...T) bool

PermittedValue returns true if value is in the permitted list Works with any comparable type (strings, ints, etc.)

func Unique

func Unique[T comparable](values []T) bool

Unique returns true if all slice values are distinct Works with any comparable type

Types

type Validator

type Validator struct {
	Errors map[string]string
}

Validator collects validation errors by field name

func New

func New() *Validator

New creates a Validator instance ready to collect validation errors

func (*Validator) AddError

func (v *Validator) AddError(key, message string)

AddError records a validation error for a field Existing errors for the same field are preserved (no overwrite)

func (*Validator) Check

func (v *Validator) Check(ok bool, key, message string)

Check adds an error when validation fails Example: v.Check(len(name) > 0, "name", "Name is required")

func (*Validator) Valid

func (v *Validator) Valid() bool

Valid returns true when no validation errors exist

Jump to

Keyboard shortcuts

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