Documentation
¶
Overview ¶
Package validation implements a simple library for struct tag validte.
- Use interface for Validater
- Support User define Validater
- Support Struct define validater interface
- Support slice/array/pointer and netestd struct validate. Not for map now!
Index ¶
- Constants
- Variables
- func AddValidater(name string, validater ValidaterFunc) error
- func EnableDebug(flag bool)
- func NewErrWrongType(expect string, value interface{}) error
- type CustomValidators
- type ErrOnlyStrcut
- type ErrUnsupportedType
- type ErrWrongExpectType
- type Error
- type Validater
- type ValidaterFunc
- type Validation
Constants ¶
const ( Email string = "" /* 1212-byte string literal not displayed */ CreditCard string = "" /* 151-byte string literal not displayed */ Alpha string = "^[a-zA-Z]+$" Alphanumeric string = "^[a-zA-Z0-9]+$" Numeric string = "^[-+]?[0-9]+$" Int string = "^(?:[-+]?(?:0|[1-9][0-9]*))$" Float string = "^(?:[-+]?(?:[0-9]+))?(?:\\.[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$" Hexadecimal string = "^[0-9a-fA-F]+$" Hexcolor string = "^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$" RGBcolor string = "" /* 157-byte string literal not displayed */ ASCII string = "^[\x00-\x7F]+$" Multibyte string = "[^\x00-\x7F]" FullWidth string = "[^\u0020-\u007E\uFF61-\uFF9F\uFFA0-\uFFDC\uFFE8-\uFFEE0-9a-zA-Z]" HalfWidth string = "[\u0020-\u007E\uFF61-\uFF9F\uFFA0-\uFFDC\uFFE8-\uFFEE0-9a-zA-Z]" Base64 string = "^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$" PrintableASCII string = "^[\x20-\x7E]+$" DataURI string = "^data:.+\\/(.+);base64$" DNSName string = `^([a-zA-Z0-9]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9]{1}[a-zA-Z0-9_-]{1,62})*$` IP string = `` /* 659-byte string literal not displayed */ URLSchema string = `((ftp|tcp|udp|wss?|https?):\/\/)` URLUsername string = `(\S+(:\S*)?@)` Hostname string = `` URLPath string = `((\/|\?|#)[^\s]*)` URLPort string = `(:(\d{1,5}))` URLIP string = `([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))` URLSubdomain string = `((www\.)|([a-zA-Z0-9]([-\.][a-zA-Z0-9]+)*))` URL string = `^` + URLSchema + `?` + URLUsername + `?` + `((` + URLIP + `|(\[` + IP + `\])|(([a-zA-Z0-9]([a-zA-Z0-9-]+)?[a-zA-Z0-9]([-\.][a-zA-Z0-9]+)*)|(` + URLSubdomain + `?))?(([a-zA-Z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-zA-Z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-zA-Z\x{00a1}-\x{ffff}]{1,}))?))` + URLPort + `?` + URLPath + `?$` )
From https://github.com/asaskevich/govalidator/blob/master/patterns.go
const ( ValidTag = "valid" // Validater tag name FuncSeparator = ";" // Func sparator "required;email" ValidIgnor = "-" // Igore for validater RequiredKey = "required" // required key for not empty value )
Global var for validation pkg
Variables ¶
var ( ErrBadURLFormat = errors.New("url format is not valid") ErrBadEmailFormat = errors.New("email format is not valid") ErrRequired = errors.New("field can't be empty or zero") ErrValidater = errors.New("validater should not be nil") ErrValidaterNoFound = errors.New("validater not found") ErrValidaterExists = errors.New("validater exist") )
Error for Validater
Functions ¶
func AddValidater ¶
func AddValidater(name string, validater ValidaterFunc) error
AddValidater Function export to add user define Validater
func NewErrWrongType ¶
NewErrWrongType new error for unmatched type
Types ¶
type CustomValidators ¶
CustomValidators Because user can add user define validater, avoid data race, add rwlock
func (*CustomValidators) AddValidater ¶
func (cvm *CustomValidators) AddValidater(name string, validater ValidaterFunc) error
AddValidater If name conflict with CustomValidators, replace. conflict with validatorsMap, return err
type ErrOnlyStrcut ¶
ErrOnlyStrcut only for validate struct
func (*ErrOnlyStrcut) Error ¶
func (err *ErrOnlyStrcut) Error() string
ErrOnlyStrcut detail error msg
type ErrUnsupportedType ¶
ErrUnsupportedType not support type
func (*ErrUnsupportedType) Error ¶
func (err *ErrUnsupportedType) Error() string
type ErrWrongExpectType ¶
type ErrWrongExpectType struct { ExpectType string PassValue interface{} }
ErrWrongExpectType expect type don't match passed type
func (*ErrWrongExpectType) Error ¶
func (err *ErrWrongExpectType) Error() string
ErrWrongExpectType detail error
type Validater ¶
type Validater interface {
Validater() error
}
Validater Interface For use define stuct, without params If validate struct has this interface, we will call this interface firstly
type Validation ¶
type Validation struct {
Errors []*Error
}
Validation err list
func (*Validation) ErrMsg ¶
func (mv *Validation) ErrMsg() string
ErrMsg Return msg detail Error message
func (*Validation) HasError ¶
func (mv *Validation) HasError() bool
HasError Check has errors or not
func (*Validation) Validate ¶
func (mv *Validation) Validate(obj interface{}) bool
Validate entry function. True: Validiton passed. False: Validate don't passed, mv.ErrMsg() contains the detail info.