Skip to content

Commit e564451

Browse files
authored
chore: using errors.As instead of type assertion (#1346)
## Fixes Or Enhances fix issue #763 **Make sure that you've checked the boxes below before you submit PR:** - [ ] Tests exist or have been written that cover this particular change. @go-playground/validator-maintainers
1 parent 57dcfdc commit e564451

File tree

2 files changed

+43
-36
lines changed

2 files changed

+43
-36
lines changed

_examples/simple/main.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"errors"
45
"fmt"
56

67
"github.com/go-playground/validator/v10"
@@ -61,24 +62,26 @@ func validateStruct() {
6162
// this check is only needed when your code could produce
6263
// an invalid value for validation such as interface with nil
6364
// value most including myself do not usually have code like this.
64-
if _, ok := err.(*validator.InvalidValidationError); ok {
65+
if errors.As(err, &validator.InvalidValidationError{}) {
6566
fmt.Println(err)
6667
return
6768
}
6869

69-
for _, err := range err.(validator.ValidationErrors) {
70-
71-
fmt.Println(err.Namespace())
72-
fmt.Println(err.Field())
73-
fmt.Println(err.StructNamespace())
74-
fmt.Println(err.StructField())
75-
fmt.Println(err.Tag())
76-
fmt.Println(err.ActualTag())
77-
fmt.Println(err.Kind())
78-
fmt.Println(err.Type())
79-
fmt.Println(err.Value())
80-
fmt.Println(err.Param())
81-
fmt.Println()
70+
var validateErrs validator.ValidationErrors
71+
if errors.As(err, &validateErrs) {
72+
for _, e := range validateErrs {
73+
fmt.Println(e.Namespace())
74+
fmt.Println(e.Field())
75+
fmt.Println(e.StructNamespace())
76+
fmt.Println(e.StructField())
77+
fmt.Println(e.Tag())
78+
fmt.Println(e.ActualTag())
79+
fmt.Println(e.Kind())
80+
fmt.Println(e.Type())
81+
fmt.Println(e.Value())
82+
fmt.Println(e.Param())
83+
fmt.Println()
84+
}
8285
}
8386

8487
// from here you can create your own error messages in whatever language you wish

_examples/struct-level/main.go

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"reflect"
78
"strings"
@@ -114,33 +115,36 @@ func main() {
114115
// this check is only needed when your code could produce
115116
// an invalid value for validation such as interface with nil
116117
// value most including myself do not usually have code like this.
117-
if _, ok := err.(*validator.InvalidValidationError); ok {
118+
if errors.As(err, &validator.InvalidValidationError{}) {
118119
fmt.Println(err)
119120
return
120121
}
121122

122-
for _, err := range err.(validator.ValidationErrors) {
123-
e := validationError{
124-
Namespace: err.Namespace(),
125-
Field: err.Field(),
126-
StructNamespace: err.StructNamespace(),
127-
StructField: err.StructField(),
128-
Tag: err.Tag(),
129-
ActualTag: err.ActualTag(),
130-
Kind: fmt.Sprintf("%v", err.Kind()),
131-
Type: fmt.Sprintf("%v", err.Type()),
132-
Value: fmt.Sprintf("%v", err.Value()),
133-
Param: err.Param(),
134-
Message: err.Error(),
123+
var validateErrs validator.ValidationErrors
124+
if errors.As(err, &validateErrs) {
125+
for _, err := range validateErrs {
126+
e := validationError{
127+
Namespace: err.Namespace(),
128+
Field: err.Field(),
129+
StructNamespace: err.StructNamespace(),
130+
StructField: err.StructField(),
131+
Tag: err.Tag(),
132+
ActualTag: err.ActualTag(),
133+
Kind: fmt.Sprintf("%v", err.Kind()),
134+
Type: fmt.Sprintf("%v", err.Type()),
135+
Value: fmt.Sprintf("%v", err.Value()),
136+
Param: err.Param(),
137+
Message: err.Error(),
138+
}
139+
140+
indent, err := json.MarshalIndent(e, "", " ")
141+
if err != nil {
142+
fmt.Println(err)
143+
panic(err)
144+
}
145+
146+
fmt.Println(string(indent))
135147
}
136-
137-
indent, err := json.MarshalIndent(e, "", " ")
138-
if err != nil {
139-
fmt.Println(err)
140-
panic(err)
141-
}
142-
143-
fmt.Println(string(indent))
144148
}
145149

146150
// from here you can create your own error messages in whatever language you wish

0 commit comments

Comments
 (0)