little-cui closed pull request #317: [SCB-376] Optimize validate parameter log print URL: https://github.com/apache/incubator-servicecomb-service-center/pull/317
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/pkg/validate/validate.go b/pkg/validate/validate.go index 7bbe15e1..6c25e30d 100644 --- a/pkg/validate/validate.go +++ b/pkg/validate/validate.go @@ -54,8 +54,9 @@ func (v *ValidateRule) String() string { return "{" + util.StringJoin(arr[:idx], ",") + "}" } -func (v *ValidateRule) Match(s interface{}) bool { - var invalid bool = false +func (v *ValidateRule) Match(s interface{}) (ok bool, invalidValue interface{}) { + invalidValue = s + var invalid bool sv := reflect.ValueOf(s) if v.Min > 0 && !invalid { switch sv.Kind() { @@ -107,12 +108,14 @@ func (v *ValidateRule) Match(s interface{}) bool { } keys := sv.MapKeys() for _, key := range keys { - if !itemV.Match(key.Interface()) { + if ok, v := itemV.Match(key.Interface()); !ok { invalid = true + invalidValue = v break } - if !itemV.Match(sv.MapIndex(key).Interface()) { + if ok, v := itemV.Match(sv.MapIndex(key).Interface()); !ok{ invalid = true + invalidValue = v break } } @@ -121,8 +124,9 @@ func (v *ValidateRule) Match(s interface{}) bool { Regexp: v.Regexp, } for i, l := 0, sv.Len(); i < l; i++ { - if !itemV.Match(sv.Index(i).Interface()) { + if ok, v := itemV.Match(sv.Index(i).Interface()) ; !ok{ invalid = true + invalidValue = v break } } @@ -148,7 +152,8 @@ func (v *ValidateRule) Match(s interface{}) bool { } } } - return !invalid + ok = !invalid + return } type Validator struct { @@ -237,11 +242,14 @@ func (v *Validator) Validate(s interface{}) error { } } // TODO null pointer如何校验 - if field.Kind() != reflect.Ptr && !validate.Match(fi) { - if filter(fieldName) { - return fmt.Errorf("The field '%s.%s' value does not match rule: %s", st.Type.Name(), fieldName, validate) + if field.Kind() != reflect.Ptr{ + ok, invalidValue := validate.Match(fi) + if !ok { + if filter(fieldName) { + return fmt.Errorf("The field '%s.%s' value does not match rule: %s", st.Type.Name(), fieldName, validate) + } + return fmt.Errorf("The field '%s.%s' value(%v) does not match rule: %s", st.Type.Name(), fieldName, invalidValue, validate) } - return fmt.Errorf("The field '%s.%s' value(%v) does not match rule: %s", st.Type.Name(), fieldName, fi, validate) } } } ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services