z-schema
z-schema copied to clipboard
discuss: Custom formats are module-scoped
What was the reasoning behind making ZSchema.registerFormat
instead of validator.registerFormat
? As it stands right now, using multiple ZSchema
instances in the same process with different custom formats is impossible because the registry of custom formats is stored in the ZSchema
module itself, not in the ZSchema
instance. Before I blindly ask for, or submit a PR to, make the custom formats scoped to the ZSchema
instance, I wanted to better understand.
The only thing that pops to my mind years after writing this is that I never expected a conflict. If you have a format xxx
meaning one thing and format xxx
meaning other thing within one application, something smells very weird?
Don't think of it in terms of conflicts in how a format is handled but in terms of different validators (dis)allowing different formats. Maybe I'm just writing tests that are too strict but since I wrap z-schema
, I typically start a test where I register a format by using z-schema
in a way where the format hasn't been registered, asserting that there was a failure, registering the format with my API and then running the same test to ensure there was no failure.
Where this becomes an issue is when I create a new ZSchema
in between test runs. If I run a pre/post registration test flow more than once, the second set of tests fail unless I use a unique format name for each set of tests.
Yeah, in this case, that's bad. It probably should have lived on the instance, not the class. I don't see a downside in changing that with a PR.
I'll get started.