validator

package
v0.0.0-...-9e5d004 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2023 License: MIT Imports: 3 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	BetterGUIDRX = regexp.MustCompile("^[a-zA-Z0-9_-]{20}$")
	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])?)*$")
	UsernameRX   = regexp.MustCompile("^[A-Za-z0-9][A-Za-z0-9_-]{3,30}[A-Za-z0-9]")
)

Functions

func Matches

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

Matches checks if the given value satisfies the given regex rx.

func PermittedValue

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

PermittedValue checks that the comparable value of type T is one of the provided permittedValues.

func Unique

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

Unique checks that the given slice of comparable values of type T contains only unique values.

func ValidateEmail

func ValidateEmail(v *Validator, email string)

ValidateEmail performs standard validation checks against the provided email address and populates any errors into v.

func ValidateStrLenByte

func ValidateStrLenByte(v *Validator, value, key string, min, max int)

ValidateStrLenByte validates that the given string is between min and max bytes long. Any errors are saved into the given key in the given Validator's Error map.

func ValidateStrLenRune

func ValidateStrLenRune(v *Validator, value, key string, min, max int)

ValidateStrLenRune validates that the given string is between min and max runes (characters) long. Any errors are saved into the given key in the given Validator's Error map.

func ValidateURL

func ValidateURL(v *Validator, value, key string)

ValidateURL validates that the given string can be parsed as a URL.

func ValidateURLHTTP

func ValidateURLHTTP(v *Validator, value, key string)

ValidateURLHTTP validates that the given string can be parsed as a URL and that the scheme is an HTTP one.

Types

type Validator

type Validator struct {
	Errors map[string]string
}

A Validator is simply a map from field names to error messages.

func New

func New() *Validator

New returns a pointer to a new, empty validator struct.

func (*Validator) AddError

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

AddError adds a single, new validation error to the Errors map. If an error with the same key already exists, then this is a no-op.

func (*Validator) Check

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

Check will add a new error to the Errors map if ok evaluates to false. Any validation function or boolean operation can be evaluated for the ok parameter.

func (*Validator) Valid

func (v *Validator) Valid() bool

Valid determines if any errors have been picked up by the Validator.

Jump to

Keyboard shortcuts

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