-
-
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
Support for nested suites #1141
Comments
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.
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 (see
mainSuite
).I know that
matchesArrayOfSuite
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 on
items
andinnerItem
if the object is not valid, but also havemainSuite
to inherit the details fromitemSuite
to show them to their respective fields.The text was updated successfully, but these errors were encountered: