You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api/configuration/unless.md
+65-7Lines changed: 65 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,12 @@ The `.unless` option is used to control when a rule or chain of rules should **n
7
7
8
8
By default, the `.unless` option will apply to all rules in the chain so far, but you can pass a second parameter to specify that it should only apply to the rule immediately preceding it.
9
9
10
+
:::note
11
+
12
+
In the case that there are multiple `.when` and/or `.unless` conditions in the rule chain, each condition applies only to the rules defined **between it and the previous condition**.
13
+
14
+
:::
15
+
10
16
## Examples
11
17
12
18
### Apply to all rules in the chain so far
@@ -51,6 +57,59 @@ formValidator.validate({
51
57
// ❌ { deliveryNote: 'Value cannot be null' }
52
58
```
53
59
60
+
### Multiple calls within the same chain
61
+
62
+
In this example we apply multiple `.unless` conditions within the same rule chain.
63
+
64
+
In particular, we validate that the account balance is non-negative unless overdrafts are allowed, and also validate that the account balance is more than 100 unless the account is not subject to minimum balance requirements.
@@ -122,8 +178,10 @@ Matches the type of the base model.
122
178
123
179
### `appliesTo`
124
180
125
-
This is an optional parameter which can be used to control whether the condition applies to all rules in the chain so far, or just the rule immediately preceding the call to `.unless`.
181
+
This is an optional parameter which can be used to control which rules in the current rule chain the condition applies to.
182
+
183
+
A value of `'AppliesToAllValidators'` means that the `.unless` condition applies to all rules in the current rule chain so far. If there are other calls to `.when` or `.unless` in the chain, only the rules defined since the most recent condition will have the condition applied to them.
126
184
127
-
By default, this parameter is set to `'AppliesToAllValidators'`, which means that the `.unless` condition applies to all rules in the current chain.
185
+
A value of `'AppliesToCurrentValidator'` specifies that the `.unless` condition only controls the execution of the rule immediately preceding it in the current rule chain.
128
186
129
-
Setting this value to `'AppliesToCurrentValidator'` specifies that the `.unless` condition only controls the execution of the rule immediately preceding it in the current chain.
187
+
By default, the `appliesTo` parameter is set to `'AppliesToAllValidators'`.
Copy file name to clipboardExpand all lines: docs/api/configuration/when.md
+57-7Lines changed: 57 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,12 @@ The `.when` option is used to control when a rule or chain of rules should execu
7
7
8
8
By default, the `.when` option will apply to all rules in the chain so far, but you can pass a second parameter to specify that it should only apply to the rule immediately preceding it.
9
9
10
+
:::note
11
+
12
+
In the case that there are multiple `.when` and/or `.unless` conditions in the rule chain, each condition applies only to the rules defined **between it and the previous condition**.
13
+
14
+
:::
15
+
10
16
## Examples
11
17
12
18
### Apply to all rules in the chain so far
@@ -51,6 +57,51 @@ formValidator.validate({
51
57
// ❌ { deliveryNote: 'Value cannot be null' }
52
58
```
53
59
60
+
### Multiple calls within the same chain
61
+
62
+
In this example we apply multiple `.when` conditions within the same rule chain.
63
+
64
+
In particular, we validate that Sunday delivery rates are only applied when the delivery day is a Sunday.
65
+
66
+
```typescript
67
+
import { Validator } from'fluentvalidation-ts';
68
+
69
+
typeFormModel= {
70
+
deliveryDay:string;
71
+
deliveryRate:number;
72
+
};
73
+
74
+
classFormValidatorextendsValidator<FormModel> {
75
+
constructor() {
76
+
super();
77
+
78
+
this.ruleFor('deliveryRate')
79
+
.equal(4.99)
80
+
.withMessage('Sunday rates must apply if delivery day is Sunday')
@@ -122,8 +170,10 @@ Matches the type of the base model.
122
170
123
171
### `appliesTo`
124
172
125
-
This is an optional parameter which can be used to control whether the condition applies to all rules in the chain so far, or just the rule immediately preceding the call to `.when`.
173
+
This is an optional parameter which can be used to control which rules in the current rule chain the condition applies to.
174
+
175
+
A value of `'AppliesToAllValidators'` means that the `.when` condition applies to all rules in the current rule chain so far. If there are other calls to `.when` or `.unless` in the chain, only the rules defined since the most recent condition will have the condition applied to them.
126
176
127
-
By default, this parameter is set to `'AppliesToAllValidators'`, which means that the `.when` condition applies to all rules in the current chain.
177
+
A value of `'AppliesToCurrentValidator'` specifies that the `.when` condition only controls the execution of the rule immediately preceding it in the current rule chain.
128
178
129
-
Setting this value to `'AppliesToCurrentValidator'` specifies that the `.when` condition only controls the execution of the rule immediately preceding it in the current chain.
179
+
By default, the `appliesTo` parameter is set to `'AppliesToAllValidators'`.
0 commit comments