validation

package
v0.0.0-...-e7b361f Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2018 License: MIT Imports: 5 Imported by: 1

Documentation

Overview

Package validation provides validation for values.

Index

Constants

View Source
const (
	RuleTypeEmailAddress = iota + 1
	RuleTypeMaxLength
	RuleTypeMinLength
	RuleTypeRequired
)

Common types of rules. Rule types express what a rule is supposed to check. This information can be used to improve input fields, e.g. HTML form fields can use attributes that correspond with the rule type.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item struct {
	Rules []*Rule
	// contains filtered or unexported fields
}

Item can have zero or more validation rules that are used to validate the item’s value.

func (*Item) EmailAddress

func (i *Item) EmailAddress(message string) *Item

EmailAddress checks if the item’s value is an e-mail address. It only checks the length and whether there is exactly one “at” sign preceded and followed by at least one character.

func (*Item) Equals

func (i *Item) Equals(value2 interface{}, message string) *Item

Equals checks if the item’s value equals value2.

func (*Item) Func

func (i *Item) Func(fn func(value interface{}) (bool, error), message string) *Item

func (*Item) Max

func (i *Item) Max(max float64, message string) *Item

Max checks if the item’s value is equal or less than max.

func (*Item) MaxLength

func (i *Item) MaxLength(maxLength int, message string) *Item

MaxLength checks if the item’s value has a maximum length of maxLength.

func (*Item) Min

func (i *Item) Min(min float64, message string) *Item

Min checks if the item’s value is equal or greater than min.

func (*Item) MinLength

func (i *Item) MinLength(minLength int, message string) *Item

MinLength checks if the item’s value has a minimum length of minLength.

func (*Item) Number

func (i *Item) Number(message string) *Item

Number checks if the item’s value is a number.

func (*Item) Pattern

func (i *Item) Pattern(pattern *regexp.Regexp, message string) *Item

Pattern checks if the item’s value matches the regular expression.

func (*Item) Required

func (i *Item) Required(message string) *Item

Required checks if the item’s value is non-zero.

func (*Item) Validate

func (i *Item) Validate() (bool, string, error)

Validate checks if the item’s value is valid according to the specified validation rules. If it is valid, the function returns true. If it is not valid, the rule’s validation error message is returned. If an error occurred, the error is returned. A returned error does not mean the value is invalid, it solely means something went wrong. Rules are checked in order of creation. If the item’s value was found to be invalid, any further rules are not checked.

type Items

type Items map[string]*Item

Items manages Item objects.

func New

func New() Items

New returns a new instance of Items.

func (Items) Add

func (i Items) Add(name string, value interface{}) *Item

Add adds an item whose value is to be validated. Validation rules must be attached to the item itself.

func (Items) Validate

func (i Items) Validate() (Messages, error)

Validate validates all items.

type Messages

type Messages map[string]string

Messages is a map whose keys are item names and whose values are validation error messages. The map only contains the names of items that failed validation.

func (Messages) Error

func (m Messages) Error() string

Error implements the Error interface.

type Rule

type Rule struct {
	// Arguments that Func was called with.
	Args []interface{}

	// Func returns whether the argument is valid, or that an error occurred
	// while validating. A returned error does not mean the argument is invalid,
	// it solely means something went wrong while validating.
	Func func(interface{}) (bool, error)

	// Message that informs the user if her input is invalid.
	Message string

	// Type gives information about the rule type, e.g. RuleTypeMaxLength means
	// it is a rule for checking maximum length. A value of 0 means no type is
	// provided.
	Type int
}

Rule contains the validation function and information about it.

Jump to

Keyboard shortcuts

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