go-playground / validator

:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
MIT License
16.05k stars 1.29k forks source link

Add support for `errors.As` #1273

Open roman-vanesyan opened 1 month ago

roman-vanesyan commented 1 month ago

Package version eg. v9, v10:

v10

Issue, Question or Enhancement:

Provide support for the errors.As. Go 1.13+ has addition of errors.As that helps to cast given error the target if it or one of its nested errors implements the type of target.

To add support for the errors.As functionality all publicly available errors should implement As method of the following signature:

As(interface{}) bool

Code sample, to showcase or reproduce:

err := validator.Struct(&v)
if err != nil {
  err = fmt.Errorf("wrapped error: %w", err)
}

// ...

var validationErr *validation.ValidationErrors
ok := errors.As(err, &validationErr) // true