ExpressValidator.Extensions.ValidationOnStart 0.1.0-preview

This is a prerelease version of ExpressValidator.Extensions.ValidationOnStart.
There is a newer version of this package available.
See the version list below for details.
dotnet add package ExpressValidator.Extensions.ValidationOnStart --version 0.1.0-preview
                    
NuGet\Install-Package ExpressValidator.Extensions.ValidationOnStart -Version 0.1.0-preview
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ExpressValidator.Extensions.ValidationOnStart" Version="0.1.0-preview" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ExpressValidator.Extensions.ValidationOnStart" Version="0.1.0-preview" />
                    
Directory.Packages.props
<PackageReference Include="ExpressValidator.Extensions.ValidationOnStart" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ExpressValidator.Extensions.ValidationOnStart --version 0.1.0-preview
                    
#r "nuget: ExpressValidator.Extensions.ValidationOnStart, 0.1.0-preview"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ExpressValidator.Extensions.ValidationOnStart@0.1.0-preview
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ExpressValidator.Extensions.ValidationOnStart&version=0.1.0-preview&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=ExpressValidator.Extensions.ValidationOnStart&version=0.1.0-preview&prerelease
                    
Install as a Cake Tool

A lightweight library that brings expressive, fail-fast validation to .NET configuration options using the ExpressValidator library.

✨ Key Features

  • Fail-fast validation on startup with ValidateOnStart
  • Startup validation - catch configuration issues immediately when your app starts
  • Fluent API - express validation rules in a clean, readable way
  • Expressive syntax for property-level rules
  • Seamless integration with IValidateOptions<TOptions> and the Microsoft Options pipeline
  • Configurable validation mode - stop on first error or continue
  • Detailed error reporting - with property names
  • No AbstractValidator required – define rules inline, directly in your service registration

⚡ Quick Start

1. Define Your Options Class

public class MyOptions
{
	public int Option1 { get; set; }
	public int Option2 { get; set; }
}

public class MyOptions2
{
	public int Option3 { get; set; }
	public int Option4 { get; set; }
}

2. Configure Validation in Program.cs

var builder = WebApplication.CreateBuilder(args);

//// All validation in one place, no extra classes needed
builder.Services
	.AddOptionsWithExpressValidation<MyOptions>(
	(eb) =>
		eb.AddProperty(o => o.Option1)
		.WithValidation(o => o.GreaterThan(10))
		.AddProperty(o => o.Option2)
		.WithValidation(o => o.GreaterThan(50)),
			"MyOptions"); // Configuration section name
	.AddOptionsWithExpressValidation<MyOptions2>(
		(eb) =>
		eb.AddProperty(o => o.Option3)
		.WithValidation(o => o.GreaterThan(5))
		.AddProperty(o => o.Option4)
		.WithValidation(o => o.GreaterThan(7)), 
			"MyOptions2");

3. Handle Validation Errors at Startup

var loggerFactory = LoggerFactory.Create(lb => lb.AddConsole());
var logger = loggerFactory.CreateLogger<Program>();

try
{
	var app = builder.Build();
	app.MapGet("/", () => "Hello!");
	await app.RunAsync();
}
// Handles the standard exception thrown when a single set of options (e.g., MyOptions) fails validation.
catch (OptionsValidationException ove)
{
	foreach (var failure in ove.Failures)
	{
		logger.LogCritical(ove, failure);
	}
}
// Handles the case where validation errors from multiple option types (e.g., both MyOptions and MyOptions2)
catch (AggregateException ae) when (ae.InnerExceptions.All(e => e is OptionsValidationException))
{
	foreach (var failure in ae.Flatten().InnerExceptions)
	{
		logger.LogCritical(ae, "A critical exception occurred: {Message}", failure.Message);
	}
}
catch (Exception ex)
{
	logger.LogCritical(ex, "An unhandled exception occurred during application startup.");
}

🧩 How It Works

AddOptionsWithExpressValidation<TOptions>

Registers options, binds configuration, attaches express validation, and enables ValidateOnStart.

services.AddOptionsWithExpressValidation<MyOptions>(
	eb => eb.AddProperty(o => o.Option1).WithValidation(o => o.GreaterThan(10)),
	"MyOptions");

ExpressOptionsValidator<TOptions>

Implements IValidateOptions<TOptions> and runs the express validator against your options.

  • Converts validation results into ValidateOptionsResult
  • Produces per-property error messages
  • Supports short-circuit or collect-all failure modes

IValidateOptions<TOptions>

The standard Microsoft interface for options validation.

This ensures fail-fast startup validation and protects against misconfiguration.

🏆 Sample

See samples folder for concrete example.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.0 169 12/23/2025
0.2.0-preview 267 12/18/2025
0.1.0-preview 229 12/15/2025