Immediate.Validations is a source generator validating
Immediate.Handlers
handler parameters.
You can install Immediate.Validations with NuGet:
Install-Package Immediate.Validations
Or via the .NET Core command line interface:
dotnet add package Immediate.Validations
Either command, from Package Manager Console or .NET Core CLI, will download and install Immediate.Validations.
Add Immediate.Validations to the Immediate.Handlers behaviors pipeline by including it in the list of default Behaviors for the assembly:
using Immediate.Validations.Shared;
[assembly: Behaviors(
typeof(ValidationBehavior<,>)
)]
Indicate that a class should be validated by adding the [Validate]
attribute and IValidationTarget<>
interface:
[Validate]
public record Query : IValidationTarget<Query>;
When Nullable Reference Types is enabled, any non-nullable reference types are automatically checked for null
. Other
validations are available like so:
[Validate]
public record Query : IValidationTarget<Query>
{
[GreaterThan(0)]
public required int Id { get; init; }
}
The result of doing the above is that when a parameter fails one or more validations, a ValidationException
is thrown,
which can be handled via ProblemDetails or any other infrastructure mechanism.