validation-patterns

Implements validation patterns for ASP.NET Core using FluentValidation, DataAnnotations, and MediatR pipeline behaviors.

Updated Mar 8, 2026
One-click install
npx skills add https://github.com/AGIBuild/dotnet.CI.template --skill validation-patterns-agibuild
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: validation-patterns
Source: https://github.com/AGIBuild/dotnet.CI.template/tree/main/.cursor/skills/validation-patterns
Command: npx skills add https://github.com/AGIBuild/dotnet.CI.template --skill validation-patterns-agibuild

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Choosing and implementing the right validation approach in ASP.NET Core is confusing: developers must decide between DataAnnotations, IValidatableObject, custom attributes, IValidateOptions<T>, and FluentValidation, then wire each one correctly while avoiding silent failures and duplicated rules. ## Core Features & Use Cases - Decision guidance: A decision tree maps validation complexity to the right approach, from simple DataAnnotations attributes to FluentValidation for async and database-dependent rules. - Complete implementation patterns: Covers custom ValidationAttribute classes, IValidatableObject cross-property rules, IValidateOptions<T> startup configuration validation, FluentValidation setup with conditional and collection rules, and a MediatR ValidationBehavior pipeline. - Anti-patterns and gotchas: Documents common mistakes such as missing validateAllProperties, init-only options properties, and trusting client-side validation. - Use Case: When building a sign-up endpoint, apply the FluentValidation pattern to enforce password complexity, unique email checks against the database, and terms acceptance, then register a MediatR pipeline behavior so every request is validated automatically. ## Quick Start Ask the agent to implement FluentValidation for a new request model in your ASP.NET Core project, including registration and a MediatR validation pipeline behavior.

Frequently Asked Questions about validation-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I set up FluentValidation in ASP.NET Core?▼

Install the FluentValidation and FluentValidation.DependencyInjectionExtensions NuGet packages, then call AddFluentValidationAutoValidation and AddValidatorsFromAssemblyContaining<Program>() during service registration. Define validators by subclassing AbstractValidator<T> with RuleFor declarations.

FluentValidation vs DataAnnotations: which should I use?▼

DataAnnotations attributes suit simple property-level constraints like Required, Range, and StringLength. FluentValidation fits complex scenarios needing conditional rules, async database checks, collection validation, and highly testable validator classes.

How do I validate configuration options at startup in .NET?▼

Implement IValidateOptions<T> with a Validate method returning ValidateOptionsResult, register it as a singleton, and chain ValidateDataAnnotations() and ValidateOnStart() on the options builder. Without ValidateOnStart, validation only runs on first access.

Why does Validator.TryValidateObject miss some validation errors?▼

By default Validator.TryValidateObject only checks Required attributes. Pass validateAllProperties: true as the fourth argument so all validation attributes on the object are evaluated.

When should I use IValidatableObject instead of a custom ValidationAttribute?▼

Use IValidatableObject when cross-property validation logic is specific to one model, such as comparing start and end dates. Create a custom ValidationAttribute when the same rule must be reused across multiple models.

How do I add validation to a MediatR pipeline?▼

Create a ValidationBehavior<TRequest, TResponse> implementing IPipelineBehavior that runs all injected IValidator<TRequest> instances before calling next. Register it with cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>)) and throw a ValidationException on failures.