ealush / vest

Vest ✅ Declarative validations framework

Home Page:https://vestjs.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Omit without when

vonagam opened this issue · comments

I am writing a utility for working with forms/vest. Inputs in html can be marked as disabled or readonly. By specification such inputs do not participate in validation. I want to provide a way for a user to toggle those states for arbitrary fields and match behaviour of vest suite with html specification.

There is omitWhen which does what I need but to utilize it I would need to wrap every test call with omitWhen. I can write a wrapper for test and ask user to use it. But I would prefer to have a single simple omit call with a list of disabled/readonly fields at the start of the suite.

Thought about what questions might come up about implementation. One might be about interaction with skip/only. I think omit should take precedence over those and otherwise should not be affected by them, so a separate layer of focus.

Are there any drawbacks to adding omit?

I think that what you're after is optional with custom rules. Optional has two modes - one that only takes the field name. That's the default behavior, and one that takes a boolean/function value that describes whether the field should be omitted.

optional({
  username: true // Will be omitted
});

The only difference between this and omitWhen is that optional runs on a second pass, meaning - it runs the field normally, but then excludes it. I can imagine it useful that if the optional condition is set from the get-go and not a function, it will behave the same as your imagined omit. That's especially true for async validations

Oh, cool, I saw optional but it was documented mostly in the context of a value missing (event custom functions were kinda about that) and there was no example with booleans. Seem to be working, thanks.

Scratch My latest comment about the difference with what you're after. Just looked at the code, it behaves just as you'd expect. Wrote a few new tests to confirm that.

287f65e

Hm... so why not introduce omit as an alias for optional of sorts?

To omit with optional I would need to reduce an array into an object and then vest will iterate over that object to make SuiteOptionalFields.setOptionalField (where third argument can be reused for all omitted, as they have no custom logic or need for checks). So it would be both more convenient and more performant to allow passing an array directly.

omit(["asd"]) as optional({"asd": true}). (Just a thought, not a request.)

I am afraid it might be confusing with the behavior of skip, which works within the current scope, while optional works on the suite level. Essentially, at any nesting level where you put skip, it only applies to the descendants of that scope. A consumer of vest could mistake omit() to work just like this.

I am wondering if this is more reasonable to create a general-case omit() that would be similar to the way skip currently works.