From d28f55eb8b9546eaf678dfdc7d81dae98a37e3bb Mon Sep 17 00:00:00 2001 From: Adphi Date: Tue, 23 Nov 2021 13:12:49 +0100 Subject: [PATCH] update validation interceptor interfaces --- interceptors/validation/validation.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/interceptors/validation/validation.go b/interceptors/validation/validation.go index 7ec87d5..32aa53b 100644 --- a/interceptors/validation/validation.go +++ b/interceptors/validation/validation.go @@ -11,6 +11,11 @@ import ( "go.linka.cloud/grpc/interceptors" ) +type validatorAll interface { + Validate() error + ValidateAll() error +} + // The validate interface starting with protoc-gen-validate v0.6.0. // See https://github.com/envoyproxy/protoc-gen-validate/pull/455. type validator interface { @@ -42,6 +47,9 @@ func validatorErrorToGrpc(e validatorError) *errdetails.BadRequest_FieldViolatio } func errToStatus(err error) error { + if err == nil { + return nil + } switch v := err.(type) { case validatorError: return errors.InvalidArgumentD(err, validatorErrorToGrpc(v)) @@ -60,14 +68,15 @@ func errToStatus(err error) error { func (i interceptor) validate(req interface{}) error { switch v := req.(type) { + case validatorAll: + if i.all { + return errToStatus(v.ValidateAll()) + } + return errToStatus(v.Validate()) case validatorLegacy: - if err := v.Validate(); err != nil { - return errToStatus(err) - } + return errToStatus(v.Validate()) case validator: - if err := v.Validate(i.all); err != nil { - return errToStatus(err) - } + return errToStatus(v.Validate(i.all)) } return nil }