Validation: .when and .tag on multiple rules, and relying on actual binding for validation

#1

From I understand, .when(condition) (and .tag(label)) can be applied only to single rules, so if several rules need to depend on the same condition this has to be done:

ValidationRules
.ensure(property)
.required().when(someCondition)
.satisfies(someRule).when(someCondition)
.satisfies(someOtherRule).when(someCondition)

Am I correct of have I missed something?

Moreover, in our application we often rely on elements being actually bound in order to determine whether their values should be validated, thus treating if.bind=“condition” on a component as an implicit .when(condition). (See also this.)
Design-wise, is this considered appropriate?

0 Likes

#2

.tag(label) can be applied to as many rules as you want, I believe.

About .when(condition), i think it’s just a guard so that you can reuse rules. Will the comment Specifies a condition that must be met before attempting to validate the rule. make it easier to understand?

0 Likes

#3

I seem to have explained myself quite poorly, especially in my “single rules” word choice.
I understand how .tag and .when are used and what they mean; what I am looking for (with very low priority) is a way to apply the exact same .tag / .when on several rules at a time; something like turning my code block above into:

ValidationRules
.ensure(property)
.startOfWhenBlock(someCondition)
.required()
.satisfies(someRule)
.satisfies(someOtherRule)
.endOfWhenBlock(someCondition)
.satisfies(someExtraRule)

In this pseudo-example someRule and someOtherRule would be subject to someCondition while someExtraRule would be tested regardless. It would essentially be a shortcut to mass-tag groups of rules.

0 Likes