Comments (6)
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 each
is used. Maybe I'm missing something?
Here's an example based on your example: https://codesandbox.io/s/react-forked-oy06l8?file=/src/suite.js
from vest.
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
from vest.
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
https://vestjs.dev/docs/writing_your_suite/optional_fields - allows you to set values as optional so they do not set the form as invalid when they are not filled out
from vest.
I appreciate your quick response!
I will share with you more details -
My form library is Felete and my validator is Vest.
There are some fields in my form that are not consts and I'm getting them at runtime (I don't have the fields names)
It looks something like this:
[
{name: "address" , required: true},
{name: "building_number", required: false},
...
]
Is there a way to create a dynamic suite that gets data on the fly?
from vest.
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 each
and optional
this way doesn't seem to work (i.e., the fields aren't marked as optional).
from vest.
@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:
You type in the optional field, and then remove its content. In this scenario, the field is indeed not valid any more.
It all boils down to what optional
really means in your app's context, and what qualifies as optional-satisfying-test.
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, optional
can mean different things in different context. As I mentioned in a previous answer a couple of weeks back, Vest doesn't have a notion of your apps logic, or in other words - Vest has no knowledge of "what should happen now", it also doesn't make sense of your data, so marking a certain field as optional, doesn't mean Vest is going to check the data properties passed to it, because you may name them whatever you want.
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?
Meaning, if a test of a certain field did indeed run, it will be regarded as required, because its tests were triggered.
- or, in other words - Vest allows the Suite to be regarded as valid, if the fields marked as optional did not run at all.
from vest.
Related Issues (20)
- Vest@5 is ready! Help test it!
- Last next release does not include development builds in dist HOT 4
- Debounce HOT 9
- Pending info in summary HOT 7
- Omit without when HOT 5
- `skip` and `only` on the same level HOT 5
- Subscribe HOT 6
- Migration guide example code - eager -> mode HOT 1
- Problem with OmitWhen executing code when condition is true HOT 2
- Done and empty suite HOT 3
- In SvelteKit, all the asynchronous tests below do not work at all. HOT 1
- Cannot find name 'Predicate'. Did you mean 'Predicates'? HOT 5
- only.group missing HOT 4
- Stateless suite HOT 2
- isDate alway fails HOT 7
- Multiple groups using same tests function HOT 2
- Eager mode does not work in a group/omitWhen/skipWhen HOT 5
- Support for nested suites HOT 2
- isNotEmpty on a numeric field HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vest.