Feature Request: Async Validation Support#1
Great library! I love the aggressive validation style.
However, in async-heavy workflows (like the ones I build in async-flow), we often need to validate data against external sources (DB checks, API calls).
Could we add support for async validators? Something like:
const schema = struct.object({
username: struct.string().refine(async (val) => await checkAvailability(val))
});
This would make it indispensable for backend services.
Interesting idea. I usually prefer synchronous validation for speed, but I can see the use case for DB checks. Will consider it for v2.
Update: I am prioritizing standard string validators (email, uuid, etc.) in the next release. Async validation requires a bigger refactor of the police signature, so it will likely land in v1.2.
Thanks for the review @iron-compiler.
- Implemented regex caching using a module-level
Map. Compilation now only happens once per unique pattern string. - Wrapped
new RegExpin a try/catch block. Invalid patterns now throw aValidationErrorinstead of crashing the process.
Ready for another look!
@iron-compiler Thanks for the review! I updated the code to cache compiled regexes (via a module-level Map) and added a try-catch block to handle invalid patterns safely. Should be much more robust now.