val

package
v0.0.0-...-3fc3a86 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2015 License: MIT, MIT Imports: 6 Imported by: 0

README

Val

Build Status

A struct validation library for go. Val took code and inspiration from:

type ValStruct struct {
	Required        string            `val:”nonzero”`
	Email           string            `val:”email”`
	Website         string            `val:"url"`
	IpAddress       string            `val:"ip"`
	PetName         string            `val:"alpha"`
	Phone           string            `val:"num | len(10)"`
	Password        string            `val:”alphanum | minlen(5) | maxlen(15)"`
	DayOfWeek       int               `val:”gte(0) | lt(7)”`
	Lat             float64           `val:”lat”`
	Lng             float64           `val:”lon”`
	TennisGameScore string            `val:”in(love,15,30,40)”`
	NewSuperHero    string            `val:”notin(Superman,Batman,The Flash)”`
	UserInfo        map[string]string `val:"haskey(id)"`
	Zipcode         string            `val:”match(^\d{5}(?:[-\s]\d{4})?$)”`
	Color           string            `val:”hexcolor”`
	Ages            []int             `val:”each( gt(18) | lt(35) )”`
	Explode         string            `val:”panic(nonzero)”`
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Alpha = Matches(regexAlpha)
View Source
var AlphaNum = combineValFuncs(Matches("[a-zA-Z]+"), Matches("[0-9]+"))
View Source
var Email = Matches(regexEmail)
View Source
var HexColor = Matches(regexHexColor)
View Source
var Ip = Matches(regexIp)
View Source
var Lat = combineValFuncs(
	numericalMatch("<=", 90.0),
	numericalMatch(">=", -90.0))
View Source
var Lon = combineValFuncs(
	numericalMatch("<=", 180.0),
	numericalMatch(">=", -180.0))
View Source
var Nonzero = func(k string, v interface{}) error {
	m := &matchers.BeZeroMatcher{}
	zero, _ := m.Match(v)
	if zero {
		return formatError(k)
	}
	return nil
}
View Source
var Num = Matches(regexNum)
View Source
var Url = Matches(regexUrl)

Functions

func ErrorFromMap

func ErrorFromMap(errMap map[string]error) error

func Struct

func Struct(str interface{}) (bool, map[string]error)

Types

type ValFunc

type ValFunc func(k string, v interface{}) error

func Each

func Each(valFuncs ...ValFunc) ValFunc

func Gt

func Gt(v interface{}) ValFunc

func Gte

func Gte(v interface{}) ValFunc

func HasKey

func HasKey(k interface{}) ValFunc

func In

func In(list []interface{}) ValFunc

func Len

func Len(l int) ValFunc

func Lt

func Lt(v interface{}) ValFunc

func Lte

func Lte(v interface{}) ValFunc

func Matches

func Matches(regex string) ValFunc

func MaxLen

func MaxLen(l int) ValFunc

func MinLen

func MinLen(l int) ValFunc

func NotIn

func NotIn(list []interface{}) ValFunc

func Panic

func Panic(valFuncs ...ValFunc) ValFunc

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

func New

func New() *Validator

func (*Validator) Add

func (this *Validator) Add(k string, v interface{}, funcs ...ValFunc)

func (*Validator) Validate

func (this *Validator) Validate() (bool, map[string]error)

Jump to

Keyboard shortcuts

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