🎯

dotnet-source-gen-options-validation

🎯Skill

from im5tu/dotnet-skills

VibeIndex|
What it does

Converts options validation to compile-time source generator for AOT-compatible, reflection-free validation at startup.

πŸ“¦

Part of

im5tu/dotnet-skills(10 items)

dotnet-source-gen-options-validation

Installation

Quick InstallInstall with npx
npx skills add im5tu/dotnet-skills
πŸ“– Extracted from docs: im5tu/dotnet-skills
1Installs
-
AddedFeb 4, 2026

Skill Details

SKILL.md

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

Overview

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

  1. Check existing validation setup:

- If class already has a corresponding [OptionsValidator] partial class, skip it

- Note any existing ValidateDataAnnotations() calls for removal later

  1. 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

  1. Register the validator in DI:

```csharp

services.AddSingleton, ValidateMyOptions>();

```

- Replace any existing ValidateDataAnnotations() calls

- Ensure Microsoft.Extensions.Options using directive is present

  1. Verify with build:

```bash

dotnet build

```

  1. If build fails, check for:

- Missing partial keyword on validator class

- Unsupported validation attributes

- Missing package reference to Microsoft.Extensions.Options

  1. 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

More from this repository9

🎯
dotnet-enable-testing-platform🎯Skill

Enables and configures testing platforms and frameworks for .NET projects, streamlining test setup and infrastructure.

🎯
dotnet-source-gen-logging🎯Skill

Transforms .NET logging calls to use LoggerMessage source generator for high-performance, AOT-compatible logging with zero boxing overhead.

🎯
dotnet-json-polymorphic🎯Skill

Converts JSON payloads with polymorphic type handling, enabling dynamic deserialization of complex .NET object hierarchies.

🎯
dotnet-source-gen-json🎯Skill

Generates JSON serialization and deserialization code for .NET classes using source generators, improving performance and reducing boilerplate.

🎯
dotnet-source-gen-regex🎯Skill

Converts .NET regex instances to compile-time source-generated implementations for improved performance and AOT compatibility.

🎯
dotnet-centralise-packages🎯Skill

Centralizes NuGet package versions across .NET projects by creating a single `Directory.Packages.props` file and updating project references.

🎯
dotnet-enable-autocomplete🎯Skill

Enables dotnet CLI tab autocomplete across multiple shells, automatically detecting .NET version and configuring shell-specific completion scripts.

🎯
dotnet-update-packages🎯Skill

Automatically lists and updates outdated NuGet packages in .NET projects, guiding users through dependency management with interactive confirmation.

🎯
dotnet-aot-analysis🎯Skill

Analyzes and configures .NET projects for Native AOT compatibility, applying source generators and optimizing AOT deployment settings.