Koto.Validation 0.5.0-preview.5

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

Koto.Validation

FluentValidation extensions and the validation pipeline behavior for the Koto suite.

Uses FluentValidation v12 (Apache 2.0 on all versions), pinned to [12.0.0,13.0.0).

Install

dotnet add package Koto.Validation

What's included

Type Purpose
KotoValidators.MustBeValueObject<T, TSource, TValueObject> Validates a property by calling a domain factory (Func<TSource, Result<TVO>>) — any source type
KotoValidators.MustBeValueObjectOrNull<T, TSource, TValueObject> The same for a value that may be absent: null is fine, a value that is there must pass the factory
KotoValidators.MustBePresent The request must carry this value; a missing one is general.value.is-required — for what is not a value object, such as a list
KotoValidators.ListMustContainNumberOfItems Min/max count rule with Errors.General.CollectionIsTooSmall/Large
ValidationBehavior<TRequest, TResponse> Runs all validators for the request sequentially before the handler
ValidationRuleFailed The error for rules not backed by a domain factory (validation.rule.failed, details.rule = the rule's code)
AddValidationBehavior() Puts the behavior at an explicit position in KotoApplicationOptions
AddKotoValidation(assemblies) Scans IValidator<T> implementations (registers the behavior if it was not added through the options)

Register

builder.Services.AddKotoApplication(
    o => o.AddLoggingBehavior().AddValidationBehavior().AddTransactionBehavior(),  // Logging → Validation → Transaction
    typeof(PlaceOrderHandler).Assembly);
builder.Services.AddKotoValidation(typeof(PlaceOrderValidator).Assembly);

With this order a validation failure never opens a transaction. Calling AddKotoValidation alone still works — the behavior is then appended after the behaviors configured in the options.

Write a validator — one source of truth

The validation logic lives in the domain factory; the validator only calls it:

public sealed class PlaceOrderValidator : AbstractValidator<PlaceOrderCommand>
{
    public PlaceOrderValidator()
    {
        RuleFor(x => x.Email).MustBeValueObject(Email.Create);      // string source
        RuleFor(x => x.Quantity).MustBeValueObject(Quantity.Create); // int source — any type works
        RuleFor(x => x.Items).ListMustContainNumberOfItems(1, 100);
        RuleFor(x => x.CustomerId).NotEmpty();                       // plain FluentValidation rule
    }
}

Validators are keyed by the concrete command type and discovered by the pipeline automatically.

What the handler never sees, and what the client gets

On failure the behavior returns an AppResult carrying one EnrichedError per failure, each bound to its field:

  • a MustBeValueObject failure keeps the domain error — identity.email.invalid, … — with its record properties as details;
  • a required field that the request left out is general.value.is-required: the factory is not asked about a value that is not there (see below);
  • any other rule (NotEmpty(), Length(), MustAsync()) becomes validation.rule.failed with details.rule set to the rule's error code (NotEmptyValidator or your WithErrorCode value).

Nothing is collapsed into a string; the API layer renders every error inside the response envelope.

Validators run one after another, so async rules may share scoped services such as a DbContext.

A factory parses, the request requires

Email.Create answers one question: is this string an email? That an email is needed at all is a rule of the request, not of Email, so a factory takes a non-nullable value and has no "required" error. The validator says both things in the open — what must be there, and what may be absent:

public sealed record RegisterCustomerCommand(string Email, string? Phone, int? Age) : ICommand<Guid>;

RuleFor(x => x.Email).MustBeValueObject(Email.Create);         // must be there, and be an email
RuleFor(x => x.Phone).MustBeValueObjectOrNull(Phone.Create);   // may be absent; when it is there it must be a phone
RuleFor(x => x.Age).MustBeValueObjectOrNull(Age.Create);       // the same for a number
The request sent MustBeValueObject MustBeValueObjectOrNull
nothing (null) general.value.is-required fine
"" or anything the factory refuses the factory's error the factory's error
a valid value fine fine

A nullable type alone does not make a value optional: a missing Phone under MustBeValueObject is general.value.is-required. Nothing about optionality is hidden — not in the domain, not in the library: the name of the rule says it.

What is not a value object — a list of lines, a nested object — is required the same way, with the same code:

RuleFor(x => x.Lines).MustBePresent();   // missing → general.value.is-required; an empty list is the order's rule

ListMustContainNumberOfItems does not count a missing list; say it must be there with MustBePresent().

In the handler an optional value is parsed only when it is there:

var phone = command.Phone is null ? null : Phone.Create(command.Phone);
if (phone is { IsFailure: true }) return phone.Errors.ForField(nameof(command.Phone));

var customer = Customer.Register(email.Value, phone?.Value);   // Register(Email email, Phone? phone)

Upgrading from 0.4

0.5 adds AddValidationBehavior() to position the behavior explicitly, removes MustBeEntity, NotEmptyWithKotoError and LengthWithKotoError, runs validators sequentially, and reports plain rules as validation.rule.failed (details.rule) instead of the FluentValidation error code. The step-by-step guide for the whole suite, with before/after code, is docs/migrations/0.4-to-0.5.md.

Product Compatible and additional computed target framework versions.
.NET 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 (1)

Showing the top 1 NuGet packages that depend on Koto.Validation:

Package Downloads
Koto.Api.FastEndpoints

FastEndpoints integration for Koto: CQRS base endpoints responding with the Koto Envelope, binding errors in the same envelope, correlation ID middleware, and global exception handler.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.5.0-preview.9 50 11/30/2026
0.5.0-preview.8 60 11/30/2026
0.5.0-preview.7 57 11/29/2026
0.5.0-preview.6 49 11/29/2026
0.5.0-preview.5 56 11/29/2026
0.5.0-preview.4 56 11/29/2026
0.5.0-preview.3 51 11/29/2026
0.5.0-preview.2 59 11/29/2026
0.5.0-preview.1 64 11/8/2026
0.4.0 177 7/21/2026
0.3.0-preview.15 83 7/21/2026
0.3.0-preview.14 85 7/21/2026
0.3.0-preview.13 83 7/21/2026
0.3.0-preview.12 75 7/21/2026
0.3.0-preview.11 76 7/21/2026
0.3.0-preview.10 77 7/21/2026
0.3.0-preview.9 88 7/21/2026
0.3.0-preview.8 79 7/21/2026
0.3.0-preview.7 89 7/21/2026
0.3.0-preview.6 85 7/20/2026
Loading failed

0.5.0-preview.5 — bug fix: ListMustContainNumberOfItems threw on a missing list (HTTP 500); new rule MustBePresent() reports a missing value that is not a value object as general.value.is-required, bound to the field. Since preview.4: a value object's factory only parses, MustBeValueObjectOrNull for optional values; analyzers KOTO001/KOTO002. Migration guide: https://github.com/arigatory/Koto/blob/main/docs/migrations/0.4-to-0.5.md