StaticNorth.Valiant.Validation.Mediator 1.1.0

dotnet add package StaticNorth.Valiant.Validation.Mediator --version 1.1.0
                    
NuGet\Install-Package StaticNorth.Valiant.Validation.Mediator -Version 1.1.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="StaticNorth.Valiant.Validation.Mediator" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="StaticNorth.Valiant.Validation.Mediator" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="StaticNorth.Valiant.Validation.Mediator" />
                    
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 StaticNorth.Valiant.Validation.Mediator --version 1.1.0
                    
#r "nuget: StaticNorth.Valiant.Validation.Mediator, 1.1.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 StaticNorth.Valiant.Validation.Mediator@1.1.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=StaticNorth.Valiant.Validation.Mediator&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=StaticNorth.Valiant.Validation.Mediator&version=1.1.0
                    
Install as a Cake Tool

Valiant Validation

Valiant Validation is a source-generated validation library for .NET. It keeps validation authoring small and explicit while generating direct runtime code that is friendly to trimming, Native AOT, and dependency injection without reflection-based assembly scanning.

Packages

  • StaticNorth.Valiant.Validation provides the runtime API and source generator.
  • StaticNorth.Valiant.Validation.AspNetCore adds Minimal API, MVC, and problem-details integration.
  • StaticNorth.Valiant.Validation.AspNetCore.OpenApi adds native ASP.NET Core OpenAPI schema integration.
  • StaticNorth.Valiant.Validation.MediatR adds a MediatR pipeline behavior. It intentionally pins MediatR to 12.5.0, the final free MediatR release.
  • StaticNorth.Valiant.Validation.Mediator adds the equivalent behavior for the source-generated Mediator package.

Install the most specific package your application uses. Integration packages forward StaticNorth.Valiant.Validation runtime, build, and analyzer assets, so a separate StaticNorth.Valiant.Validation package reference is not required for validator generation. Reference multiple integration packages when using multiple integrations, such as ASP.NET Core validation and OpenAPI.

Quick Start

using StaticNorth.Valiant.Validation;

[ValiantValidator]
public sealed partial class CreateUserValidator : ValidatorDefinition<CreateUser>
{
    public override void Configure(ValidationContext<CreateUser> context)
    {
        context.Property(user => user.Name)
            .Required()
            .MaxLength(64);

        context.Property(user => user.Age)
            .GreaterThanOrEqualTo(18);
    }
}

The generator emits an IValidator<CreateUser> implementation with direct property reads and native branch checks. When Microsoft.Extensions.DependencyInjection is available, it also emits AddValiantValidators(...):

services.AddValiantValidators(options =>
{
    options.Mode = ValiantValidationMode.Auto;
    options.Lifetime = ValiantValidatorLifetime.Scoped;
    options.FailureBehavior = ValiantFailureBehavior.ValidateAll;
});

Validation Status

Use Validate(...) or ValidateAsync(...) when callers need failure codes, messages, property names, and severity partitions. In Fast mode, generated validators build that diagnostic result lazily and avoid allocating a result builder for valid inputs.

Use IsValid(...) or IsValidAsync(...) when callers only need the validity status. The generated Fast path evaluates rule conditions, predicates, severities, and reused validators directly without constructing failure objects. Because diagnostics are not requested, failure code, message, and property-name expressions are not evaluated. Warning-only results are valid; errors and unknown severity values are invalid.

Debuggable mode keeps the materialized runtime path so user-authored configuration and diagnostic expressions remain available to the debugger.

Fluent Rules

Validators inherit ValidatorDefinition<T> and override Configure to declare rules through ValidationContext<T>:

public override void Configure(ValidationContext<CreateUser> context)
{
    if (context.Instance.RequiresInvite)
    {
        context.Property(user => user.InviteCode).Required();
    }

    context.Property(user => user.Email)
        .Required()
        .MaxLength(254);
}

In Debuggable mode, the user-authored Configure method executes at runtime, so breakpoints in native if, else, and switch statements work normally.

Attribute Rules

Validators can also derive from ValidatorDefinition<T> when model attributes should define the default rules:

using StaticNorth.Valiant.Validation;

[ValiantValidator]
public sealed partial class CreateUserValidator : ValidatorDefinition<CreateUser>;

public sealed class CreateUser
{
    [Required("NameRequired", "Name is required.")]
    [MaxLength(64)]
    public string? Name { get; init; }
}

Validation attributes are Valiant attributes, not System.ComponentModel.DataAnnotations. They use the same rule names and generated logic as fluent rules.

Reuse

Whole-model validators can be reused from another validator:

public void Configure(ValidationContext<CreateUser> context)
{
    context.UseValidator(_nameValidator);
    context.Property(user => user.Age).GreaterThanOrEqualTo(18);
}

Property validators derive from PropertyValidatorDefinition<TProperty> and can be applied to property values:

public void Configure(ValidationContext<CreateUser> context)
{
    context.Property(user => user.Address).UseValidator(_addressValidator);
}

Property validators skip null property values. Add Required() before UseValidator(...) when the property itself must be present.

ASP.NET Core

StaticNorth.Valiant.Validation.AspNetCore integrates generated validators with Minimal APIs, MVC, and validation problem responses.

builder.Services.AddValiantValidators();
builder.Services.AddValiantMvcValidation();

StaticNorth.Valiant.Validation.AspNetCore.OpenApi maps generated validation metadata into native ASP.NET Core OpenAPI schema constraints and x-valiant-rules extensions:

builder.Services.AddValiantValidators();
builder.Services.AddValiantOpenApi();

Mediator

StaticNorth.Valiant.Validation.MediatR and StaticNorth.Valiant.Validation.Mediator add pipeline behavior integration:

services.AddMediatR(configuration =>
{
    configuration.AddValiantValidation();
});
services.AddMediator(options =>
{
    options.Assemblies = [typeof(CreateUserCommand).Assembly];
});

services.AddValiantMediatorValidation();

For NativeAOT, configure the source-generated Mediator pipeline directly:

services.AddMediator(options =>
{
    options.Assemblies = [typeof(CreateUserCommand).Assembly];
    options.PipelineBehaviors = [typeof(ValiantValidationBehavior<,>)];
});

Use one Mediator registration path for the Valiant behavior. Do not also call AddValiantMediatorValidation() when ValiantValidationBehavior<,> is listed in PipelineBehaviors.

Benchmarks

The standalone StaticNorth.Valiant.Validation.Benchmarks project measures the generated Fast path for valid, one-failure, and all-failure inputs, with Validly as a pinned relative control. Its README documents correctness checks and the repeatable before/after workflow.

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 was computed.  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 was computed.  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
1.1.0 111 7/30/2026
1.0.1 99 7/26/2026