Masterly.NonEmptyList.Configuration 3.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Masterly.NonEmptyList.Configuration --version 3.0.0
                    
NuGet\Install-Package Masterly.NonEmptyList.Configuration -Version 3.0.0
                    
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="Masterly.NonEmptyList.Configuration" Version="3.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Masterly.NonEmptyList.Configuration" Version="3.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Masterly.NonEmptyList.Configuration" />
                    
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 Masterly.NonEmptyList.Configuration --version 3.0.0
                    
#r "nuget: Masterly.NonEmptyList.Configuration, 3.0.0"
                    
#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 Masterly.NonEmptyList.Configuration@3.0.0
                    
#: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=Masterly.NonEmptyList.Configuration&version=3.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Masterly.NonEmptyList.Configuration&version=3.0.0
                    
Install as a Cake Tool

Masterly.NonEmptyList.Configuration

Configuration and Options binding for Masterly.NonEmptyList. A config array that must have at least one entry is checked once, when the host starts, and the failure names the key.

Installation

dotnet add package Masterly.NonEmptyList.Configuration

Why

The default binder cannot construct NonEmptyList<T> because the type has no parameterless constructor, and it would happily bind an empty array anyway. This package binds non-empty properties itself and delegates everything else to the default binder.

Usage

public sealed class SmtpOptions
{
    public required NonEmptyList<string> Hosts { get; init; }   // required: empty or missing fails at startup
    public NonEmptySet<string>? Tags { get; set; }               // optional: null when absent
    public NonEmptyString Sender { get; set; }                   // required: blank fails at startup
    public int Port { get; init; } = 25;                         // bound by the default binder
}

builder.Services.AddNonEmptyOptions<SmtpOptions>("Smtp");
// same as: AddOptions<SmtpOptions>().BindNonEmptyConfiguration("Smtp").ValidateOnStart()
{ "Smtp": { "Hosts": ["smtp1", "smtp2"], "Sender": "noreply@example.com" } }

An empty Hosts array stops the host with:

Configuration key 'Smtp:Hosts' must contain at least one element.

On .NET 8 the JSON provider drops an empty array altogether, so the key reads as missing and the message is Configuration key 'Smtp:Hosts' is required and must contain at least one element.

Both the array form (Hosts:0, Hosts:1) and a comma-separated scalar (Hosts=smtp1,smtp2, handy for environment variables) are accepted.

Every element and dictionary entry is read by this package, never skipped: Microsoft's binder would silently turn ["80", "eighty"] into [80]. A value that cannot be converted, a value that converts to null, a section where a single value is expected, and two dictionary keys that convert to the same key all throw InvalidOperationException naming the full key, for example Configuration key 'Smtp:Ports:1' contains 'eighty', which is not a valid Int32. Nested collections inside a non-empty collection are read with the same rules, and an object's own required non-empty members are checked. A JSON null element reads as an empty string on every target framework. Everything Microsoft's binder can create on its own (plain properties, plain collections, objects without non-empty members) is still bound by it, with its usual behaviour: constructor binding, [ConfigurationKeyName], get-only collections.

Direct reads

NonEmptyList<int> ports = configuration.GetNonEmptyList<int>("Smtp:Ports");
NonEmptyList<int>? optional = configuration.GetNonEmptyListOrNull<int>("Smtp:Ports");
NonEmptySet<string> tags = configuration.GetNonEmptySet<string>("Tags");
NonEmptyDictionary<string, int> limits = configuration.GetNonEmptyDictionary<string, int>("Limits");
ConstrainedList<int, AtLeast<Two>> pair = configuration.GetConstrainedList<int, AtLeast<Two>>("Pair");
NonEmptyString sender = configuration.GetNonEmptyString("Smtp:Sender");
SmtpOptions options = configuration.GetSection("Smtp").GetNonEmpty<SmtpOptions>();

Supported property types: NonEmptyList<T>, ImmutableNonEmptyList<T>, NonEmptySet<T>, NonEmptyDictionary<TKey, TValue>, ConstrainedList<T, TConstraint>, NonEmptyString, [NonEmptyCollection]-generated classes, and nested classes containing any of them. A property is treated as optional when its type is nullable (NonEmptyList<string>?) or it already has a value.

Documentation

Full guide: https://a7mdfre7at.github.io/Masterly.NonEmptyList/docs/configuration.

License

MIT

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 is compatible.  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 is compatible.  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
3.0.1 96 11/2/2026
3.0.0 89 11/2/2026

v3.0.0:
- Initial release: GetNonEmptyList / GetNonEmptySet / GetNonEmptyDictionary / GetNonEmptyString on IConfiguration
- BindNonEmptyConfiguration on OptionsBuilder: binds options classes containing non-empty collections; required ones fail at startup with the config key in the message
- NonEmptyConfigurationBinder for manual binding