valid

package module
v0.0.0-...-f610434 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2014 License: MIT Imports: 3 Imported by: 0

README

valid GoDoc

Simple 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

```go
// 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.
```

Current validators

Strings
  • Len - Length in closed interval [min, max].
  • LenStrict - Length in open interval (min, max).
  • MinLen - Length at least min.
  • MinLenStrict - Length at least min+1.
  • MaxLen - Length at most max.
  • MaxLenStrict - Length at most max-1.
  • Nonempty - string must not be empty.
  • EmailRFC - e-mail address validation using the net/mail package.
  • Email - e-mail address validation using the HTML5 Email field regexp.
  • Regex - Validate using regular expression, given as string.
  • RegexCompiled - Validate using regular expression, given as *regexp.Regexp.

Create custom validators

Validators are different for each type, but they all have similar interfaces.

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.

Example

You can see code for a custom validator here https://gist.github.com/vladvelici/00679f8dff9e205cc157.

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

Constants

This section is empty.

Variables

View Source
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

type StringFunc func(val string) error

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 <[email protected]>" is invalid).

For validation of uniqueness, consider normalising the addresses. (e.g. "[email protected]" is the same as "[email protected]".

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.

Jump to

Keyboard shortcuts

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