validator

package
v0.0.0-...-3f89699 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: MIT Imports: 6 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])?)*$")
)

Functions

func Empty

func Empty(value string) bool

Empty checks if a string is empty.

func EndsWith

func EndsWith(value, suffix string) bool

EndsWith check whether a string ends with a particular suffix.

func In

func In[T comparable](value T, list ...T) bool

In checks if the value is present in a given list of arguments.

func IsNumber

func IsNumber(value string) bool

IsNumber checks if the given string is all numbers.

func Matches

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

Matches will match a string with a regular expression.

func Max

func Max[T cmp.Ordered](value T, max T) bool

Max checks if a value is equal or lesser than a maximum. For length, use MaxLength instead.

func MaxLength

func MaxLength(value string, max int) bool

MaxLength checks if a string's utf8 length is equal or lesser the given maximum.

func Min

func Min[T cmp.Ordered](value T, min T) bool

Min checks if a value is equal or bigger than a minimum. For length, use MinLength instead.

func MinLength

func MinLength(value string, min int) bool

MinLength checks if a string's utf8 length is equal or greater the given minimum.

func NotEmpty

func NotEmpty(value string) bool

NotEmpty checks if the given value is not empty.

func Range

func Range[T cmp.Ordered](value T, min, max T) bool

Range checks if a value is inside a number range, inclusive. For length, use RangeLength instead.

func RangeLength

func RangeLength(value string, min, max int) bool

RangeLength checks if a string's utf8 length is inside the given range, inclusive.

func StartsWith

func StartsWith(value, prefix string) bool

StartsWith check whether a string starts with a particular prefix.

func Unique

func Unique[T cmp.Ordered](values []T) bool

Unique checks if all elements of a given slice are unique.

Types

type Case

type Case struct {
	Cond bool
	Msg  string
}

Case is a test-case, consisting of two parts. Cond will call a function that returns a boolean. if it returns false, Msg will be added to the Validator.Errors.

type Validator

type Validator struct {
	Errors map[string]string `json:"errors"`
}

Validator will check for cases by Check method and will return a boolean with Valid method. If a validation error happens, the Msg will be stored inside the Errors map.

func New

func New() *Validator

New will return an instance of Validator.

func (*Validator) Check

func (v *Validator) Check(key string, cases ...Case)

Check accepts name of the field as the first argument, following by an arbitrary number of validation Case.

func (*Validator) Valid

func (v *Validator) Valid() bool

Valid returns a boolean indicating whether validation was successful or not.

Jump to

Keyboard shortcuts

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