askill
dotnet-source-gen-options-validation

dotnet-source-gen-options-validationSafety 95Repository

Converts options validation to use the compile-time source generator. Use when the user wants AOT-compatible, reflection-free options validation.

0 stars
1.2k downloads
Updated 2/4/2026

Package Files

Loading files...
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
  • Source generation happens at compile time; value validation runs at startup
  • Migrating from ValidateDataAnnotations() to source-generated validation

Steps

  1. Find options classes with data annotations:

    • Search for classes using [Required], [Range], [RegularExpression], [MaxLength], [MinLength], [Length]
    • These are typically in files matching *Options.cs or *Settings.cs
  2. Check existing validation setup:

    • If class already has a corresponding [OptionsValidator] partial class, skip it
    • Note any existing ValidateDataAnnotations() calls for removal later
  3. For each options class, create a partial validator class:

    [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
  4. Register the validator in DI:

    services.AddSingleton<IValidateOptions<MyOptions>, ValidateMyOptions>();
    
    • Replace any existing ValidateDataAnnotations() calls
    • Ensure Microsoft.Extensions.Options using directive is present
  5. Verify with build:

    dotnet build
    
  6. If build fails, check for:

    • Missing partial keyword on validator class
    • Unsupported validation attributes
    • Missing package reference to Microsoft.Extensions.Options
  7. 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.Options version 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

Install

Download ZIP
Requires askill CLI v1.0+

AI Quality Score

88/100Analyzed 2/24/2026

High-quality technical skill for .NET developers. Provides comprehensive, step-by-step guidance for migrating options validation to compile-time source generators. Well-structured with clear when-to-use criteria, code examples, troubleshooting, and supported attributes. Specific to .NET but highly reusable within that domain. Only minor gaps in advanced scenarios.

95
90
75
85
90

Metadata

Licenseunknown
Version-
Updated2/4/2026
PublisherIm5tu

Tags

ci-cd