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