Documentation ¶
Overview ¶
Package valid provides basic input validation for Go. This package does not deal with any form of input processing.
This package is all about checking if values satisfy different validation rules. It is outside the goals of this package to deal with web form input processing. There are already plenty of librbaries doing that.
Usage
// A variable to validate. var input string // ... input processing (e.g. from web forms) ... // Validation errs := valid.String(input, Nonempty("This field is required."), MaxLen(20, "Input is too long.")) // errs is an []error containing errors with the relevant error messages.
Create custom validators ¶
Validators are different for each type, but they all have similar interfaces.
For Strings, the interface `StringValidator` has one method `Validate(string) error`. Any struct that implements this interface can be used as a string validator.
A convenience type is also defined: `type StringFunc func(val string) error`. This type implements the `StringValidator` interface, so any function with the above signature can be used as a validator.
You can see code for a custom validator here https://gist.github.com/vladvelici/00679f8dff9e205cc157.
Index ¶
- Variables
- func String(val string, v ...StringValidator) []error
- type StringFunc
- type StringValidator
- func Alphanumeric(message string) StringValidator
- func AlphanumericPermissive(message string) StringValidator
- func Email(message string) StringValidator
- func EmailRFC(message string) StringValidator
- func MaxLen(max int, message string) StringValidator
- func MaxLenStrict(max int, message string) StringValidator
- func MinLen(min int, message string) StringValidator
- func MinLenStrict(min int, message string) StringValidator
- func Nonempty(message string) StringValidator
Constants ¶
This section is empty.
Variables ¶
var ( // RegAlphanumeric matches alphanumeric characters RegAlphanumeric = regexp.MustCompile("^[a-zA-Z0-9]*$") // RegAlphanumericPermissive matches numbers, letters, -, _, and . RegAlphanumericPermissive = regexp.MustCompile("^[a-zA-Z0-9-_.]*$") // RegEmail is the HTML5 E-mail address regular expression, according to W3C http://www.w3.org/TR/html-markup/input.email.html#input.email.attrs.value.single RegEmail = regexp.MustCompile("^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$") )
Common regular expressions used for validation.
Functions ¶
func String ¶
func String(val string, v ...StringValidator) []error
String applies a list of StringValidators to a string value and returns a list of aggregated errors.
Types ¶
type StringFunc ¶
A StringFunc takes a value to validate and returns a validation error.
This type implements the StringValidator interface, thus any functions with this signature can be casted to StringFunc and used as a StringValidator.
func Len ¶
func Len(min, max int, message string) StringFunc
Len creates a length validator, that checks if the length of given string is in the closed interval [min, max]. It includes min and max: all val that satisfy max >= len(val) >= min are considered valid.
func LenStrict ¶
func LenStrict(min, max int, message string) StringFunc
LenStrict creates a length validator, that checks if the length of given string is in the open interval (min, max). It does not include min and max: all val that satisfy max > len(val) > min are considered valid.
func Regexp ¶
func Regexp(pattern, message string) StringFunc
Regexp creates a regular expression validator, using the pattern given as string.
func RegexpCompiled ¶
func RegexpCompiled(reg *regexp.Regexp, message string) StringFunc
RegexpCompiled creates a regular expression validator, using the given already-compiled *regexp.Regexp.
func (StringFunc) Validate ¶
func (s StringFunc) Validate(val string) error
Validate function of StringFunc
type StringValidator ¶
type StringValidator interface { // Validate validates the given parameter and returns a validation error, or nil // if the input is valid. Validate(string) error }
StringValidator interface is implemented by all string validators.
func Alphanumeric ¶
func Alphanumeric(message string) StringValidator
Alphanumeric creates a validator that consideres all alphanumeric inputs (including the empty string) valid.
func AlphanumericPermissive ¶
func AlphanumericPermissive(message string) StringValidator
AlphanumericPermissive creates a validator that consideres all inputs containing only letters (a-z, A-Z), numbers (0-9), underscore ("_"), minus sign ("-") and period (".") as valid. (including the empty string) valid.
func Email ¶
func Email(message string) StringValidator
Email creates a validator that uses the HTML5 e-mail field regexp as defined by W3C to validate e-mail addresses.
The regular expression is defined by W3C here: http://www.w3.org/TR/html-markup/input.email.html#input.email.attrs.value.single
func EmailRFC ¶
func EmailRFC(message string) StringValidator
EmailRFC creates a validator for e-mail address according to the net/mail package. Non-empty Address.Name addresses are considered invalid (e.g. "John <john@example.org>" is invalid).
For validation of uniqueness, consider normalising the addresses. (e.g. "john+tag@example.com" is the same as "john@example.com".
func MaxLen ¶
func MaxLen(max int, message string) StringValidator
MaxLen creates a maximum length string validator that consideres all strings val valid if they satisfy len(val) <= max.
func MaxLenStrict ¶
func MaxLenStrict(max int, message string) StringValidator
MaxLenStrict creates a maximum length string validator that consideres all strings val valid if they satisfy len(val) < max.
func MinLen ¶
func MinLen(min int, message string) StringValidator
MinLen creates a minimum length string validator that consideres all strings val valid if they satisfy len(val) >= min.
func MinLenStrict ¶
func MinLenStrict(min int, message string) StringValidator
MinLenStrict creates a strict minimum length string validator that consideres all strings val valid if they satisfy len(val) > min.
func Nonempty ¶
func Nonempty(message string) StringValidator
Nonempty creates a validator that checks whether the given string is not empty.