Skip to content

Commit

Permalink
🐛 fix: call of reflect.Value.Interface on zero Value. issues #223
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 1, 2023
1 parent 18b0e03 commit 3142ae2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
27 changes: 27 additions & 0 deletions issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1397,3 +1397,30 @@ func TestIssues_221(t *testing.T) {
fmt.Println(v.Errors) // all error messages
}
}

// https://github.com/gookit/validate/issues/223
func TestIssues_223(t *testing.T) {
m := map[string]any{
"clinics": []map[string]any{
{
"clinic_id": nil,
},
},
}

v := validate.Map(m)

v.StringRule("clinics", "required|array")
v.StringRule("clinics.*.clinic_id", "string")

if !assert.False(t, v.Validate()) { // validate ok
safeData := v.SafeData()

fmt.Println("Validation OK:")
dump.Println(safeData)
} else {
fmt.Println("Validation Fail:")
fmt.Println(string(v.Errors.JSON())) // all error messages
assert.StrContains(t, v.Errors.String(), "clinics.*.clinic_id value must be a string")
}
}
20 changes: 14 additions & 6 deletions validating.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (v *Validation) ValidateErr(scene ...string) error {

// ValidateE do validate processing and return Errors
//
// TIP: need use len() to check the Errors is empty or not.
// NOTE: need use len() to check the return is empty or not.
func (v *Validation) ValidateE(scene ...string) Errors {
if v.Validate(scene...) {
return nil
Expand All @@ -50,6 +50,12 @@ func (v *Validation) Validate(scene ...string) bool {
return v.IsSuccess()
}

// release instance to pool TODO
// defer func() {
// v.resetRules()
// vPool.Put(v)
// }()

// init scene info
v.SetScene(scene...)
v.sceneFields = v.sceneFieldMap()
Expand All @@ -67,11 +73,9 @@ func (v *Validation) Validate(scene ...string) bool {
}

v.hasValidated = true
if v.hasError {
// clear safe data on error.
if v.hasError { // clear safe data on error.
v.safeData = make(map[string]any)
}

return v.IsSuccess()
}

Expand Down Expand Up @@ -247,7 +251,7 @@ func (r *Rule) valueValidate(field, name string, val any, v *Validation) (ok boo
// call custom validator in the rule.
fm := r.checkFuncMeta
if fm == nil {
// fallback: get validator for global or validation
// fallback: get validator from global or validation
fm = v.validatorMeta(name)
if fm == nil {
panicf("the validator '%s' does not exist", r.validator)
Expand Down Expand Up @@ -306,7 +310,11 @@ func (r *Rule) valueValidate(field, name string, val any, v *Validation) (ok boo
return false
}
} else {
subVal = subRv.Interface()
if subRv.IsValid() {
subVal = subRv.Interface()
} else {
subVal = nil
}
}

// 2. call built in validator
Expand Down

0 comments on commit 3142ae2

Please sign in to comment.