0Pricing
C# Academy · Lesson

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

  1. Data Annotation Validation
  2. FluentValidation Rules
  3. Custom and Conditional Rules
  4. Integrating Validation with APIs
← Back to C# Academy