validation

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2015 License: Apache-2.0 Imports: 7 Imported by: 0

README

validation

validation is a form validation for a data validation and error collecting using Go.

Installation and tests

Install:

go get github.com/astaxie/beego/validation

Test:

go test github.com/astaxie/beego/validation

Example

Direct Use:

import (
	"github.com/astaxie/beego/validation"
	"log"
)

type User struct {
	Name string
	Age int
}

func main() {
	u := User{"man", 40}
	valid := validation.Validation{}
	valid.Required(u.Name, "name")
	valid.MaxSize(u.Name, 15, "nameMax")
	valid.Range(u.Age, 0, 140, "age")
	if valid.HasErrors() {
		// validation does not pass
		// print invalid message
		for _, err := range valid.Errors {
			log.Println(err.Key, err.Message)
		}
	}
	// or use like this
	if v := valid.Max(u.Age, 140); !v.Ok {
		log.Println(v.Error.Key, v.Error.Message)
	}
}

Struct Tag Use:

import (
	"github.com/astaxie/beego/validation"
)

// validation function follow with "valid" tag
// functions divide with ";"
// parameters in parentheses "()" and divide with ","
// Match function's pattern string must in "//"
type user struct {
	Id   int
	Name string `valid:"Required;Match(/^(test)?\\w*@;com$/)"`
	Age  int    `valid:"Required;Range(1, 140)"`
}

func main() {
	valid := Validation{}
	u := user{Name: "test", Age: 40}
	b, err := valid.Valid(u)
	if err != nil {
		// handle error
	}
	if !b {
		// validation does not pass
		// blabla...
	}
}

Struct Tag Functions:

Required
Min(min int)
Max(max int)
Range(min, max int)
MinSize(min int)
MaxSize(max int)
Length(length int)
Alpha
Numeric
AlphaNumeric
Match(pattern string)
AlphaDash
Email
IP
Base64
Mobile
Tel
Phone
ZipCode

LICENSE

BSD License http://creativecommons.org/licenses/BSD/

Documentation

Overview

package for validations

import (
	"github.com/astaxie/beego/validation"
	"log"
)

type User struct {
	Name string
	Age int
}

func main() {
	u := User{"man", 40}
	valid := validation.Validation{}
	valid.Required(u.Name, "name")
	valid.MaxSize(u.Name, 15, "nameMax")
	valid.Range(u.Age, 0, 140, "age")
	if valid.HasErrors() {
		// validation does not pass
		// print invalid message
		for _, err := range valid.Errors {
			log.Println(err.Key, err.Message)
		}
	}
	// or use like this
	if v := valid.Max(u.Age, 140); !v.Ok {
		log.Println(v.Error.Key, v.Error.Message)
	}
}

more info: http://beego.me/docs/mvc/controller/validation.md

Index

Constants

View Source
const (
	VALIDTAG = "valid"
)

Variables

View Source
var MessageTmpls = map[string]string{
	"Required":     "Can not be empty",
	"Min":          "Minimum is %d",
	"Max":          "Maximum is %d",
	"Range":        "Range is %d to %d",
	"MinSize":      "Minimum size is %d",
	"MaxSize":      "Maximum size is %d",
	"Length":       "Required length is %d",
	"Alpha":        "Must be valid alpha characters",
	"Numeric":      "Must be valid numeric characters",
	"AlphaNumeric": "Must be valid alpha or numeric characters",
	"Match":        "Must match %s",
	"NoMatch":      "Must not match %s",
	"AlphaDash":    "Must be valid alpha or numeric or dash(-_) characters",
	"Email":        "Must be a valid email address",
	"IP":           "Must be a valid ip address",
	"Base64":       "Must be valid base64 characters",
	"Mobile":       "Must be valid mobile number",
	"Tel":          "Must be valid telephone number",
	"Phone":        "Must be valid telephone or mobile phone number",
	"ZipCode":      "Must be valid zipcode",
}

Functions

This section is empty.

Types

type Alpha

type Alpha struct {
	Key string
}

func (Alpha) DefaultMessage

func (a Alpha) DefaultMessage() string

func (Alpha) GetKey

func (a Alpha) GetKey() string

func (Alpha) GetLimitValue

func (a Alpha) GetLimitValue() interface{}

func (Alpha) IsSatisfied

func (a Alpha) IsSatisfied(obj interface{}) bool

type AlphaDash

type AlphaDash struct {
	NoMatch
	Key string
}

func (AlphaDash) DefaultMessage

func (a AlphaDash) DefaultMessage() string

func (AlphaDash) GetKey

func (a AlphaDash) GetKey() string

func (AlphaDash) GetLimitValue

func (a AlphaDash) GetLimitValue() interface{}

type AlphaNumeric

type AlphaNumeric struct {
	Key string
}

func (AlphaNumeric) DefaultMessage

func (a AlphaNumeric) DefaultMessage() string

func (AlphaNumeric) GetKey

func (a AlphaNumeric) GetKey() string

func (AlphaNumeric) GetLimitValue

func (a AlphaNumeric) GetLimitValue() interface{}

func (AlphaNumeric) IsSatisfied

func (a AlphaNumeric) IsSatisfied(obj interface{}) bool

type Base64

type Base64 struct {
	Match
	Key string
}

func (Base64) DefaultMessage

func (b Base64) DefaultMessage() string

func (Base64) GetKey

func (b Base64) GetKey() string

func (Base64) GetLimitValue

func (b Base64) GetLimitValue() interface{}

type Email

type Email struct {
	Match
	Key string
}

func (Email) DefaultMessage

func (e Email) DefaultMessage() string

func (Email) GetKey

func (e Email) GetKey() string

func (Email) GetLimitValue

func (e Email) GetLimitValue() interface{}

type Funcs

type Funcs map[string]reflect.Value

Validate function map

func (Funcs) Call

func (f Funcs) Call(name string, params ...interface{}) (result []reflect.Value, err error)

validate values with named type string

type IP

type IP struct {
	Match
	Key string
}

func (IP) DefaultMessage

func (i IP) DefaultMessage() string

func (IP) GetKey

func (i IP) GetKey() string

func (IP) GetLimitValue

func (i IP) GetLimitValue() interface{}

type Length

type Length struct {
	N   int
	Key string
}

Requires an array or string to be exactly a given length.

func (Length) DefaultMessage

func (l Length) DefaultMessage() string

func (Length) GetKey

func (l Length) GetKey() string

func (Length) GetLimitValue

func (l Length) GetLimitValue() interface{}

func (Length) IsSatisfied

func (l Length) IsSatisfied(obj interface{}) bool

type Match

type Match struct {
	Regexp *regexp.Regexp
	Key    string
}

Requires a string to match a given regex.

func (Match) DefaultMessage

func (m Match) DefaultMessage() string

func (Match) GetKey

func (m Match) GetKey() string

func (Match) GetLimitValue

func (m Match) GetLimitValue() interface{}

func (Match) IsSatisfied

func (m Match) IsSatisfied(obj interface{}) bool

type Max

type Max struct {
	Max int
	Key string
}

func (Max) DefaultMessage

func (m Max) DefaultMessage() string

func (Max) GetKey

func (m Max) GetKey() string

func (Max) GetLimitValue

func (m Max) GetLimitValue() interface{}

func (Max) IsSatisfied

func (m Max) IsSatisfied(obj interface{}) bool

type MaxSize

type MaxSize struct {
	Max int
	Key string
}

Requires an array or string to be at most a given length.

func (MaxSize) DefaultMessage

func (m MaxSize) DefaultMessage() string

func (MaxSize) GetKey

func (m MaxSize) GetKey() string

func (MaxSize) GetLimitValue

func (m MaxSize) GetLimitValue() interface{}

func (MaxSize) IsSatisfied

func (m MaxSize) IsSatisfied(obj interface{}) bool

type Min

type Min struct {
	Min int
	Key string
}

func (Min) DefaultMessage

func (m Min) DefaultMessage() string

func (Min) GetKey

func (m Min) GetKey() string

func (Min) GetLimitValue

func (m Min) GetLimitValue() interface{}

func (Min) IsSatisfied

func (m Min) IsSatisfied(obj interface{}) bool

type MinSize

type MinSize struct {
	Min int
	Key string
}

Requires an array or string to be at least a given length.

func (MinSize) DefaultMessage

func (m MinSize) DefaultMessage() string

func (MinSize) GetKey

func (m MinSize) GetKey() string

func (MinSize) GetLimitValue

func (m MinSize) GetLimitValue() interface{}

func (MinSize) IsSatisfied

func (m MinSize) IsSatisfied(obj interface{}) bool

type Mobile

type Mobile struct {
	Match
	Key string
}

func (Mobile) DefaultMessage

func (m Mobile) DefaultMessage() string

func (Mobile) GetKey

func (m Mobile) GetKey() string

func (Mobile) GetLimitValue

func (m Mobile) GetLimitValue() interface{}

type NoMatch

type NoMatch struct {
	Match
	Key string
}

Requires a string to not match a given regex.

func (NoMatch) DefaultMessage

func (n NoMatch) DefaultMessage() string

func (NoMatch) GetKey

func (n NoMatch) GetKey() string

func (NoMatch) GetLimitValue

func (n NoMatch) GetLimitValue() interface{}

func (NoMatch) IsSatisfied

func (n NoMatch) IsSatisfied(obj interface{}) bool

type Numeric

type Numeric struct {
	Key string
}

func (Numeric) DefaultMessage

func (n Numeric) DefaultMessage() string

func (Numeric) GetKey

func (n Numeric) GetKey() string

func (Numeric) GetLimitValue

func (n Numeric) GetLimitValue() interface{}

func (Numeric) IsSatisfied

func (n Numeric) IsSatisfied(obj interface{}) bool

type Phone

type Phone struct {
	Mobile
	Tel
	Key string
}

just for chinese telephone or mobile phone number

func (Phone) DefaultMessage

func (p Phone) DefaultMessage() string

func (Phone) GetKey

func (p Phone) GetKey() string

func (Phone) GetLimitValue

func (p Phone) GetLimitValue() interface{}

func (Phone) IsSatisfied

func (p Phone) IsSatisfied(obj interface{}) bool

type Range

type Range struct {
	Min
	Max
	Key string
}

Requires an integer to be within Min, Max inclusive.

func (Range) DefaultMessage

func (r Range) DefaultMessage() string

func (Range) GetKey

func (r Range) GetKey() string

func (Range) GetLimitValue

func (r Range) GetLimitValue() interface{}

func (Range) IsSatisfied

func (r Range) IsSatisfied(obj interface{}) bool

type Required

type Required struct {
	Key string
}

func (Required) DefaultMessage

func (r Required) DefaultMessage() string

func (Required) GetKey

func (r Required) GetKey() string

func (Required) GetLimitValue

func (r Required) GetLimitValue() interface{}

func (Required) IsSatisfied

func (r Required) IsSatisfied(obj interface{}) bool

type Tel

type Tel struct {
	Match
	Key string
}

func (Tel) DefaultMessage

func (t Tel) DefaultMessage() string

func (Tel) GetKey

func (t Tel) GetKey() string

func (Tel) GetLimitValue

func (t Tel) GetLimitValue() interface{}

type ValidFormer

type ValidFormer interface {
	Valid(*Validation)
}

type ValidFunc

type ValidFunc struct {
	Name   string
	Params []interface{}
}

Valid function type

type Validation

type Validation struct {
	Errors    []*ValidationError
	ErrorsMap map[string]*ValidationError
}

A Validation context manages data validation and error messages.

func (*Validation) Alpha

func (v *Validation) Alpha(obj interface{}, key string) *ValidationResult

Test that the obj is [a-zA-Z] if type is string

func (*Validation) AlphaDash

func (v *Validation) AlphaDash(obj interface{}, key string) *ValidationResult

Test that the obj is [0-9a-zA-Z_-] if type is string

func (*Validation) AlphaNumeric

func (v *Validation) AlphaNumeric(obj interface{}, key string) *ValidationResult

Test that the obj is [0-9a-zA-Z] if type is string

func (*Validation) Base64

func (v *Validation) Base64(obj interface{}, key string) *ValidationResult

Test that the obj is base64 encoded if type is string

func (*Validation) Check

func (v *Validation) Check(obj interface{}, checks ...Validator) *ValidationResult

Apply a group of validators to a field, in order, and return the ValidationResult from the first one that fails, or the last one that succeeds.

func (*Validation) Clear

func (v *Validation) Clear()

Clean all ValidationError.

func (*Validation) Email

func (v *Validation) Email(obj interface{}, key string) *ValidationResult

Test that the obj is email address if type is string

func (*Validation) Error

func (v *Validation) Error(message string, args ...interface{}) *ValidationResult

Add an error to the validation context.

func (*Validation) ErrorMap

func (v *Validation) ErrorMap() map[string]*ValidationError

Return the errors mapped by key. If there are multiple validation errors associated with a single key, the first one "wins". (Typically the first validation will be the more basic).

func (*Validation) HasErrors

func (v *Validation) HasErrors() bool

Has ValidationError nor not.

func (*Validation) IP

func (v *Validation) IP(obj interface{}, key string) *ValidationResult

Test that the obj is IP address if type is string

func (*Validation) Length

func (v *Validation) Length(obj interface{}, n int, key string) *ValidationResult

Test that the obj is same length to n if type is string or slice

func (*Validation) Match

func (v *Validation) Match(obj interface{}, regex *regexp.Regexp, key string) *ValidationResult

Test that the obj matches regexp if type is string

func (*Validation) Max

func (v *Validation) Max(obj interface{}, max int, key string) *ValidationResult

Test that the obj is less than max if obj's type is int

func (*Validation) MaxSize

func (v *Validation) MaxSize(obj interface{}, max int, key string) *ValidationResult

Test that the obj is shorter than max size if type is string or slice

func (*Validation) Min

func (v *Validation) Min(obj interface{}, min int, key string) *ValidationResult

Test that the obj is greater than min if obj's type is int

func (*Validation) MinSize

func (v *Validation) MinSize(obj interface{}, min int, key string) *ValidationResult

Test that the obj is longer than min size if type is string or slice

func (*Validation) Mobile

func (v *Validation) Mobile(obj interface{}, key string) *ValidationResult

Test that the obj is chinese mobile number if type is string

func (*Validation) NoMatch

func (v *Validation) NoMatch(obj interface{}, regex *regexp.Regexp, key string) *ValidationResult

Test that the obj doesn't match regexp if type is string

func (*Validation) Numeric

func (v *Validation) Numeric(obj interface{}, key string) *ValidationResult

Test that the obj is [0-9] if type is string

func (*Validation) Phone

func (v *Validation) Phone(obj interface{}, key string) *ValidationResult

Test that the obj is chinese mobile or telephone number if type is string

func (*Validation) Range

func (v *Validation) Range(obj interface{}, min, max int, key string) *ValidationResult

Test that the obj is between mni and max if obj's type is int

func (*Validation) RecursiveValid

func (v *Validation) RecursiveValid(objc interface{}) (bool, error)

Recursively validate a struct. Step1: Validate by v.Valid Step2: If pass on step1, then reflect obj's fields Step3: Do the Recursively validation to all struct or struct pointer fields

func (*Validation) Required

func (v *Validation) Required(obj interface{}, key string) *ValidationResult

Test that the argument is non-nil and non-empty (if string or list)

func (*Validation) SetError

func (v *Validation) SetError(fieldName string, errMsg string) *ValidationError

Set error message for one field in ValidationError

func (*Validation) Tel

func (v *Validation) Tel(obj interface{}, key string) *ValidationResult

Test that the obj is chinese telephone number if type is string

func (*Validation) Valid

func (v *Validation) Valid(obj interface{}) (b bool, err error)

Validate a struct. the obj parameter must be a struct or a struct pointer

func (*Validation) ZipCode

func (v *Validation) ZipCode(obj interface{}, key string) *ValidationResult

Test that the obj is chinese zip code if type is string

type ValidationError

type ValidationError struct {
	Message, Key, Name, Field, Tmpl string
	Value                           interface{}
	LimitValue                      interface{}
}

func (*ValidationError) String

func (e *ValidationError) String() string

Returns the Message.

type ValidationResult

type ValidationResult struct {
	Error *ValidationError
	Ok    bool
}

A ValidationResult is returned from every validation method. It provides an indication of success, and a pointer to the Error (if any).

func (*ValidationResult) Key

Get ValidationResult by given key string.

func (*ValidationResult) Message

func (r *ValidationResult) Message(message string, args ...interface{}) *ValidationResult

Set ValidationResult message by string or format string with args

type Validator

type Validator interface {
	IsSatisfied(interface{}) bool
	DefaultMessage() string
	GetKey() string
	GetLimitValue() interface{}
}

type ZipCode

type ZipCode struct {
	Match
	Key string
}

func (ZipCode) DefaultMessage

func (z ZipCode) DefaultMessage() string

func (ZipCode) GetKey

func (z ZipCode) GetKey() string

func (ZipCode) GetLimitValue

func (z ZipCode) GetLimitValue() interface{}

Jump to

Keyboard shortcuts

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