Skip to content

Commit

Permalink
chore: using errors.As instead of type assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
fatelei committed Dec 20, 2024
1 parent 6c3307e commit d7356f1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 36 deletions.
31 changes: 17 additions & 14 deletions _examples/simple/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"

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

for _, err := range err.(validator.ValidationErrors) {

fmt.Println(err.Namespace())
fmt.Println(err.Field())
fmt.Println(err.StructNamespace())
fmt.Println(err.StructField())
fmt.Println(err.Tag())
fmt.Println(err.ActualTag())
fmt.Println(err.Kind())
fmt.Println(err.Type())
fmt.Println(err.Value())
fmt.Println(err.Param())
fmt.Println()
var validateErrs validator.ValidationErrors
if errors.As(err, &validateErrs) {
for _, e := range validateErrs {
fmt.Println(e.Namespace())
fmt.Println(e.Field())
fmt.Println(e.StructNamespace())
fmt.Println(e.StructField())
fmt.Println(e.Tag())
fmt.Println(e.ActualTag())
fmt.Println(e.Kind())
fmt.Println(e.Type())
fmt.Println(e.Value())
fmt.Println(e.Param())
fmt.Println()
}
}

// from here you can create your own error messages in whatever language you wish
Expand Down
48 changes: 26 additions & 22 deletions _examples/struct-level/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -114,33 +115,36 @@ func main() {
// this check is only needed when your code could produce
// an invalid value for validation such as interface with nil
// value most including myself do not usually have code like this.
if _, ok := err.(*validator.InvalidValidationError); ok {
if errors.As(err, &validator.InvalidValidationError{}) {
fmt.Println(err)
return
}

for _, err := range err.(validator.ValidationErrors) {
e := validationError{
Namespace: err.Namespace(),
Field: err.Field(),
StructNamespace: err.StructNamespace(),
StructField: err.StructField(),
Tag: err.Tag(),
ActualTag: err.ActualTag(),
Kind: fmt.Sprintf("%v", err.Kind()),
Type: fmt.Sprintf("%v", err.Type()),
Value: fmt.Sprintf("%v", err.Value()),
Param: err.Param(),
Message: err.Error(),
var validateErrs validator.ValidationErrors
if errors.As(err, &validateErrs) {
for _, err := range validateErrs {
e := validationError{
Namespace: err.Namespace(),
Field: err.Field(),
StructNamespace: err.StructNamespace(),
StructField: err.StructField(),
Tag: err.Tag(),
ActualTag: err.ActualTag(),
Kind: fmt.Sprintf("%v", err.Kind()),
Type: fmt.Sprintf("%v", err.Type()),
Value: fmt.Sprintf("%v", err.Value()),
Param: err.Param(),
Message: err.Error(),
}

indent, err := json.MarshalIndent(e, "", " ")
if err != nil {
fmt.Println(err)
panic(err)
}

fmt.Println(string(indent))
}

indent, err := json.MarshalIndent(e, "", " ")
if err != nil {
fmt.Println(err)
panic(err)
}

fmt.Println(string(indent))
}

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

0 comments on commit d7356f1

Please sign in to comment.