Custom and Conditional Rules
Build complex, conditional validation logic.
Beyond Built-In Validators
Real-world validation often needs custom logic: a value passing a domain check, or a rule that only applies under certain conditions. FluentValidation supports both with Must, When and custom validator types.
The Must Predicate
Must takes a predicate that returns true when the value is valid. It is the simplest way to express arbitrary logic inline.
RuleFor(c => c.Username)
.Must(name => !name.Contains(" "))
.WithMessage("Username cannot contain spaces.");All lessons in this course
- Data Annotation Validation
- FluentValidation Rules
- Custom and Conditional Rules
- Integrating Validation with APIs