forked from Fuyu/validate
79 lines
1.2 KiB
Go
79 lines
1.2 KiB
Go
|
package validate
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestOptionalMultiple(t *testing.T) {
|
||
|
type s struct {
|
||
|
A string `validate:"optional,eq=a"`
|
||
|
B int `validate:"gt=3,lt=20"`
|
||
|
}
|
||
|
|
||
|
var pass1 = s{``, 4}
|
||
|
var pass2 = s{`a`, 19}
|
||
|
|
||
|
var fail = s{`b`, 3}
|
||
|
|
||
|
check(t, pass1, 0)
|
||
|
check(t, pass2, 0)
|
||
|
check(t, fail, 2)
|
||
|
}
|
||
|
|
||
|
func TestNesting(t *testing.T) {
|
||
|
type sa struct {
|
||
|
AA string `validate:"required"`
|
||
|
}
|
||
|
|
||
|
type sb struct {
|
||
|
BA int `validate:"gt=10"`
|
||
|
}
|
||
|
|
||
|
type s struct {
|
||
|
A []sa `validate:"required"`
|
||
|
B sb
|
||
|
}
|
||
|
|
||
|
var pass = s{[]sa{{`abc`}}, sb{12}}
|
||
|
|
||
|
var fail1 = s{nil, sb{12}}
|
||
|
var fail2 = s{[]sa{{``}}, sb{12}}
|
||
|
var fail3 = s{[]sa{{``}}, sb{9}}
|
||
|
|
||
|
check(t, pass, 0)
|
||
|
check(t, fail1, 1)
|
||
|
check(t, fail2, 1)
|
||
|
check(t, fail3, 2)
|
||
|
}
|
||
|
|
||
|
func TestValidationErrorField(t *testing.T) {
|
||
|
type sc struct {
|
||
|
D int `validate:"eq=1"`
|
||
|
}
|
||
|
type sb struct {
|
||
|
C []sc
|
||
|
}
|
||
|
type sa struct {
|
||
|
B sb
|
||
|
}
|
||
|
type s struct {
|
||
|
A sa
|
||
|
}
|
||
|
|
||
|
errs := Validate(s{
|
||
|
A: sa{
|
||
|
B: sb{
|
||
|
C: []sc{
|
||
|
{D: 0},
|
||
|
{D: 1},
|
||
|
{D: 2},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
})
|
||
|
|
||
|
if errs[0].Field.String() != `A.B.C[0].D` || errs[1].Field.String() != `A.B.C[2].D` {
|
||
|
t.Fatal(`Expected errors to be A.B.C[0].D and A.B.C[2].D; got`, errs[0].Field, `and`, errs[1].Field)
|
||
|
}
|
||
|
}
|