Skip to content

Commit 3c162b6

Browse files
authored
Use correct pointer in errors.As(). Fix "panic: errors: *target must be interface or implement error" in examples. (#1378)
Only *InvalidValidationError implements error, so we must use double pointer. good explanation: https://www.reddit.com/r/golang/comments/txi397/comment/i3lybab/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button and https://stackoverflow.com/a/69448087/9066110 ## Fixes Or Enhances Fixes `panic: errors: *target must be interface or implement error` for `_examples/simple/` and `_examples/struct-level/` that was introduced in #1346 **Make sure that you've checked the boxes below before you submit PR:** - [x] Tests exist or have been written that cover this particular change. @go-playground/validator-maintainers
1 parent 0240917 commit 3c162b6

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

_examples/simple/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ func validateStruct() {
6262
// this check is only needed when your code could produce
6363
// an invalid value for validation such as interface with nil
6464
// value most including myself do not usually have code like this.
65-
if errors.As(err, &validator.InvalidValidationError{}) {
65+
var invalidValidationError *validator.InvalidValidationError
66+
if errors.As(err, &invalidValidationError) {
6667
fmt.Println(err)
6768
return
6869
}

_examples/struct-level/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ func main() {
115115
// this check is only needed when your code could produce
116116
// an invalid value for validation such as interface with nil
117117
// value most including myself do not usually have code like this.
118-
if errors.As(err, &validator.InvalidValidationError{}) {
118+
var invalidValidationError *validator.InvalidValidationError
119+
if errors.As(err, &invalidValidationError) {
119120
fmt.Println(err)
120121
return
121122
}

0 commit comments

Comments
 (0)