validator

package
v0.0.0-...-e705344 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package validator provides a set of utilities for validating data, including email validation, uniqueness checks, and permitted value checks. It is taken from "Let's Go " by Alex Edwards.

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

EmailRX is a regular expression for validating email addresses.

Functions

func Matches

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

Matches checks if a string value matches a given regular expression.

func PermittedValue

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

PermittedValue checks if a value is within a set of permitted values.

func Unique

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

Unique checks if all values in a slice are unique.

Types

type Validator

type Validator struct {
	Errors map[string]string
}

Validator holds validation errors.

func New

func New() *Validator

New creates a new Validator instance.

func (*Validator) AddError

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

AddError adds an error message for a given key if it doesn't already exist.

func (*Validator) Check

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

Check adds an error message for a given key if the condition is not met.

func (*Validator) Valid

func (v *Validator) Valid() bool

Valid returns true if there are no validation errors.

Jump to

Keyboard shortcuts

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