ReHackt.Extensions.Options.Validation 8.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package ReHackt.Extensions.Options.Validation --version 8.0.3                
NuGet\Install-Package ReHackt.Extensions.Options.Validation -Version 8.0.3                
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="ReHackt.Extensions.Options.Validation" Version="8.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ReHackt.Extensions.Options.Validation --version 8.0.3                
#r "nuget: ReHackt.Extensions.Options.Validation, 8.0.3"                
#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.
// Install ReHackt.Extensions.Options.Validation as a Cake Addin
#addin nuget:?package=ReHackt.Extensions.Options.Validation&version=8.0.3

// Install ReHackt.Extensions.Options.Validation as a Cake Tool
#tool nuget:?package=ReHackt.Extensions.Options.Validation&version=8.0.3                

ReHackt.Extensions.Options.Validation

Extends OptionsBuilder<T> and IServiceCollection with nested data annotations validation and eager validation on start.

Get started

Get it on <a href="https://www.nuget.org/packages/ReHackt.Extensions.Options.Validation"><img src="https://www.nuget.org/Content/gallery/img/default-package-icon.svg" height=18 style="height:18px;" /> NuGet</a>

TL;DR

  1. Create your options class(es)
  2. Decorate your options with data annotations
  3. Call ConfigureAndValidate<T>(Action<T> configureOptions) on your IServiceCollection

ConfigureAndValidate will configure your options (calling the base Configure method), but will also check that the built configuration respects the data annotations, otherwise an OptionsValidationException (with details) is thrown as soon as the application is started. No misconfiguration surprise at runtime!

Example

// Startup configuration

public void ConfigureServices(IServiceCollection services)
{
    services.ConfigureAndValidate<ApplicationOptions>(options => _configuration.Bind(options));
    
    ...
}


// Options classes

public class ApplicationOptions
{
    [Required]
    public EmailOptions Email { get; set; }

    public SecurityOptions Security { get; set; }

    ...
}

public class EmailOptions
{
    [Required]
    public string Host { get; set; }

    [Required]
    public int Port { get; set; }

    public bool UseSsl { get; set; }

    ...
}

public class SecurityOptions
{
    [Range(8, int.MaxValue, ErrorMessage = "The {0} must be greater than {1}.")]
    public int PasswordMinLength { get; set; } = 8;

    ...
}

Use

ServiceCollection extension

ConfigureAndValidate
services.ConfigureAndValidate<TOptions>(configureOptions)

Is syntactic sugar for

services
    .AddOptions<TOptions>()
        .Configure(configureOptions)
        .ValidateDataAnnotationsRecursively()
        .ValidateOnStart()   // or ValidateEagerly()
        .Services

OptionsBuilder extensions

ValidateDataAnnotationsRecursively

This method register this options instance for validation of its DataAnnotations at the first dependency injection. Nested objects are supported.

ValidateOnStart (or ValidateEagerly in previous versions)

This method validates this options instance at application startup rather than at the first dependency injection.

ConfigureAndValidate
optionsBuilder.ConfigureAndValidate<TOptions>(configureOptions)

Is syntactic sugar for

optionsBuilder
    .Configure(configureOptions)
    .ValidateDataAnnotationsRecursively()
    .ValidateOnStart()   // or ValidateEagerly()
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (6)

Showing the top 5 NuGet packages that depend on ReHackt.Extensions.Options.Validation:

Package Downloads
ReHackt.AspNetCore.Authentication

Package Description

ReHackt.RazorEmails

Provides an email layout to create email templates with Razor and tag helpers, and a service to send emails using SMTP.

ReHackt.Email

Provides an email layout to create email templates with Razor and tag helpers, and a service abstraction to send emails.

ReHackt.AspNetCore.Builder

Package Description

Quantoz.Nexus.Sdk.Shared

Shared files used by sdk's for interacting with the Quantoz Nexus environment.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
9.0.0 683 11/12/2024
8.0.3 8,989 10/9/2024
8.0.2 120,758 2/15/2024
8.0.1 10,985 1/18/2024
8.0.0 788 1/18/2024
7.0.1 160,498 4/17/2023
7.0.0 55,750 12/2/2022
6.0.3 371,303 8/24/2022
6.0.2 2,212 8/10/2022
6.0.1 73,344 12/30/2021
6.0.0 73,522 11/27/2021
5.0.0 32,632 11/11/2020
3.1.9 11,818 10/15/2020
3.1.8 552 9/14/2020
3.1.7 486 9/14/2020
3.1.6 485 9/14/2020
3.1.5 503 9/14/2020
3.1.4 492 9/14/2020
3.1.3 495 9/14/2020
3.1.2 503 9/14/2020
3.1.1 492 9/14/2020
3.1.0 789 9/14/2020