validator

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2022 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package validator provides utilities to provide a uniform valditation experience.

This validator is adapted from Alex Edwards' "let's go further" book, see https://lets-go-further.alexedwards.net

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func In

func In(value string, list ...string) bool

In checks if value is part of list.

func Matches

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

Matches checks value against the supplied rx

func Unique

func Unique(values []string) bool

Unique checks if all values are unique

When values is an empty list it returns true.

Types

type Validator

type Validator struct {
	Errors           map[string]error
	PrefixKeyInError bool
}

func New

func New() *Validator

New returns a new empty Validator

func (*Validator) AddError

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

AddError adds an error to the validator

Per key only one error message can be added. Additional errors will silently be ignored.

When both err and message are given they are wrapped in a new Error.

Either err or message must given a non-empty value, otherwise the error is silently ignored.

func (*Validator) Check

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

Check evaluates ok and adds messages if not true.

This can be used to build easy to read list of checks.

v.Check(f.Page > 0, "page", errors.New("invalid param"), "must be greater than zero")
v.Check(f.Page <= 10_000_000, "page", nil, "must be a maximum of 10 million")

func (*Validator) ErrorOrNil

func (v *Validator) ErrorOrNil() error

ErrorOrNil wraps all the errors in a single error or returns nil

func (*Validator) Valid

func (v *Validator) Valid() bool

Valid return a boolean if errors have been encountered

Jump to

Keyboard shortcuts

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