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
<PackageReference Include="StaticNorth.Valiant.Validation.Mediator" Version="1.1.0" />
<PackageVersion Include="StaticNorth.Valiant.Validation.Mediator" Version="1.1.0" />
<PackageReference Include="StaticNorth.Valiant.Validation.Mediator" />
paket add StaticNorth.Valiant.Validation.Mediator --version 1.1.0
#r "nuget: StaticNorth.Valiant.Validation.Mediator, 1.1.0"
#:package StaticNorth.Valiant.Validation.Mediator@1.1.0
#addin nuget:?package=StaticNorth.Valiant.Validation.Mediator&version=1.1.0
#tool nuget:?package=StaticNorth.Valiant.Validation.Mediator&version=1.1.0
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.Validationprovides the runtime API and source generator.StaticNorth.Valiant.Validation.AspNetCoreadds Minimal API, MVC, and problem-details integration.StaticNorth.Valiant.Validation.AspNetCore.OpenApiadds native ASP.NET Core OpenAPI schema integration.StaticNorth.Valiant.Validation.MediatRadds a MediatR pipeline behavior. It intentionally pins MediatR to12.5.0, the final free MediatR release.StaticNorth.Valiant.Validation.Mediatoradds 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 | 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 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. |
-
net8.0
- Mediator.Abstractions (>= 3.0.2)
- StaticNorth.Valiant.Validation (>= 1.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.