im5tu/dotnet-skills · Archived

dotnet-source-gen-options-validation

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

First seen Feb 4, 2026

Installation

$ npx skills add im5tu/dotnet-skills --skill dotnet-source-gen-options-validation

Stronger alternatives

This repository is archived — consider an actively maintained alternative.

Similar popular skills

Related neighbors and high-traction skills in the same topics — useful to compare before installing.

Also in this package

Other skills from im5tu/dotnet-skills.

npx skills add im5tu/dotnet-skills

Browse all from im5tu/dotnet-skills

More details

Agent compatibility

Declared targets from SKILL.md / docs. Unmarked agents are not listed — the skill may still install via the CLI.

Claude Code Not declared
Cursor Not declared
Codex Not declared
GitHub Copilot Not declared
Windsurf Not declared
Gemini CLI Not declared
Cline Not declared
OpenCode Not declared

Repository health

Stars 1
License MIT
Default branch main
Open issues 0
Status Archived

Skill metadata

Parsed from SKILL.md frontmatter.

Version1.0
LicenseMIT
Allowed toolsBash(dotnet:*) Read Glob Grep AskUserQuestion
More metadata
author
Im5tu
version
1.0
repositoryUrl
https://github.com/im5tu/dotnet-skills

Package contents

Files included with this skill beyond the listing page.

  • skill md SKILL.md 2,944 B
  • docs SUMMARY.md 189 B

History

  1. First seen on skills.sh
  2. First recorded snapshot · 3 installs

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

  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<IValidateOptions<MyOptions>, 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