twitchtv / twirp

A simple RPC framework with protobuf service definitions
https://twitchtv.github.io/twirp/docs/intro.html
Apache License 2.0
7.18k stars 327 forks source link

Use ServerHooks to validate request message content #188

Closed manhdaovan closed 5 years ago

manhdaovan commented 5 years ago

If the RequestReceived of ServerHooks struct is changed to func(ctx context.Context, req interface{}) (context.Context, error) instead of func(context.Context) (context.Context, error), go-proto-validators can be integrated to validate request message content.

For example:

type Validator interface{
    Validate() error
}
func ValidateReq(ctx context.Context, req interface{}) (context.Context, error){
    if r, ok := req.(Validator); ok {
        if err := r.Validate(); err != nil {
            return nil, err
        }
    }

    // other logic
}
spenczar commented 5 years ago

This has come up a few times, and after some thorough discussion, we've decided not to do it. Previous issues have the discussion: https://github.com/twitchtv/twirp/issues/187 https://github.com/twitchtv/twirp/issues/184