Skip to content

Commit

Permalink
Add extra documentation for validating specific properties
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremySkinner committed Feb 13, 2023
1 parent 9f786f9 commit e6239df
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Example
:caption: Other Features

including-rules
specific-properties
rulesets
cascade
di
Expand Down
38 changes: 38 additions & 0 deletions docs/specific-properties.md
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.

0 comments on commit e6239df

Please sign in to comment.