validation

package
v1.0.4 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	// ValidTag struct tag
	ValidTag       = "valid"
	ValidTagErrMsg = "validErrMsg"
)

Variables

View Source
var CanSkipFuncs = map[string]struct{}{
	"Email":   {},
	"IP":      {},
	"Mobile":  {},
	"Tel":     {},
	"Phone":   {},
	"ZipCode": {},
}

CanSkipFuncs will skip valid if RequiredFirst is true and the struct field's value is empty

View Source
var (

	// ErrInt64On32 show 32 bit platform not support int64
	ErrInt64On32 = fmt.Errorf("not support int64 on 32-bit platform")
)
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",
}

MessageTmpls store commond validate template

Functions

func AddCustomFunc

func AddCustomFunc(name string, f CustomFunc) error

AddCustomFunc Add a custom function to validation The name can not be:

Clear
HasErrors
ErrorMap
Error
Check
Valid
NoMatch

If the name is same with exists function, it will replace the origin valid function

func SetDefaultMessage

func SetDefaultMessage(msg map[string]string)

SetDefaultMessage set default messages if not set, the default messages are

"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",

Types

type Alpha

type Alpha struct {
	Key string
}

Alpha check the alpha

func (Alpha) DefaultMessage

func (a Alpha) DefaultMessage() string

DefaultMessage return the default Length error message

func (Alpha) GetKey

func (a Alpha) GetKey() string

GetKey return the m.Key

func (Alpha) GetLimitValue

func (a Alpha) GetLimitValue() interface{}

GetLimitValue return the limit value

func (Alpha) IsSatisfied

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

IsSatisfied judge whether obj is valid

type AlphaDash

type AlphaDash struct {
	NoMatch
	Key string
}

AlphaDash check not Alpha

func (AlphaDash) DefaultMessage

func (a AlphaDash) DefaultMessage() string

DefaultMessage return the default AlphaDash error message

func (AlphaDash) GetKey

func (a AlphaDash) GetKey() string

GetKey return the n.Key

func (AlphaDash) GetLimitValue

func (a AlphaDash) GetLimitValue() interface{}

GetLimitValue return the limit value

type AlphaNumeric

type AlphaNumeric struct {
	Key string
}

AlphaNumeric check alpha and number

func (AlphaNumeric) DefaultMessage

func (a AlphaNumeric) DefaultMessage() string

DefaultMessage return the default Length error message

func (AlphaNumeric) GetKey

func (a AlphaNumeric) GetKey() string

GetKey return the a.Key

func (AlphaNumeric) GetLimitValue

func (a AlphaNumeric) GetLimitValue() interface{}

GetLimitValue return the limit value

func (AlphaNumeric) IsSatisfied

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

IsSatisfied judge whether obj is valid

type Base64

type Base64 struct {
	Match
	Key string
}

Base64 check struct

func (Base64) DefaultMessage

func (b Base64) DefaultMessage() string

DefaultMessage return the default Base64 error message

func (Base64) GetKey

func (b Base64) GetKey() string

GetKey return the b.Key

func (Base64) GetLimitValue

func (b Base64) GetLimitValue() interface{}

GetLimitValue return the limit value

type CustomFunc

type CustomFunc func(v *Validation, obj interface{}, key string)

CustomFunc is for custom validate function

type Email

type Email struct {
	Match
	Key string
}

Email check struct

func (Email) DefaultMessage

func (e Email) DefaultMessage() string

DefaultMessage return the default Email error message

func (Email) GetKey

func (e Email) GetKey() string

GetKey return the n.Key

func (Email) GetLimitValue

func (e Email) GetLimitValue() interface{}

GetLimitValue return the limit value

type Error

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

Error show the error

func (*Error) Error

func (e *Error) Error() string

Implement Error interface. Return e.String()

func (*Error) String

func (e *Error) String() string

String Returns the Message.

type Funcs

type Funcs map[string]reflect.Value

Funcs Validate function map

func (Funcs) Call

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

Call validate values with named type string

type IP

type IP struct {
	Match
	Key string
}

IP check struct

func (IP) DefaultMessage

func (i IP) DefaultMessage() string

DefaultMessage return the default IP error message

func (IP) GetKey

func (i IP) GetKey() string

GetKey return the i.Key

func (IP) GetLimitValue

func (i IP) GetLimitValue() interface{}

GetLimitValue return the limit value

type Length

type Length struct {
	N   int
	Key string
}

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

func (Length) DefaultMessage

func (l Length) DefaultMessage() string

DefaultMessage return the default Length error message

func (Length) GetKey

func (l Length) GetKey() string

GetKey return the m.Key

func (Length) GetLimitValue

func (l Length) GetLimitValue() interface{}

GetLimitValue return the limit value

func (Length) IsSatisfied

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

IsSatisfied judge whether obj is valid

type Match

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

Match Requires a string to match a given regex.

func (Match) DefaultMessage

func (m Match) DefaultMessage() string

DefaultMessage return the default Match error message

func (Match) GetKey

func (m Match) GetKey() string

GetKey return the m.Key

func (Match) GetLimitValue

func (m Match) GetLimitValue() interface{}

GetLimitValue return the limit value

func (Match) IsSatisfied

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

IsSatisfied judge whether obj is valid

type Max

type Max struct {
	Max int
	Key string
}

Max validate struct

func (Max) DefaultMessage

func (m Max) DefaultMessage() string

DefaultMessage return the default max error message

func (Max) GetKey

func (m Max) GetKey() string

GetKey return the m.Key

func (Max) GetLimitValue

func (m Max) GetLimitValue() interface{}

GetLimitValue return the limit value, Max

func (Max) IsSatisfied

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

IsSatisfied judge whether obj is valid not support int64 on 32-bit platform

type MaxSize

type MaxSize struct {
	Max int
	Key string
}

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

func (MaxSize) DefaultMessage

func (m MaxSize) DefaultMessage() string

DefaultMessage return the default MaxSize error message

func (MaxSize) GetKey

func (m MaxSize) GetKey() string

GetKey return the m.Key

func (MaxSize) GetLimitValue

func (m MaxSize) GetLimitValue() interface{}

GetLimitValue return the limit value

func (MaxSize) IsSatisfied

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

IsSatisfied judge whether obj is valid

type Min

type Min struct {
	Min int
	Key string
}

Min check struct

func (Min) DefaultMessage

func (m Min) DefaultMessage() string

DefaultMessage return the default min error message

func (Min) GetKey

func (m Min) GetKey() string

GetKey return the m.Key

func (Min) GetLimitValue

func (m Min) GetLimitValue() interface{}

GetLimitValue return the limit value, Min

func (Min) IsSatisfied

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

IsSatisfied judge whether obj is valid not support int64 on 32-bit platform

type MinSize

type MinSize struct {
	Min int
	Key string
}

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

func (MinSize) DefaultMessage

func (m MinSize) DefaultMessage() string

DefaultMessage return the default MinSize error message

func (MinSize) GetKey

func (m MinSize) GetKey() string

GetKey return the m.Key

func (MinSize) GetLimitValue

func (m MinSize) GetLimitValue() interface{}

GetLimitValue return the limit value

func (MinSize) IsSatisfied

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

IsSatisfied judge whether obj is valid

type Mobile

type Mobile struct {
	Match
	Key string
}

Mobile check struct

func (Mobile) DefaultMessage

func (m Mobile) DefaultMessage() string

DefaultMessage return the default Mobile error message

func (Mobile) GetKey

func (m Mobile) GetKey() string

GetKey return the m.Key

func (Mobile) GetLimitValue

func (m Mobile) GetLimitValue() interface{}

GetLimitValue return the limit value

type NoMatch

type NoMatch struct {
	Match
	Key string
}

NoMatch Requires a string to not match a given regex.

func (NoMatch) DefaultMessage

func (n NoMatch) DefaultMessage() string

DefaultMessage return the default NoMatch error message

func (NoMatch) GetKey

func (n NoMatch) GetKey() string

GetKey return the n.Key

func (NoMatch) GetLimitValue

func (n NoMatch) GetLimitValue() interface{}

GetLimitValue return the limit value

func (NoMatch) IsSatisfied

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

IsSatisfied judge whether obj is valid

type Numeric

type Numeric struct {
	Key string
}

Numeric check number

func (Numeric) DefaultMessage

func (n Numeric) DefaultMessage() string

DefaultMessage return the default Length error message

func (Numeric) GetKey

func (n Numeric) GetKey() string

GetKey return the n.Key

func (Numeric) GetLimitValue

func (n Numeric) GetLimitValue() interface{}

GetLimitValue return the limit value

func (Numeric) IsSatisfied

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

IsSatisfied judge whether obj is valid

type Phone

type Phone struct {
	Mobile
	Tel
	Key string
}

Phone just for chinese telephone or mobile phone number

func (Phone) DefaultMessage

func (p Phone) DefaultMessage() string

DefaultMessage return the default Phone error message

func (Phone) GetKey

func (p Phone) GetKey() string

GetKey return the p.Key

func (Phone) GetLimitValue

func (p Phone) GetLimitValue() interface{}

GetLimitValue return the limit value

func (Phone) IsSatisfied

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

IsSatisfied judge whether obj is valid

type Range

type Range struct {
	Min
	Max
	Key string
}

Range Requires an integer to be within Min, Max inclusive.

func (Range) DefaultMessage

func (r Range) DefaultMessage() string

DefaultMessage return the default Range error message

func (Range) GetKey

func (r Range) GetKey() string

GetKey return the m.Key

func (Range) GetLimitValue

func (r Range) GetLimitValue() interface{}

GetLimitValue return the limit value, Max

func (Range) IsSatisfied

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

IsSatisfied judge whether obj is valid not support int64 on 32-bit platform

type Required

type Required struct {
	Key string
}

Required struct

func (Required) DefaultMessage

func (r Required) DefaultMessage() string

DefaultMessage return the default error message

func (Required) GetKey

func (r Required) GetKey() string

GetKey return the r.Key

func (Required) GetLimitValue

func (r Required) GetLimitValue() interface{}

GetLimitValue return nil now

func (Required) IsSatisfied

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

IsSatisfied judge whether obj has value

type Result

type Result struct {
	Error *Error
	Ok    bool
}

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

func (*Result) Key

func (r *Result) Key(key string) *Result

Key Get Result by given key string.

func (*Result) Message

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

Message Set Result message by string or format string with args

type Tel

type Tel struct {
	Match
	Key string
}

Tel check telephone struct

func (Tel) DefaultMessage

func (t Tel) DefaultMessage() string

DefaultMessage return the default Tel error message

func (Tel) GetKey

func (t Tel) GetKey() string

GetKey return the t.Key

func (Tel) GetLimitValue

func (t Tel) GetLimitValue() interface{}

GetLimitValue return the limit value

type ValidFormer

type ValidFormer interface {
	Valid(*Validation)
}

ValidFormer valid interface

type ValidFunc

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

ValidFunc Valid function type

type Validation

type Validation struct {
	// if this field set true, in struct tag valid
	// if the struct field vale is empty
	// it will skip those valid functions, see CanSkipFuncs
	RequiredFirst bool

	Errors    []*Error
	ErrorsMap map[string][]*Error
}

A Validation context manages data validation and error messages.

func (*Validation) AddError

func (v *Validation) AddError(key, message string)

AddError adds independent error message for the provided key

func (*Validation) Alpha

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

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

func (*Validation) AlphaDash

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

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

func (*Validation) AlphaNumeric

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

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

func (*Validation) Base64

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

Base64 Test that the obj is base64 encoded if type is string

func (*Validation) Check

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

Check 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()

Clear Clean all ValidationError.

func (*Validation) Email

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

Email Test that the obj is email address if type is string

func (*Validation) Error

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

Error Add an error to the validation context.

func (*Validation) ErrorMap

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

ErrorMap 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

HasErrors Has ValidationError nor not.

func (*Validation) IP

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

IP Test that the obj is IP address if type is string

func (*Validation) Length

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

Length 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) *Result

Match Test that the obj matches regexp if type is string

func (*Validation) Max

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

Max 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) *Result

MaxSize 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) *Result

Min 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) *Result

MinSize 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) *Result

Mobile 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) *Result

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

func (*Validation) Numeric

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

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

func (*Validation) Phone

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

Phone 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) *Result

Range 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)

RecursiveValid 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) *Result

Required 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) *Error

SetError Set error message for one field in ValidationError

func (*Validation) Tel

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

Tel 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)

Valid 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) *Result

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

type Validator

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

Validator interface

type ZipCode

type ZipCode struct {
	Match
	Key string
}

ZipCode check the zip struct

func (ZipCode) DefaultMessage

func (z ZipCode) DefaultMessage() string

DefaultMessage return the default Zip error message

func (ZipCode) GetKey

func (z ZipCode) GetKey() string

GetKey return the z.Key

func (ZipCode) GetLimitValue

func (z ZipCode) GetLimitValue() interface{}

GetLimitValue return the limit value

Jump to

Keyboard shortcuts

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