Overview
TheIntent.Application.FluentValidation module provides comprehensive validation support for application services using the FluentValidation library. Apply validation rules declaratively in the designer, and the module generates fully-configured validators.
Module:
Intent.Application.FluentValidationVersion: 3.11.6+Dependencies:Intent.Common.CSharpIntent.Modelers.Services
FluentValidation.DependencyInjectionExtensions v12.x (automatically installed)Key Features
- Designer-Driven Validation: Define rules in the Services Designer
- Rich Validation Rules: 20+ built-in validation types
- Custom Validators: Add custom validation logic
- Automatic DI Registration: Validators registered automatically
- MediatR Integration: Pipeline validation for commands/queries
- Unique Constraint Validation: Database-backed uniqueness checks
- Conditional Validation: Apply rules based on conditions
- Cascade Modes: Control validation behavior
- Error Message Customization: Override default messages
Installation
Quick Start
Apply Validation Stereotype
In the Services Designer, apply the Validation stereotype to a parameter or DTO field
Validation Rules
String Validations
Required
Required
Ensures the value is not null or empty.
Generated Validator
Min Length
Min Length
Minimum string length.
Generated Validator
Max Length
Max Length
Maximum string length.
Generated Validator
Email Address
Email Address
Validates email format.
Generated Validator
Regular Expression
Regular Expression
Custom regex pattern validation.
Generated Validator
Numeric Validations
Min Value
Min Value
Minimum numeric value.
Generated Validator
Max Value
Max Value
Maximum numeric value.
Generated Validator
Range
Range
Value must be within a range.
Generated Validator
Collection Validations
Not Empty (Collection)
Not Empty (Collection)
Collection must contain at least one item.
Generated Validator
Collection Item Validation
Collection Item Validation
Validate each item in a collection.
Generated Validator
Special Validations
Enum Validation
Enum Validation
Value must be a valid enum value.
Generated Validator
Must (Custom)
Must (Custom)
Custom validation logic.
Configured in Designer
Generated Validator
Configuration
Module Settings
Unique Constraint Validation
Options:Enabled by default | Disabled by default
Default: Disabled by default
Enables automatic validation for entity unique constraints.
When enabled, the module generates validation rules that check database uniqueness using repository queries.
Generate Stub Validators
Type:switch
Default: true
Generates empty validator classes for commands/queries even if no validation rules are defined.
Cascade Mode
Control how validation proceeds when rules fail:Cascade Mode Options
Generated Validators
Command Validator Example
Nested Object Validation
Nested Validators
Integration
MediatR Pipeline Validation
With theIntent.Application.MediatR.FluentValidation module:
Pipeline Behavior
ASP.NET Core Integration
With theIntent.AspNetCore.Controllers module:
Controller Action
Validation Error Response
Unique Constraint Validation
Validate database uniqueness constraints:Generated Unique Validation
Unique Email Validator
Custom Validation Rules
Add custom validation logic in managed sections:Custom Validation
Best Practices
Validate at Boundaries
Validate at Boundaries
Validate input at application boundaries (commands, DTOs) rather than in domain entities. Keep domain logic separate from validation.
Use Appropriate Error Messages
Use Appropriate Error Messages
Provide clear, actionable error messages:
Leverage Cascade Modes
Leverage Cascade Modes
Use
CascadeMode.Stop for dependent validations to avoid confusing error messages:Keep Validators Focused
Keep Validators Focused
Each validator should handle a single command, query, or DTO. Don’t create god validators.
Troubleshooting
Validators Not Running
Validators Not Running
Issue: Validation rules aren’t being executed.Solution:
- Ensure validators are registered in DI (automatic with FluentValidation module)
- Check MediatR pipeline behavior is registered
- Verify the validator class name matches the command/query name + “Validator”
Unique Validation Performance
Unique Validation Performance
Issue: Slow validation due to database queries.Solution:
- Add database indexes on validated columns
- Consider disabling automatic unique validation
- Rely on database constraints and handle violation exceptions
Validation Exception Handling
Validation Exception Handling
Issue: Unhandled
ValidationException.Solution: The Intent.AspNetCore modules automatically handle validation exceptions. Ensure you’re using compatible versions.