Commit faa33416 authored by Andrea Spacca's avatar Andrea Spacca

Fix after test failure

parent ee9cf057
......@@ -245,9 +245,14 @@ func (v *Validation) ZipCode(obj interface{}, key string) *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 "Required" != chk.GetKey() {
if "Required" != validatorName {
return &Result{Ok: true}
}
} else {
......
......@@ -442,3 +442,56 @@ func TestSkipValid(t *testing.T) {
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")
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment