Validate validates an incoming request according to different criteria: - If the request is nil, [api.ErrEmptyRequest] is returned - The request is validated according to the generated validation method - Lastly, if the request is a [api.PaginatedRequest], an additional check is performed to ensure
(req IncomingRequest)
| 49 | // Note: This function already returns a gRPC error, so the error can be returned directly without any wrapping in a |
| 50 | // request function. |
| 51 | func Validate(req IncomingRequest) (err error) { |
| 52 | // Check, if request is nil. We need to check whether the interface itself is nil (untyped nil); this happens if |
| 53 | // someone is directly setting nil to a variable of the interface IncomingRequest. Furthermore, we need to check, |
| 54 | // whether the *value* of the interface is nil. This can happen if nil is first assigned to a variable of a struct |
| 55 | // (pointer) that implements the interface. If this variable is then passed to the validate function, the req |
| 56 | // parameter is not nil, but the value of the interface representing it is. |
| 57 | if util.IsNil(req) { |
| 58 | return status.Errorf(codes.InvalidArgument, "%s", ErrEmptyRequest) |
| 59 | } |
| 60 | // Validate request |
| 61 | err = validator.Validate(req) |
| 62 | if err != nil { |
| 63 | return status.Errorf(codes.InvalidArgument, "%v: %v", ErrInvalidRequest, err) |
| 64 | } |
| 65 | |
| 66 | return nil |
| 67 | } |
| 68 | |
| 69 | type IncomingRequest interface { |
| 70 | proto.Message |