Skip to content
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

Closed
wants to merge 4 commits into from
Closed

Conversation

leventeliu
Copy link
Contributor

PGV changed its interface (again 😹) and a new interface ValidateAll() error was added to implement a partial function of the legacy interface Validate(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 over Validate. Also, backward compatibility is considered in these changes. Please review.

@google-cla
Copy link

google-cla bot commented Jul 13, 2021

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 @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@leventeliu
Copy link
Contributor Author

@googlebot I signed it!

validator/validator.go Show resolved Hide resolved
@@ -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 {
Copy link
Collaborator

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

Copy link
Contributor Author

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?

Copy link
Collaborator

@johanbrandhorst johanbrandhorst Jul 15, 2021

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?

Copy link
Contributor Author

@leventeliu leventeliu Jul 15, 2021

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?

Copy link
Collaborator

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?

Copy link
Contributor Author

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.

@leventeliu leventeliu changed the base branch from master to v2 July 16, 2021 02:31
@leventeliu leventeliu changed the base branch from v2 to master July 16, 2021 02:32
@leventeliu leventeliu closed this Jul 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants