valid

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: BSD-3-Clause Imports: 6 Imported by: 0

README

valid

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Empty

func Empty(value string) bool

Empty checks if the given string is empty.

func EndsWith

func EndsWith(value, suffix string) bool

StartsWith checks if the given string ends with the given suffix.

func In

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

In checks if the given value is contained in the 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 checks if the given string matches the given regular expression.

func Max

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

Max checks if the given value is equal or less than the given max.

func MaxLength

func MaxLength(value string, max int) bool

MaxLength checks if the given string's UTF8 length is equal or less than the given max.

func Min

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

Min checks if the given value is equal or greater than the given min.

func MinLength

func MinLength(value string, min int) bool

MinLength checks if the given string's UTF8 length is equal or greater than the given min.

func NotEmpty

func NotEmpty(value string) bool

NotEmpty checks if the given string is not empty.

func Range

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

Range checks if the given value is inside the given min and max.

func RangeLength

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

RangeLength checks if the given string's UTF8 length is within the given range.

func StartsWith

func StartsWith(value, prefix string) bool

StartsWith checks if the given string starts with the given prefix.

func Unique

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

Unique checks if the given slice of values are all unique.

Types

type Case

type Case struct {
	Cond bool
	Msg  string
}

Case represents a validation test case. Contains a condition that is a the boolean result of the check, and an error message.

type Error

type Error map[string][]string

Error is a map of validation error messages.

func (Error) Error

func (e Error) Error() string

Error returns a formatted string of all the validatin error messages.

type Validator

type Validator struct {
	Errors Error `json:"errors"`
}

Validator performs a number of validation checks and determines if eveything is valid or not. Contains the map of error messages.

func New

func New() *Validator

New returns an instance of Validator.

func (*Validator) Check

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

Check goes through the validation test cases and adds any validation errors for failing cases.

func (*Validator) Valid

func (v *Validator) Valid() bool

Valid returns if the validation has passed or not.

Jump to

Keyboard shortcuts

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