-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extra documentation for validating specific properties
- Loading branch information
1 parent
9f786f9
commit e6239df
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,6 +80,7 @@ Example | |
:caption: Other Features | ||
|
||
including-rules | ||
specific-properties | ||
rulesets | ||
cascade | ||
di | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Validating specific properties | ||
|
||
If your validator contains rules for several properties you can limit execution to only validate specific properties by using the `IncludeProperties` option: | ||
|
||
```csharp | ||
// Validator definition | ||
public class CustomerValidator : AbstractValidator<Customer> | ||
{ | ||
public CustomerValidator() | ||
{ | ||
RuleFor(x => x.Surname).NotNull(); | ||
RuleFor(x => x.Forename).NotNull(); | ||
RuleForEach(x => x.Orders).SetValidator(new OrderValidator()); | ||
} | ||
} | ||
``` | ||
|
||
```csharp | ||
var validator = new CustomerValidator(); | ||
validator.Validate(customer, options => | ||
{ | ||
options.IncludeProperties(x => x.Surname); | ||
}); | ||
``` | ||
|
||
In the above example only the rule for the `Surname` property will be executed. | ||
|
||
When working with sub-properties of collections, you can use a wildcard indexer (`[]`) to indicat all items of a collection. For example, if you wanted to validate the `Cost` property of every order, you could use the following: | ||
|
||
```csharp | ||
var validator = new CustomerValidator(); | ||
validator.Validate(customer, options => | ||
{ | ||
options.IncludeProperties("Orders[].Cost"); | ||
}); | ||
``` | ||
|
||
If you want more arbitrary grouping of rules you can use [Rule Sets](rulesets) instead. |