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

[Feature Request] Expose global validator instance publicly #193

Open
jchadwick-buf opened this issue Feb 24, 2025 · 1 comment
Open

[Feature Request] Expose global validator instance publicly #193

jchadwick-buf opened this issue Feb 24, 2025 · 1 comment
Labels
Feature New feature or request

Comments

@jchadwick-buf
Copy link
Member

Feature description:
getGlobalValidator returns the global validator used by the free Validate function, but there have been some requests to expose the global validator directly.

Problem it solves or use case:
This feature was requested as a part of #190:

Side note: Would you consider making getGlobalValidator from the protovalidate package public? While looking into this, I noticed that we have several different validator instances at play as well because they are being instantiated by libraries. We could make some of them use protovalidate.Validate and share a single validator. Some libraries, such as the Connect interceptor, require a Validator instance to be passed and we can't make it use the global validator because getGlobalValidator is private.

Software (e.g. connectrpc/validate-go) that needs to support a non-default validator has no obvious way to use the global validator instead.

Proposed implementation or solution:
I think exposing getGlobalValidator alone is the lowest possible overhead solution, as it only involves hitting the sync.Once gate one time. However, it isn't exactly the most idiomatic Go.

The sync.Once overhead is very small and already taken by every global Validate call, so another possible alternative is to not directly expose getGlobalValidator, but instead have a global Validator instance whose implementation calls the free Validate function under the hood. If one were trying to solve this problem outside of protovalidate, this is probably roughly the same approach you'd take.

Contribution:
Of course, I am willing to contribute the solution for this.

@ash2k
Copy link

ash2k commented Feb 24, 2025

The main motivator for this feature request is to reduce memory consumption (by making use of the same validator). But this actually may work the other way around too (i.e. things can get worse). If a library that currently require a validator to be passed explicitly starts to use the global one you may end up having two+ validators.

I guess it may still be fine to expose the global validator via a lazy getter and document the recommended usage pattern for the libraries - use the validator explicitly provided by the user and default to getting the global one if none was provided. I.e. highlight that the global one should only be retrieved if the user didn't pass one explicitly to reduce memory footprint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants