-
-
Notifications
You must be signed in to change notification settings - Fork 85
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
feat: Strict Mode #963
Comments
Hey @MentalGear, thank you for taking interest in improving Vest. That’s an interesting one, and the way I see it, it can be the other side of #931. There they ask for type safety - so that only fields that are declared initially will be allowed by typescript in the different fields. You want to make sure that all fields in the data object are covered. I think that these two together can make up a really powerful “strict” mode. I imagine something like:
The idea is that in runtime you create a narrowed down version of all the functions that take the field name (and possibly group name). It is also easier to handle in TS land since we don't need to implicitly propagate them all the way down. I do that explicitly within How does that sound? |
Thanks for the interest @ealush and the detailed description. Indeed, type safety would also be great, but it's a bit too much intricate details for me, at least for the start. What I imagined was a simple flag based approach during init ( for ( const key of Object.keys(userInput) ) {
if (suite.hasOwnProperty(key)===false) throw new Error(`Strict Mode Error: Input has "${key}" property that has no validation rule in vest`)
} I think this would already bring a lot of value (as in the mentioned use-case above for outdated validation detection), and from there on, strict mode could be gradually improved. Where do you think would that strict check best be placed? |
i think it may be nicer (just from the flow) to structure it like this
If you also want to me sure every property has a validator (and typescript to check their existence) you probably have to do something like
For the strict types it may be worth it to switch to a ready made schema library like zod which already supports this and should be relatively easy to include |
Thx for the example, but it seems like it would introduce a new method that is basically the same as suit. Also, this would mean that we would need to rename the calls when we want to use strict. I'd rather prefer a simple flag:
|
renaming has some validity to it since if it really is supposed to be typed the api has to work differently so types can be passed along through the chain i.e. the method "test" needs to be passed along from the typed suite function or it will not be able to provide type info |
Hey, this is a pretty interesting discussion.
While I do see it bring some added value, I also see some of the possible pitfalls:
Thinking into your proposal, I think that strict mode should be bi-directional, meaning that strict will only pass if both are satisfied:
I think that if we create our internal version of test, we don't need to actually modify its interface and the tests can run as they do, with some extra glue code added.
Definitely if strict types is your top priority, then zod is a good library to go to. Their offer is quite different than Vest's so I can see why some would prefer one over the other. I'll let you in on my thought process when adding features to Vest.
I think that this needs a few extra spins to get right, but I'm already starting to iterate on it. I even mentioned this issue when talking about the future of Vest recently in a conference talk. @MentalGear, I'll draft some simple addon that you'll be able to use on top of Vest. Seems pretty feasible without even changing the internals, it does not mean we won't eventually, but just to get the ergonomics right I want a few more iterations on this. |
I'd like to open a discussion for a proposal on a
Strict Mode
, i.e.: An error is returned if not at leastone rule
exists forevery property
in the object to check.IMO, this would feature-complete vest in a sense that it could be reliably used to not only test "what's there" but also what isn't but should. e.g. if forms or data parameters change (but validation was neglect). Please let me know what you think.
The text was updated successfully, but these errors were encountered: