You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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.
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:
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 thesync.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 globalValidate
call, so another possible alternative is to not directly exposegetGlobalValidator
, but instead have a globalValidator
instance whose implementation calls the freeValidate
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.
The text was updated successfully, but these errors were encountered: