Documentation ¶
Index ¶
- func EnumValues(v interface{}) (enum []string)
- func PathIndex(path string, index int) string
- type BoolSpec
- type CNFormat
- func (CNFormat) BanPattern(name string, path string, value string, banPattern []string, ...) string
- func (CNFormat) DateRangeDefaultName() (beginName string, endTime string)
- func (CNFormat) FloatMax(name string, path string, value float64, max float64) string
- func (CNFormat) FloatMin(name string, path string, value float64, min float64) string
- func (CNFormat) IntMax(name string, path string, value int, max int) string
- func (CNFormat) IntMin(name string, path string, value int, min int) string
- func (CNFormat) IntNotAllowEmpty(name string, path string) string
- func (CNFormat) Pattern(name string, path string, value string, pattern []string, failPattern string) string
- func (CNFormat) SliceMaxLen(name string, path string, len int, maxLen int) string
- func (CNFormat) SliceMinLen(name string, path string, len int, minLen int) string
- func (CNFormat) SliceNotAllowEmpty(name string, path string) string
- func (CNFormat) SliceUnique(name string, path string, repeatElement interface{}) string
- func (CNFormat) StringEnum(name string, path string, value string, enum []string) string
- func (CNFormat) StringMaxRuneLen(name string, path string, value string, length uint64) string
- func (CNFormat) StringMinRuneLen(name string, path string, value string, length uint64) string
- func (CNFormat) StringNotAllowEmpty(name string, path string) string
- func (f CNFormat) StringRuneLen(name string, path string, value string, length uint64) string
- func (CNFormat) TimeAfterIt(name string, path string, value time.Time, afterIt time.Time) string
- func (CNFormat) TimeAfterOrEqualIt(name string, path string, value time.Time, afterOrEqualIt time.Time) string
- func (CNFormat) TimeBeforeIt(name string, path string, value time.Time, beforeIt time.Time) string
- func (CNFormat) TimeBeforeOrEqualIt(name string, path string, value time.Time, beforeOrEqualIt time.Time) string
- func (CNFormat) TimeNotAllowZero(name string, path string) string
- func (CNFormat) TimeRangeDefaultName() (beginName string, endTime string)
- type Checker
- type Data
- type ExtString
- func (ExtString) ASCII() StringSpec
- func (ExtString) Base64() StringSpec
- func (ExtString) Base64URL() StringSpec
- func (ExtString) ChinaMobile() StringSpec
- func (ExtString) DataURI() StringSpec
- func (ExtString) Email() StringSpec
- func (ExtString) HSL() StringSpec
- func (ExtString) HSLA() StringSpec
- func (ExtString) Hex() StringSpec
- func (ExtString) HexColor() StringSpec
- func (ExtString) Latitude() StringSpec
- func (ExtString) Longitude() StringSpec
- func (ExtString) PrintableASCII() StringSpec
- func (ExtString) RGB() StringSpec
- func (ExtString) RGBA() StringSpec
- func (ExtString) UUID() StringSpec
- type FloatSpec
- type FloatSpecRender
- type Formatter
- type IntSpec
- type OptionFloat
- type OptionInt
- type Report
- type Rule
- func (r *Rule) Bool(v bool, spec BoolSpec)
- func (r *Rule) Break(message string, path string)
- func (r Rule) CreateMessage(message string, customMessage func() string) string
- func (r *Rule) Error(err error)
- func (r *Rule) Float(v float64, spec FloatSpec)
- func (r *Rule) Int(v int, spec IntSpec)
- func (r *Rule) Int16(v int16, spec IntSpec)
- func (r *Rule) Int32(v int32, spec IntSpec)
- func (r *Rule) Int64(v int64, spec IntSpec)
- func (r *Rule) Int8(v int8, spec IntSpec)
- func (r *Rule) String(v string, spec StringSpec)
- func (r *Rule) Time(v time.Time, spec TimeSpec)
- func (r *Rule) Uint(v uint, spec IntSpec)
- func (r *Rule) Uint16(v uint16, spec IntSpec)
- func (r *Rule) Uint32(v uint32, spec IntSpec)
- func (r *Rule) Uint64(v uint64, spec IntSpec)
- func (r *Rule) Uint8(v uint8, spec IntSpec)
- func (r *Rule) Validator(v interface{ ... }, failMessage string, path string)
- type StringSpec
- func (spec StringSpec) CheckBanPattern(v string, r *Rule) (fail bool)
- func (spec StringSpec) CheckEnum(v string, r *Rule) (fail bool)
- func (spec StringSpec) CheckMaxRuneLen(v string, r *Rule) (fail bool)
- func (spec StringSpec) CheckMinRuneLen(v string, r *Rule) (fail bool)
- func (spec StringSpec) CheckPattern(v string, r *Rule) (fail bool)
- func (spec StringSpec) CheckRuneLen(v string, r *Rule) (fail bool)
- func (s StringSpec) NameIs(name string) StringSpec
- type TimeSpec
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EnumValues ¶
func EnumValues(v interface{}) (enum []string)
Types ¶
type BoolSpec ¶
type OptionBool struct { valid bool bool bool }
func (o OptionBool) String() string { if !o.valid {return ""} if o.bool {return "true"} else { return "false"} }
func (o OptionBool) Valid() bool { return o.valid }
func (o OptionBool) Unwrap() bool { if !o.valid {panic("OptionBool: can not wrap invalid OptionBool")} return o.bool }
func Bool(b bool) OptionBool { return OptionBool{true, b} }
type CNFormat ¶
type CNFormat struct{}
func (CNFormat) BanPattern ¶
func (CNFormat) DateRangeDefaultName ¶
func (CNFormat) IntNotAllowEmpty ¶
func (CNFormat) SliceMaxLen ¶
func (CNFormat) SliceMinLen ¶
func (CNFormat) SliceNotAllowEmpty ¶
func (CNFormat) SliceUnique ¶
func (CNFormat) StringEnum ¶
func (CNFormat) StringMaxRuneLen ¶
func (CNFormat) StringMinRuneLen ¶
func (CNFormat) StringNotAllowEmpty ¶
func (CNFormat) StringRuneLen ¶
func (CNFormat) TimeAfterIt ¶
func (CNFormat) TimeAfterOrEqualIt ¶
func (CNFormat) TimeBeforeIt ¶
func (CNFormat) TimeBeforeOrEqualIt ¶
func (CNFormat) TimeNotAllowZero ¶
func (CNFormat) TimeRangeDefaultName ¶
type Checker ¶
type Checker struct {
Format Formatter
}
func (Checker) Check ¶
Example ¶
package main import ( "context" vd "github.com/goclub/validator" "log" ) func main() { ctx := context.Background() _ = ctx err := func() (err error) { checker := vd.NewCN() createUser := RequestCreateUser{ Email: "xxx@domain.com", Name: "张三", Nickname: "三儿", Age: 20, Skills: []string{"clang", "go"}, Address: RequestCreateUserAddress{ Province: "上海", Detail: "", // }, AddressList: []RequestCreateUserAddress{ { Province: "上海", Detail: "人民广场一号", }, { Province: "上海", Detail: "", // 人民广场一号 }, }, } report, err := checker.Check(createUser) if err != nil { return } if report.Fail { log.Print("fail") log.Print("path:", report.Path) log.Print("message:", report.Message) } else { log.Print("验证通过") } return }() if err != nil { log.Printf("%+v", err) } } type RequestCreateUser struct { Email string Name string Nickname string Age int Skills []string Address RequestCreateUserAddress `json:"address"` AddressList []RequestCreateUserAddress } func (v RequestCreateUser) VD(r *vd.Rule) (err error) { r.String(v.Email, vd.StringSpec{ Name: "邮箱地址", Path: "email", Ext: []vd.StringSpec{vd.ExtString{}.Email()}, }) r.String(v.Name, vd.StringSpec{ Name: "姓名", Path: "name", MinRuneLen: 2, MaxRuneLen: 20, }) r.String(v.Nickname, vd.StringSpec{ Name: "昵称", Path: "nickname", AllowEmpty: true, BanPattern: []string{`\d`}, PatternMessage: "昵称不允许包含数字", MinRuneLen: 2, MaxRuneLen: 10, }) r.Int(v.Age, vd.IntSpec{ Name: "年龄", Path: "age", Min: vd.Int(18), MinMessage: "只允许成年人注册", }) r.Int(len(v.Skills), vd.IntSpec{ Name: "技能", Path: "skills", Max: vd.Int(10), MaxMessage: "最多填写{{Max}}项", }) for index, skill := range v.Skills { r.String(skill, vd.StringSpec{ Name: "技能项", Path: vd.PathIndex("skill", index), }) } return nil } type RequestCreateUserAddress struct { Province string Detail string } func (v RequestCreateUserAddress) VD(r *vd.Rule) (err error) { r.String(v.Province, vd.StringSpec{ Path: "province", Name: "省", }) r.String(v.Detail, vd.StringSpec{ Name: "详细地址", Path: "detail", Pattern: []string{`号`}, PatternMessage: "地址必须包含门牌号,例如:某路110号", }) return }
Output:
type ExtString ¶
type ExtString struct { }
func (ExtString) ASCII ¶
func (ExtString) ASCII() StringSpec
func (ExtString) Base64 ¶
func (ExtString) Base64() StringSpec
func (ExtString) Base64URL ¶
func (ExtString) Base64URL() StringSpec
func (ExtString) ChinaMobile ¶
func (ExtString) ChinaMobile() StringSpec
func (ExtString) DataURI ¶
func (ExtString) DataURI() StringSpec
func (ExtString) Email ¶
func (ExtString) Email() StringSpec
func (ExtString) HSL ¶
func (ExtString) HSL() StringSpec
func (ExtString) HSLA ¶
func (ExtString) HSLA() StringSpec
func (ExtString) Hex ¶
func (ExtString) Hex() StringSpec
func (ExtString) HexColor ¶
func (ExtString) HexColor() StringSpec
func (ExtString) Latitude ¶
func (ExtString) Latitude() StringSpec
func (ExtString) Longitude ¶
func (ExtString) Longitude() StringSpec
func (ExtString) PrintableASCII ¶
func (ExtString) PrintableASCII() StringSpec
func (ExtString) RGB ¶
func (ExtString) RGB() StringSpec
func (ExtString) RGBA ¶
func (ExtString) RGBA() StringSpec
func (ExtString) UUID ¶
func (ExtString) UUID() StringSpec
type FloatSpec ¶
type FloatSpec struct { Name string Path string // AllowZero bool // 暂时取消 AllowZero,目的是降低使用者学习成本,观察一段时间后再决定是否完全去掉 (2020年08月07日 by @nimoc) Min OptionFloat MinMessage string Max OptionFloat MaxMessage string Pattern []string BanPattern []string PatternMessage string }
func (FloatSpec) CheckBanPattern ¶
type FloatSpecRender ¶
type FloatSpecRender struct { Value interface{} FloatSpec }
type Formatter ¶
type Formatter interface { Pattern(name string, path string, value string, pattern []string, failPattern string) string BanPattern(name string, path string, value string, banPattern []string, failBanPattern string) string StringNotAllowEmpty(name string, path string) string StringRuneLen(name string, path string, value string, length uint64) string StringMinRuneLen(name string, path string, value string, length uint64) string StringMaxRuneLen(name string, path string, value string, length uint64) string StringEnum(name string, path string, value string, enum []string) string IntNotAllowEmpty(name string, path string) string IntMin(name string, path string, value int, min int) string IntMax(name string, path string, value int, max int) string FloatMin(name string, path string, value float64, min float64) string FloatMax(name string, path string, value float64, max float64) string SliceMinLen(name string, path string, len int, minLen int) string SliceMaxLen(name string, path string, len int, maxLen int) string SliceNotAllowEmpty(name string, path string) string SliceUnique(name string, path string, repeatElement interface{}) string TimeRangeDefaultName() (beginName string, endTime string) DateRangeDefaultName() (beginName string, endTime string) TimeNotAllowZero(name string, path string) string TimeBeforeIt(name string, path string, value time.Time, beforeIt time.Time) string TimeAfterIt(name string, path string, value time.Time, afterIt time.Time) string TimeBeforeOrEqualIt(name string, path string, value time.Time, beforeOrEqualIt time.Time) string TimeAfterOrEqualIt(name string, path string, value time.Time, afterOrEqualIt time.Time) string }
type IntSpec ¶
type OptionFloat ¶
type OptionFloat struct {
// contains filtered or unexported fields
}
option Float simulate Float? (Float or nil) vd.Float(18.1) equal OptionFloat{valid: true, float: 18.1}
func Float ¶
func Float(f float64) OptionFloat
func (OptionFloat) String ¶
func (o OptionFloat) String() string
func (OptionFloat) Unwrap ¶
func (o OptionFloat) Unwrap() float64
func (OptionFloat) Valid ¶
func (o OptionFloat) Valid() bool
type OptionInt ¶
type OptionInt struct {
// contains filtered or unexported fields
}
option int simulate int? (int or nil) vd.Int(18) equal OptionInt{valid: true, int: 18}
type Rule ¶
type Rule struct { Fail bool Message string Path []string Format Formatter // contains filtered or unexported fields }
func (Rule) CreateMessage ¶
func (*Rule) String ¶
func (r *Rule) String(v string, spec StringSpec)
type StringSpec ¶
type StringSpec struct { Name string Path string AllowEmpty bool RuneLen uint64 RuneLenMessage string MinRuneLen uint64 MinRuneLenMessage string MaxRuneLen uint64 MaxRuneLenMessage string Pattern []string BanPattern []string PatternMessage string Enum []string Ext []StringSpec }
func (StringSpec) CheckBanPattern ¶
func (spec StringSpec) CheckBanPattern(v string, r *Rule) (fail bool)
func (StringSpec) CheckMaxRuneLen ¶
func (spec StringSpec) CheckMaxRuneLen(v string, r *Rule) (fail bool)
func (StringSpec) CheckMinRuneLen ¶
func (spec StringSpec) CheckMinRuneLen(v string, r *Rule) (fail bool)
func (StringSpec) CheckPattern ¶
func (spec StringSpec) CheckPattern(v string, r *Rule) (fail bool)
func (StringSpec) CheckRuneLen ¶
func (spec StringSpec) CheckRuneLen(v string, r *Rule) (fail bool)
func (StringSpec) NameIs ¶
func (s StringSpec) NameIs(name string) StringSpec
Source Files ¶
Click to show internal directories.
Click to hide internal directories.