ExpressValidator.Extensions.ValidationOnStart
0.1.0-preview
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
<PackageReference Include="ExpressValidator.Extensions.ValidationOnStart" Version="0.1.0-preview" />
<PackageVersion Include="ExpressValidator.Extensions.ValidationOnStart" Version="0.1.0-preview" />
<PackageReference Include="ExpressValidator.Extensions.ValidationOnStart" />
paket add ExpressValidator.Extensions.ValidationOnStart --version 0.1.0-preview
#r "nuget: ExpressValidator.Extensions.ValidationOnStart, 0.1.0-preview"
#:package ExpressValidator.Extensions.ValidationOnStart@0.1.0-preview
#addin nuget:?package=ExpressValidator.Extensions.ValidationOnStart&version=0.1.0-preview&prerelease
#tool nuget:?package=ExpressValidator.Extensions.ValidationOnStart&version=0.1.0-preview&prerelease
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 MicrosoftOptionspipeline - Configurable validation mode - stop on first error or continue
- Detailed error reporting - with property names
- No
AbstractValidatorrequired – 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 | Versions 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. |
-
net8.0
- ExpressValidator (>= 0.12.2)
- Microsoft.Extensions.DependencyInjection (>= 10.0.1)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.1)
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 |