Masterly.NonEmptyList.AspNetCore
3.0.0
See the version list below for details.
dotnet add package Masterly.NonEmptyList.AspNetCore --version 3.0.0
NuGet\Install-Package Masterly.NonEmptyList.AspNetCore -Version 3.0.0
<PackageReference Include="Masterly.NonEmptyList.AspNetCore" Version="3.0.0" />
<PackageVersion Include="Masterly.NonEmptyList.AspNetCore" Version="3.0.0" />
<PackageReference Include="Masterly.NonEmptyList.AspNetCore" />
paket add Masterly.NonEmptyList.AspNetCore --version 3.0.0
#r "nuget: Masterly.NonEmptyList.AspNetCore, 3.0.0"
#:package Masterly.NonEmptyList.AspNetCore@3.0.0
#addin nuget:?package=Masterly.NonEmptyList.AspNetCore&version=3.0.0
#tool nuget:?package=Masterly.NonEmptyList.AspNetCore&version=3.0.0
Masterly.NonEmptyList.AspNetCore
ASP.NET Core integration for Masterly.NonEmptyList: the non-empty guarantee shows up in your OpenAPI document, and Validation<T, ValidationError> turns into RFC 7807 problem details in one call.
Installation
dotnet add package Masterly.NonEmptyList.AspNetCore
Binding works without this package
NonEmptyList<T>, NonEmptySet<T> and NonEmptyString have a static TryParse, and NonEmptyList<T> and NonEmptyString also ship a TypeConverter, so minimal APIs and MVC bind them from query strings and route values out of the box. An empty value is a 400 before your handler runs, and an empty JSON array in a request body is a 400 as well.
app.MapGet("/products", (NonEmptyList<int> ids) => repo.GetByIds(ids)); // ?ids=1,2,3
app.MapGet("/hello/{name}", (NonEmptyString name) => $"Hello {name}");
OpenAPI (.NET 9 and later)
builder.Services.AddOpenApi(options => options.AddNonEmptySchemas());
| Type | Schema |
|---|---|
NonEmptyList<T>, ImmutableNonEmptyList<T> |
type: array, minItems: 1, plus items: T on .NET 10 |
NonEmptySet<T> |
minItems: 1, uniqueItems: true |
ConstrainedList<T, AtLeast<Two>> and friends |
minItems / maxItems / uniqueItems derived from the constraint |
NonEmptyDictionary<TKey, TValue> |
type: object, minProperties: 1, plus additionalProperties on .NET 10 |
NonEmptyString |
type: string, minLength: 1 |
[NonEmptyCollection] classes |
bounds from MinCount, MaxCount, IsDistinct |
Validation to HTTP
app.MapPost("/orders", (CreateOrder request) =>
Validation.Combine(
Validation.NotBlank(request.Customer, new ValidationError("required", "Customer is required", "customer")),
Validation.Ensure(request.Lines, l => l.All(x => x.Length > 0), new ValidationError("blank", "Lines cannot be blank", "lines")),
(customer, lines) => new Order(customer, lines))
.ToResult()); // 200 with the order, or 400 problem details
{
"title": "One or more validation errors occurred.",
"status": 400,
"errors": { "customer": ["Customer is required"], "lines": ["Lines cannot be blank"] },
"codes": ["required", "blank"]
}
Also available: ToResult(onValid), ToCreatedResult(location), ToResultAsync, ToValidationProblem(), ToProblemDetails(), and for MVC ToActionResult(), ModelState.AddErrors(errors) and validation.TryGetValue(ModelState, out value).
NonEmptyShape.Of(type) exposes the cardinality a type guarantees (minimum, maximum, uniqueness) for your own tooling.
Documentation
Full guide: ASP.NET Core.
License
MIT
| 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 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. |
-
net10.0
- Masterly.NonEmptyList (>= 3.0.0)
- Microsoft.AspNetCore.OpenApi (>= 10.0.2)
- Microsoft.OpenApi (>= 2.13.0)
-
net8.0
- Masterly.NonEmptyList (>= 3.0.0)
-
net9.0
- Masterly.NonEmptyList (>= 3.0.0)
- Microsoft.AspNetCore.OpenApi (>= 9.0.11)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
v3.0.0:
- Initial release: AddNonEmptySchemas() OpenAPI transformer (.NET 9+), Validation.ToResult / ToValidationProblem, ModelState.AddErrors