Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: using errors.As instead of type assertion #1346

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading