-
Notifications
You must be signed in to change notification settings - Fork 694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add all-validator support #438
Conversation
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here with What to do if you already signed the CLAIndividual signers
Corporate signers
ℹ️ Googlers: Go here for more info. |
@googlebot I signed it! |
@@ -51,9 +78,13 @@ func UnaryServerInterceptor() grpc.UnaryServerInterceptor { | |||
// UnaryClientInterceptor returns a new unary client interceptor that validates outgoing messages. | |||
// | |||
// Invalid messages will be rejected with `InvalidArgument` before sending the request to server. | |||
func UnaryClientInterceptor() grpc.UnaryClientInterceptor { | |||
func UnaryClientInterceptor(validateAll ...bool) grpc.UnaryClientInterceptor { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this signature change needed? This is a potentially breaking change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, I don't see how this can break old code using zero arguments, and this is a common way to add (one) argument for a function without breaking things. Any better idea?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this is a common assumption, but this is actually a breaking change if someone has assigned this function to a variable at some point:
var interceptor func() grpc.UnaryClientInterceptor = validator.UnaryClientInterceptor
I think it's going to be very rarely cause an action breakage but it is technically breaking. Why do we need to add this parameter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, as I said in the PR description, I changed this signature to allow users to choose the new method ValidateAll
over Validate
.
As I know this new method uses a multi-error to return all validation failures. IMHO this would be much more friendly while designing user-oriented and non-performance sensitive APIs -- to tell the user which fields you provided are invalid, at one time.
And as you can see in the previous implementation, while using the old interface Validate(all bool) error
, this middleware directly calls Validate(false)
. In this way, it somehow hides part of PGV's functionality, which doesn't seem to be a good practice to me too.
So, here comes the point we should weigh the pros and cons of changing this signature. As you mentioned such kind of breaking is a real rare case, should we change it here? Or maybe we should create a new type of interceptor if I would like to use ValidateAll
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see now that this is actually targeting the master branch - I think we can just move this PR to the v2 branch and then we can change the signature without worrying about breaking users as it is still in development. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Surely, I cherry-picked it into a new branch and opened a new PR #443. Now I will close this, thanks.
PGV changed its interface (again 😹) and a new interface
ValidateAll() error
was added to implement a partial function of the legacy interfaceValidate(bool) error
.I think it's a good idea to keep this middleware up-to-date and allow users to choose this new method
ValidateAll
overValidate
. Also, backward compatibility is considered in these changes. Please review.