Open codrin-iftimie opened 8 months ago
Seems that the following is producing the right result
const itemSuite = create("itemSuite", (item, path) => {
test(`${path}.name`, "Name is required", () => {
enforce(item.name).isNotEmpty();
});
});
const mainSuite = create("mainSuite", (data, field) => {
only(field);
test("a", "A != B", () => {
enforce(data.a).equals(data.b);
});
itemSuite(data.innerItem, "innerItem");
each(data.items, (item) => {
itemSuite(item, `items.${item.guid}`);
});
});
const result = mainSuite(
{
a: "a",
b: "b",
items: [
{ name: "", guid: 1 },
{ name: "", guid: 2 },
{ name: "test", guid: 3 },
],
innerItem: {
name: "",
},
},
["items.1.name", "innerItem.name"]
); // result.errorCount = 2
Would you advise devs to use this library this way?
Hey @codrin-iftimie, sorry for responding late. I have been traveling.
Yes, your approach should work as expected, and most operations should work correctly when nesting suites this way.
only
, omitWhen
, and skipWhen
, include
should work with no problem. Querying and outputting these results should work as well.
By the way, this will work just the same if you simply use a function for your nested suite, and do not create an entire suite for that. Meaning, simply the callback of your sub suite is sufficient for this. It will be a little easier on performance.
In the future, I might add a few more features around the approach that you selected (creating two suites), but for now, these two approaches are identical in terms of functionality.
I'm researching libraries that can be used to replace my in-house form validator library. I'm using generators at the moment. Most of the requirements this library checks them. Kudos for that. But, one of the common requirements is the ability to nest validators to ensure good code reusability. Let's take this as an example
In some cases, I'd like to use the
itemSuite
to validate an item object on a "details" page where there are independent CRUD operations for each item. In other cases, I have a wizard where we allow the user to add these items as part of a bigger payload (seemainSuite
). I know thatmatchesArrayOfSuite
normatchesSuite
is not part of the currentenforce
API. Is there any plan to support something similar to this that would allow nesting suites? Expectations would be to see a general error onitems
andinnerItem
if the object is not valid, but also havemainSuite
to inherit the details fromitemSuite
to show them to their respective fields.