Agent skill
dotnet-source-gen-options-validation
Converts options validation to use the compile-time source generator for AOT-compatible, reflection-free validation. Also use when the user mentions "OptionsValidator," "options validation source gen," "AOT options validation," "compile-time validation," "ValidateDataAnnotations replacement," or "reflection-free validation." For full AOT analysis, see dotnet-aot-analysis.
Install this agent skill to your Project
npx add-skill https://github.com/Im5tu/claude/tree/main/skills/dotnet-source-gen-options-validation
Metadata
Additional technical details for this skill
- author
- Im5tu
- version
- 1.0
- repositoryUrl
- https://github.com/im5tu/dotnet-skills
SKILL.md
Convert options validation to use the compile-time source generator, enabling AOT-compatible, reflection-free validation at startup.
When to Use
- Preparing application for Native AOT compilation
- Eliminating reflection-based validation overhead
- Ensuring validation errors surface at compile time rather than runtime
- Migrating from
ValidateDataAnnotations()to source-generated validation
Steps
-
Find options classes with data annotations:
- Search for classes using
[Required],[Range],[RegularExpression],[MaxLength],[MinLength],[Length] - These are typically in files matching
*Options.csor*Settings.cs
- Search for classes using
-
Check existing validation setup:
- If class already has a corresponding
[OptionsValidator]partial class, skip it - Note any existing
ValidateDataAnnotations()calls for removal later
- If class already has a corresponding
-
For each options class, create a partial validator class:
csharp[OptionsValidator] public partial class Validate{OptionsClassName} : IValidateOptions<{OptionsClassName}> { }- Place the validator in the same namespace as the options class
- The source generator will implement the validation logic at compile time
-
Register the validator in DI:
csharpservices.AddSingleton<IValidateOptions<MyOptions>, ValidateMyOptions>();- Replace any existing
ValidateDataAnnotations()calls - Ensure
Microsoft.Extensions.Optionsusing directive is present
- Replace any existing
-
Verify with build:
bashdotnet build -
If build fails, check for:
- Missing
partialkeyword on validator class - Unsupported validation attributes
- Missing package reference to
Microsoft.Extensions.Options
- Missing
-
Report results:
- List all validator classes created
- List all DI registrations added
- Confirm build status
Supported Validation Attributes
[Required]- Property must have a value[Range]- Numeric value within specified range[RegularExpression]- String matches regex pattern[MaxLength]- Maximum length for strings/collections[MinLength]- Minimum length for strings/collections[Length]- Exact or range length constraint
Notes
- Requires .NET 8.0 or higher
- Requires
Microsoft.Extensions.Optionsversion 8.0 or higher ValidateDataAnnotations()is NOT needed and should be removed- The
[OptionsValidator]attribute triggers source generation - Validation runs at startup when options are first resolved
- Unsupported attributes will produce compiler warnings
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
flutter-animations
Comprehensive guide for implementing animations in Flutter. Use when adding motion and visual effects to Flutter apps: implicit animations (AnimatedContainer, AnimatedOpacity, TweenAnimationBuilder), explicit animations (AnimationController, Tween, AnimatedWidget/AnimatedBuilder), hero animations (shared element transitions), staggered animations (sequential/overlapping), and physics-based animations. Includes workflow for choosing the right animation type, implementation patterns, and best practices for performance and user experience.
dotnet-source-gen-logging
Converts logging to use the LoggerMessage source generator for high-performance, AOT-compatible logging. Also use when the user mentions "LoggerMessage," "logging source generator," "high-performance logging," "optimize logging," "AOT logging," or "structured logging source gen." For full AOT analysis, see dotnet-aot-analysis.
dotnet-enable-testing-platform
Enables the Microsoft Testing Platform runner in global.json. Also use when the user mentions "testing platform," "Microsoft.Testing.Platform," "new test runner," "migrate test runner," "global.json testing," or "modern test platform."
dotnet-json-polymorphic
Configures polymorphic JSON serialization with [JsonPolymorphic] and [JsonDerivedType] attributes. Also use when the user mentions "polymorphic JSON," "JsonDerivedType," "JSON inheritance," "type discriminator," "serialize derived types," or "JSON polymorphism." For full JSON source generation, see dotnet-source-gen-json.
competitor-alternatives
When the user wants to create competitor comparison or alternative pages for SEO and sales enablement. Also use when the user mentions 'alternative page,' 'vs page,' 'competitor comparison,' 'comparison page,' '[Product] vs [Product],' '[Product] alternative,' or 'competitive landing pages.' Covers four formats: singular alternative, plural alternatives, you vs competitor, and competitor vs competitor. Emphasizes deep research, modular content architecture, and varied section types beyond feature tables.
flutter-duit-bdui
Integrate Duit framework into Flutter applications including setup, driver configuration, HTTP/WebSocket transports, custom widgets, and themes. Use when integrating backend-driven UI, configuring Duit, or adding Duit to Flutter applications.
Didn't find tool you were looking for?