Skip to content

Commit 97f0c9f

Browse files
committed
Update unless and when documentation
1 parent 846124a commit 97f0c9f

2 files changed

Lines changed: 122 additions & 14 deletions

File tree

docs/api/configuration/unless.md

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ The `.unless` option is used to control when a rule or chain of rules should **n
77

88
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.
99

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+
1016
## Examples
1117

1218
### Apply to all rules in the chain so far
@@ -51,6 +57,59 @@ formValidator.validate({
5157
// ❌ { deliveryNote: 'Value cannot be null' }
5258
```
5359

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.
65+
66+
```typescript
67+
import { Validator } from 'fluentvalidation-ts';
68+
69+
type FormModel = {
70+
accountBalance: number;
71+
allowOverdrafts: boolean;
72+
subjectToMinimumBalance: boolean;
73+
};
74+
75+
class FormValidator extends Validator<FormModel> {
76+
constructor() {
77+
super();
78+
79+
this.ruleFor('accountBalance')
80+
.greaterThanOrEqualTo(0)
81+
// highlight-next-line
82+
.unless((formModel) => formModel.allowOverdrafts)
83+
.greaterThanOrEqualTo(100)
84+
// highlight-next-line
85+
.unless((formModel) => !formModel.subjectToMinimumBalance);
86+
}
87+
}
88+
89+
const formValidator = new FormValidator();
90+
91+
formValidator.validate({
92+
accountBalance: -50,
93+
allowOverdrafts: true,
94+
subjectToMinimumBalance: false,
95+
});
96+
// ✔ {}
97+
98+
formValidator.validate({
99+
accountBalance: -50,
100+
allowOverdrafts: false,
101+
subjectToMinimumBalance: false,
102+
});
103+
// ❌ { accountBalance: 'Value must be greater than or equal to 0' }
104+
105+
formValidator.validate({
106+
accountBalance: 50,
107+
allowOverdrafts: false,
108+
subjectToMinimumBalance: true,
109+
});
110+
// ❌ { accountBalance: 'Value must be greater than or equal to 100' }
111+
```
112+
54113
### Apply to a specific rule in the chain
55114

56115
In this example we apply an `.unless` condition to a specific rule in the chain.
@@ -73,10 +132,7 @@ class FormValidator extends Validator<FormModel> {
73132
.notNull()
74133
.greaterThanOrEqualTo(18)
75134
// highlight-start
76-
.unless(
77-
(formModel) => formModel.alcoholicDrink == null,
78-
'AppliesToCurrentValidator'
79-
);
135+
.unless((formModel) => formModel.alcoholicDrink == null, 'AppliesToCurrentValidator');
80136
// highlight-end
81137
}
82138
}
@@ -122,8 +178,10 @@ Matches the type of the base model.
122178

123179
### `appliesTo`
124180

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.
126184

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.
128186

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'`.

docs/api/configuration/when.md

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ The `.when` option is used to control when a rule or chain of rules should execu
77

88
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.
99

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+
1016
## Examples
1117

1218
### Apply to all rules in the chain so far
@@ -51,6 +57,51 @@ formValidator.validate({
5157
// ❌ { deliveryNote: 'Value cannot be null' }
5258
```
5359

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+
type FormModel = {
70+
deliveryDay: string;
71+
deliveryRate: number;
72+
};
73+
74+
class FormValidator extends Validator<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')
81+
// highlight-next-line
82+
.when((formModel) => formModel.deliveryDay === 'Sunday')
83+
.equal(2.99)
84+
.withMessage('Standard rates must apply if delivery day is Monday to Saturday')
85+
// highlight-next-line
86+
.when((formModel) => formModel.deliveryDay !== 'Sunday');
87+
}
88+
}
89+
90+
const formValidator = new FormValidator();
91+
92+
formValidator.validate({ deliveryDay: 'Sunday', deliveryRate: 4.99 });
93+
// ✔ {}
94+
95+
formValidator.validate({ deliveryDay: 'Sunday', deliveryRate: 2.99 });
96+
// ❌ { deliveryRate: 'Sunday rates must apply if delivery day is Sunday' }
97+
98+
formValidator.validate({ deliveryDay: 'Monday', deliveryRate: 2.99 });
99+
// ✔ {}
100+
101+
formValidator.validate({ deliveryDay: 'Monday', deliveryRate: 4.99 });
102+
// ❌ { deliveryRate: 'Standard rates must apply if delivery day is Monday to Saturday' }
103+
```
104+
54105
### Apply to a specific rule in the chain
55106

56107
In this example we apply a `.when` condition to a specific rule in the chain.
@@ -73,10 +124,7 @@ class FormValidator extends Validator<FormModel> {
73124
.notNull()
74125
.greaterThanOrEqualTo(18)
75126
// highlight-start
76-
.when(
77-
(formModel) => formModel.alcoholicDrink != null,
78-
'AppliesToCurrentValidator'
79-
);
127+
.when((formModel) => formModel.alcoholicDrink != null, 'AppliesToCurrentValidator');
80128
// highlight-end
81129
}
82130
}
@@ -122,8 +170,10 @@ Matches the type of the base model.
122170

123171
### `appliesTo`
124172

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.
126176

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.
128178

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

Comments
 (0)