1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 20:00:55 +00:00

Fix after test failure

This commit is contained in:
Andrea Spacca 2018-01-28 18:19:27 +01:00
parent ee9cf05796
commit faa3341603
2 changed files with 60 additions and 2 deletions

View File

@ -245,9 +245,14 @@ func (v *Validation) ZipCode(obj interface{}, key string) *Result {
} }
func (v *Validation) apply(chk Validator, obj interface{}) *Result { func (v *Validation) apply(chk Validator, obj interface{}) *Result {
if reflect.TypeOf(obj).Kind() == reflect.Ptr { validatorName := reflect.TypeOf(chk).Name()
if nil == obj {
if chk.IsSatisfied(obj) {
return &Result{Ok: true}
}
} else if reflect.TypeOf(obj).Kind() == reflect.Ptr {
if reflect.ValueOf(obj).IsNil() { if reflect.ValueOf(obj).IsNil() {
if "Required" != chk.GetKey() { if "Required" != validatorName {
return &Result{Ok: true} return &Result{Ok: true}
} }
} else { } else {

View File

@ -442,3 +442,56 @@ func TestSkipValid(t *testing.T) {
t.Fatal("validation should be passed") t.Fatal("validation should be passed")
} }
} }
func TestPointer(t *testing.T) {
type User struct {
ID int
Email *string `valid:"Email"`
ReqEmail *string `valid:"Required;Email"`
}
u := User{
ReqEmail: nil,
Email: nil,
}
valid := Validation{}
b, err := valid.Valid(u)
if err != nil {
t.Fatal(err)
}
if b {
t.Fatal("validation should not be passed")
}
validEmail := "a@a.com"
u = User{
ReqEmail: &validEmail,
Email: nil,
}
valid = Validation{RequiredFirst: true}
b, err = valid.Valid(u)
if err != nil {
t.Fatal(err)
}
if !b {
t.Fatal("validation should be passed")
}
invalidEmail := "a@a"
u = User{
ReqEmail: &validEmail,
Email: &invalidEmail,
}
valid = Validation{}
b, err = valid.Valid(u)
if err != nil {
t.Fatal(err)
}
if b {
t.Fatal("validation should not be passed")
}
}