@@ -109,23 +109,33 @@ func Violations(violations ...*validate.Violation) Result {
109
109
110
110
func (v violationsResult ) String () string {
111
111
bldr := & strings.Builder {}
112
- errs := v .inner .GetValidationError ().GetViolations ()
113
- _ , _ = fmt .Fprintf (bldr , "%d validation error(s)" , len (errs ))
114
- for i , err := range errs {
115
- forKey := ""
116
- if err .GetForKey () {
117
- forKey = " (key)"
112
+ violations := v .inner .GetValidationError ().GetViolations ()
113
+ if len (violations ) == 1 {
114
+ _ , _ = fmt .Fprintf (bldr , "validation error (%d violation)" , len (violations ))
115
+ } else {
116
+ _ , _ = fmt .Fprintf (bldr , "validation error (%d violations)" , len (violations ))
117
+ }
118
+ for i , violation := range violations {
119
+ _ , _ = fmt .Fprintf (bldr , "\n %s %2d. constraint_id: " , resultPadding , i + 1 )
120
+ if violation .ConstraintId == nil {
121
+ bldr .WriteString ("<nil>" )
122
+ } else {
123
+ _ , _ = fmt .Fprintf (bldr , "%#v" , violation .GetConstraintId ())
124
+ }
125
+ if violation .Message != nil {
126
+ _ , _ = fmt .Fprintf (bldr , "\n %s message: %#v" , resultPadding , violation .GetMessage ())
127
+ }
128
+ //nolint:protogetter
129
+ if violation .Field != nil {
130
+ _ , _ = fmt .Fprintf (bldr , "\n %s field: %#v %s" , resultPadding , fieldpath .Marshal (violation .GetField ()), violation .GetField ())
118
131
}
119
- violationPath := ""
120
- if path := err .GetField (); path != nil {
121
- violationPath = fieldpath .Marshal (path )
132
+ if violation .ForKey != nil {
133
+ _ , _ = fmt .Fprintf (bldr , "\n %s for_key: %v" , resultPadding , violation .GetForKey ())
122
134
}
123
- rulePath := ""
124
- if path := err . GetRule (); path != nil {
125
- rulePath = " (" + fieldpath .Marshal (path ) + ")"
135
+ //nolint:protogetter
136
+ if violation . Rule != nil {
137
+ _ , _ = fmt . Fprintf ( bldr , " \n %s rule: %#v %s" , resultPadding , fieldpath .Marshal (violation . GetRule ()), violation . GetRule ())
126
138
}
127
- _ , _ = fmt .Fprintf (bldr , "\n %s %2d. %s%s: %s%s" , resultPadding , i + 1 , violationPath , forKey , err .GetConstraintId (), rulePath )
128
- _ , _ = fmt .Fprintf (bldr , "\n %s %s" , resultPadding , err .GetMessage ())
129
139
}
130
140
return bldr .String ()
131
141
}
0 commit comments