update validation interceptor interfaces

This commit is contained in:
Adphi 2021-11-23 13:12:49 +01:00
parent 8e6fde19b5
commit d28f55eb8b

View File

@ -11,6 +11,11 @@ import (
"go.linka.cloud/grpc/interceptors" "go.linka.cloud/grpc/interceptors"
) )
type validatorAll interface {
Validate() error
ValidateAll() error
}
// The validate interface starting with protoc-gen-validate v0.6.0. // The validate interface starting with protoc-gen-validate v0.6.0.
// See https://github.com/envoyproxy/protoc-gen-validate/pull/455. // See https://github.com/envoyproxy/protoc-gen-validate/pull/455.
type validator interface { type validator interface {
@ -42,6 +47,9 @@ func validatorErrorToGrpc(e validatorError) *errdetails.BadRequest_FieldViolatio
} }
func errToStatus(err error) error { func errToStatus(err error) error {
if err == nil {
return nil
}
switch v := err.(type) { switch v := err.(type) {
case validatorError: case validatorError:
return errors.InvalidArgumentD(err, validatorErrorToGrpc(v)) return errors.InvalidArgumentD(err, validatorErrorToGrpc(v))
@ -60,14 +68,15 @@ func errToStatus(err error) error {
func (i interceptor) validate(req interface{}) error { func (i interceptor) validate(req interface{}) error {
switch v := req.(type) { switch v := req.(type) {
case validatorAll:
if i.all {
return errToStatus(v.ValidateAll())
}
return errToStatus(v.Validate())
case validatorLegacy: case validatorLegacy:
if err := v.Validate(); err != nil { return errToStatus(v.Validate())
return errToStatus(err)
}
case validator: case validator:
if err := v.Validate(i.all); err != nil { return errToStatus(v.Validate(i.all))
return errToStatus(err)
}
} }
return nil return nil
} }