Documentation ¶
Overview ¶
Package validation provides validation for values.
Index ¶
- Constants
- type Item
- func (i *Item) EmailAddress(message string) *Item
- func (i *Item) Equals(value2 interface{}, message string) *Item
- func (i *Item) Func(fn func(value interface{}) (bool, error), message string) *Item
- func (i *Item) Max(max float64, message string) *Item
- func (i *Item) MaxLength(maxLength int, message string) *Item
- func (i *Item) Min(min float64, message string) *Item
- func (i *Item) MinLength(minLength int, message string) *Item
- func (i *Item) Number(message string) *Item
- func (i *Item) Pattern(pattern *regexp.Regexp, message string) *Item
- func (i *Item) Required(message string) *Item
- func (i *Item) Validate() (bool, string, error)
- type Items
- type Messages
- type Rule
Constants ¶
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 ¶
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) Validate ¶
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 ¶
Items manages Item objects.
type Messages ¶
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.
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.