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
How to perform asynchronous validation? #48
Comments
Hey @szhangpitt, good question. I'm not explicitly opposed right now, but I think there are some big issues that would need to be solved before async validators can be considered, to design them in a way that doesn't result in way to much complexity. Some thoughts... In general I like APIs that offer a single way to do things. So my first instinct is that if async validators are to be supported, then the API itself should be async. Meaning that the generic Which means we'd have to have a "sync mode" and an "async mode". Which leaves us with two other potential options, one is to implicitly try to handle any async validator functions, by auto-detecting them and opting into the "async" mode. But this feels strange, because you wouldn't ever really know whether the API is Which means that we'd probably need to do something like you explained with a But that's only the first part of the issue... there are other questions that would need to be solved:
These are all hard problems to design around, and seem like they open a big can of worms in terms of flexibility. So, like I mentioned above, I'm not explicitly anti-async, but someone would need to write up a well thought out proposal that addresses these issues well. My hunch is that async validation logic should be outside the goals of Instead it should concern itself with quick, synchronous checks on the existing data. And then use cases that need additional async validation can use a layered approach, where they do a quick synchronous validation on the shape and types of the data, and handle the slower, more application-specific async validation after that. This allows people to retain full control over the exact async logic that works best for their use case—performance, ordering, etc. Many of the existing cases that people might be tempted to do async validation of data would probably be better served by just making the async operation normally, and handling an error at that point, instead of trying to validate it beforehand. The only real use case I can think of that needs async validation before the event itself occurs is front-end UI validation errors. I'll leave this issue open though so that others can weigh in. Thanks! |
Thanks for the detailed explanation! I too like the simplicity of a sync and consistent API. |
In our use case, we perform asynchronous transforming of data shape. We build a library based on superstruct named bumpover that allows async validating and data transforming. Feel free to give it a try if it helps. |
Hello, I want to get your thoughts on potentially supporting async validation?
My use case: validating an object by querying a service that some key actually exists in backend.
The text was updated successfully, but these errors were encountered: