validator

package
v0.0.0-...-69bde9d Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

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

Use the regexp.MustCompile() function to parse a regular expression pattern for sanity checking the format of an email address. This returns a pointer to a 'compiled' regexp.Regexp type, or panics in the event of an error. Parsing this pattern once at startup and storing the compiled *regexp.Regexp in a variable is more performant than re-parsing the pattern each time we need it.

Functions

func Matches

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

Matches() returns true if a value matches a provided compiled regular expression pattern.

func MaxChars

func MaxChars(value string, n int) bool

MaxChars() returns true if a value contains no more than n characters.

func MinChars

func MinChars(value string, n int) bool

MinChars() returns true if a value contains at least n characters.

func NotBlank

func NotBlank(value string) bool

NotBlank() returns true if a value is not an empty string.

func PermittedValue

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

PermittedValue() returns true if a value is in a list of specific permitted values.

Types

type Validator

type Validator struct {
	NonFieldErrors []string
	FieldErrors    map[string]string
}

Define a new Validator struct which contains a map of validation error messages for our form fields.

func (*Validator) AddFieldError

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

AddFieldError() adds an error message to the FieldErrors map (so long as no entry already exists for the given key).

func (*Validator) AddNonFieldError

func (v *Validator) AddNonFieldError(message string)

Create an AddNonFieldError() helper for adding error messages to the new NonFieldErrors slice.

func (*Validator) CheckField

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

CheckField() adds an error message to the FieldErrors map only if a validation check is not 'ok'.

func (*Validator) Valid

func (v *Validator) Valid() bool

Valid() returns true if the FieldErrors map doesn't contain any entries.

Jump to

Keyboard shortcuts

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