Skip to content

Commit

Permalink
Use correct pointer in errors.As(). Fix "panic: errors: *target must …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
antonsoroko authored Feb 18, 2025
1 parent 0240917 commit 3c162b6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 2 additions & 1 deletion _examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ 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 errors.As(err, &validator.InvalidValidationError{}) {
var invalidValidationError *validator.InvalidValidationError
if errors.As(err, &invalidValidationError) {
fmt.Println(err)
return
}
Expand Down
3 changes: 2 additions & 1 deletion _examples/struct-level/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ 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 errors.As(err, &validator.InvalidValidationError{}) {
var invalidValidationError *validator.InvalidValidationError
if errors.As(err, &invalidValidationError) {
fmt.Println(err)
return
}
Expand Down

0 comments on commit 3c162b6

Please sign in to comment.