Compare commits
1 Commits
84ee3011ac
...
31b9859c10
Author | SHA1 | Date |
---|---|---|
Nise Void | 31b9859c10 |
11
validate.go
11
validate.go
|
@ -223,14 +223,14 @@ func getTagFuncs(i int, ft reflect.StructField, kind reflect.Kind, tags []string
|
|||
ptr = false
|
||||
}
|
||||
|
||||
var f validateCheck
|
||||
|
||||
if tag == `optional` {
|
||||
f = func(rv reflect.Value) ([]ValidationError, bool) {
|
||||
rules = append(rules, rule{i, func(rv reflect.Value) ([]ValidationError, bool) {
|
||||
check, _ := getTagFunc(`required`, ``, kind)
|
||||
return nil, check(rv, nil)
|
||||
}})
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
|
||||
var not bool
|
||||
if strings.HasPrefix(tag, `!`) {
|
||||
not = true
|
||||
|
@ -238,14 +238,13 @@ func getTagFuncs(i int, ft reflect.StructField, kind reflect.Kind, tags []string
|
|||
|
||||
check, val := getTagFunc(strings.TrimPrefix(tag, `!`), value, kind)
|
||||
|
||||
f = func(rv reflect.Value) ([]ValidationError, bool) {
|
||||
f := func(rv reflect.Value) ([]ValidationError, bool) {
|
||||
if check(rv, val) == !not {
|
||||
return nil, true
|
||||
}
|
||||
|
||||
return []ValidationError{{Field: []Field{{nil, &ft}}, Check: tag, Value: value}}, false
|
||||
}
|
||||
}
|
||||
|
||||
if ptr {
|
||||
f = depointerFunc(f, ft, tag, value)
|
||||
|
|
|
@ -135,19 +135,14 @@ func (v val) ValidateValue() interface{} {
|
|||
func TestValidateValuer(t *testing.T) {
|
||||
type s struct {
|
||||
A val `validate:"required,lt=2"`
|
||||
B val `validate:"optional,eq=3"`
|
||||
}
|
||||
|
||||
pass1 := s{val{Valid: true}, val{}}
|
||||
pass2 := s{val{Valid: true}, val{Int: 3, Valid: true}}
|
||||
pass := s{val{Valid: true}}
|
||||
|
||||
fail1 := s{}
|
||||
fail2 := s{val{Int: 2, Valid: true}, val{}}
|
||||
fail3 := s{val{Valid: true}, val{Int: 2, Valid: true}}
|
||||
fail2 := s{val{Int: 2, Valid: true}}
|
||||
|
||||
check(t, pass1, 0)
|
||||
check(t, pass2, 0)
|
||||
check(t, pass, 0)
|
||||
check(t, fail1, 1)
|
||||
check(t, fail2, 1)
|
||||
check(t, fail3, 1)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue