Koto.Validation
0.5.0-preview.9
dotnet add package Koto.Validation --version 0.5.0-preview.9
NuGet\Install-Package Koto.Validation -Version 0.5.0-preview.9
<PackageReference Include="Koto.Validation" Version="0.5.0-preview.9" />
<PackageVersion Include="Koto.Validation" Version="0.5.0-preview.9" />
<PackageReference Include="Koto.Validation" />
paket add Koto.Validation --version 0.5.0-preview.9
#r "nuget: Koto.Validation, 0.5.0-preview.9"
#:package Koto.Validation@0.5.0-preview.9
#addin nuget:?package=Koto.Validation&version=0.5.0-preview.9&prerelease
#tool nuget:?package=Koto.Validation&version=0.5.0-preview.9&prerelease
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(PlaceOrderHandler).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 InviteCustomersValidator : AbstractValidator<InviteCustomersCommand>
{
public InviteCustomersValidator()
{
RuleForEach(x => x.Emails).MustBeValueObject(Email.Create); // what an email is, the domain says
RuleFor(x => x.Emails).ListMustContainNumberOfItems(1, 100); // how many one request may carry
RuleFor(x => x.Bonus).MustBeValueObject(Coins.Create); // int source — any type works
RuleFor(x => x.InvitedBy).NotEmpty(); // plain FluentValidation rule
}
}
A limit in a validator is a limit of the request. A rule of the business (an order holds at most 20 lines) lives in the aggregate and is not repeated here as a number.
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
MustBeValueObjectfailure keeps the domain error —identity.email.invalid, … — with its record properties asdetails; - 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()) becomesvalidation.rule.failedwithdetails.ruleset to the rule's error code (NotEmptyValidatoror yourWithErrorCodevalue).
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 registered = Customer.Register(email.Value, phone?.Value); // Register(Email email, Phone? phone)
if (registered.IsFailure) return registered.Errors;
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 | Versions 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. |
-
net10.0
- FluentValidation (>= 12.0.0 && < 13.0.0)
- Koto.Application (>= 0.5.0-preview.9)
- Koto.Domain (>= 0.5.0-preview.9)
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 |
0.5.0-preview.9 — bug fix: a Wolverine handler in a Marten service that takes IEventSourcedRepository or IUnitOfWork failed with InvalidServiceLocationException; AddKotoMarten now has Wolverine resolve Marten's sessions from the handler's scope, so the repository and the unit of work share one session. Since preview.8: AddKotoEFCoreWithoutOutbox, route-only commands without a body. Migration guide: https://github.com/arigatory/Koto/blob/main/docs/migrations/0.4-to-0.5.md