validator

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package validator implements utility for input validation

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])?)*$")
)

Functions

func Matches

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

Matches checks if a string value matches the provided regular expression Returns true if the string matches the pattern, false otherwise Commonly used with the EmailRX variable for email validation

func PermittedValue

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

PermittedValue checks if a value exists within a list of permitted values Uses Go generics to work with any comparable type (strings, ints, etc.) Returns true if the value is found in the permitted values slice

func Unique

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

Unique checks if all values in a slice are unique (no duplicates) Uses Go generics to work with slices of any comparable type Returns true if all values are unique, false if duplicates are found

Types

type Validator

type Validator struct {
	Errors map[string]string
}

Validator holds a map of validation errors keyed by field name This allows accumulating multiple validation errors before returning them

func New

func New() *Validator

New creates and returns a new Validator instance with an empty error map

func (*Validator) AddError

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

AddError adds a validation error message for the given field key If an error already exists for this key, it will not be overwritten This prevents duplicate error messages for the same field

func (*Validator) Check

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

Check evaluates a boolean condition and adds an error if the condition is false This is a convenience method for conditional validation Example: v.Check(len(name) > 0, "name", "Name is required")

func (*Validator) Valid

func (v *Validator) Valid() bool

Valid returns true if there are no validation errors stored This is typically called after running all validations to check overall validity

Jump to

Keyboard shortcuts

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