PouryaKit
ReferencePackages

PouryaKit.Validation

FluentValidation validators for commands and queries, run as one step of the Kit's fixed pipeline.

FluentValidation validators for commands and queries, run as one step of the Kit's fixed pipeline. Each validator sits beside its Feature, and every failure of every validator comes back as one failed Result, its Errors naming the input they are about, before the handler runs.

Install

dotnet add package PouryaKit.Validation --prerelease

When to use it

  • A command's or query's input needs checking before its handler runs.
  • A paged query must refuse a size, sort or filter it did not declare.

It plugs into PouryaKit.Cqrs, whose pipeline runs it, and PouryaKit.Web answers its Errors in a problem response, each naming its input.

Quick start

Register the Module's validators in its Composition Root:

module.AddPouryaKitValidation();

Write one validator per command or query that needs input checks, beside it in the Feature, as the Proving Sample does:

internal sealed class CreateProductCommandValidator : AbstractValidator<CreateProductCommand>
{
    public CreateProductCommandValidator()
    {
        RuleFor(command => command.ProductId).NotEmpty();
        RuleFor(command => command.CategoryId).NotEmpty();
        RuleFor(command => command.Name).NotEmpty().MaximumLength(200);
    }
}

What you get

  • One validation step, in its fixed place in the pipeline, so a handler sees only input that passed.
  • Every failure at once, as Errors in the Result; nothing throws.
  • Validators found in the Module's own assembly, internal ones included.
  • Rules for a Page's size, sort and filter.

Validate the input's shape here. A business rule that depends on state belongs in the aggregate, where it returns an Error.

Learn more

On this page