-
-
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
Dynamic fields #1006
Comments
Not knowing much about your specific use-case (maybe share a more detailed example?), but just from the overall description, it seems like you require something of that sort: A combination of each+optional https://vestjs.dev/docs/writing_tests/advanced_test_features/dynamic_tests - allows you to iterate over a list of values to create dynamic tests |
I appreciate your quick response! I will share with you more details -
Is there a way to create a dynamic suite that gets data on the fly? |
each(data.fields, field => {
optional(field.name)
test(
field.name,
'item price must be greater than 0',
() => {
enforce(field.price).isNumeric().greaterThan(0);
},
);
}); @ealush is this how you would write these validation tests? Combining |
@swithek I just ran a test with the exact scenario that you mentioned, and I can confirm it works correctly: https://codesandbox.io/s/react-forked-0bobdc?file=/src/suite.js Type in Req_1 and Req_2, and you'll see that the submit button lights up, even though you did not fill out the optional field. I believe that what you're experiencing is this: It all boils down to what Let me explain: If the field is empty, does that qualify for optional? Maybe. But what if the user typed inside the field and then removed its content - is that qualify as optional? Maybe in your app, the field is optional only if some other condition applies, for example - when another field has been filled... As you see, Instead, a different approach is taken in Vest, assuming it isn't aware of your business logic. Vest determines whether a field qualifies as optional by a certain rule: did it have any test runs?
|
Thank you for your quick reply @ealush! What you're saying makes complete sense, however, as specified in the docs, you can have custom rules that determine whether the field should be optional or not, is that correct? If that's the case, shouldn't having optional({
optional: !data["optional"],
}); be enough to cover all the empty value/undefined field cases, even when there are existing validation results from previous runs? This doesn't seem to work when Here's an example based on your example: https://codesandbox.io/s/react-forked-oy06l8?file=/src/suite.js |
I will have to dig deeper to see why that specific case didn't work for you, but just to unblock you now - using a function should solve it for you. optional({
optional: () => !data.optional
}); https://codesandbox.io/s/react-forked-hpry5u?file=/src/suite.js |
I have a form where some fields are dynamically created, some of the dynamic fields are required and some are optional,
I couldn’t find a way to make vest support this use case, any suggestions?
The text was updated successfully, but these errors were encountered: