ReValidator.Validation.Mvc 1.0.0

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

ReValidator.Validation.Mvc

ReValidator.Validation.Mvc is a lightweight, generic validation adapter for ASP.NET Core MVC, built on top of the ReValidator engine. It allows you to apply strongly-typed, reusable validators to controller parameters using a simple [Validate] attribute, with full support for dependency injection and dynamic rules.

This package is fully compatible with .NET 6, 7, 8, 9, and works seamlessly alongside MVC.


Features

  • Apply [Validate] to any controller parameter.
  • Leverages your existing IValidator<T> implementations.
  • Returns structured validation errors (RFC-7807 style).
  • Supports dynamic rule injection at runtime.
  • Optionally configure global MVC validation filter.
  • Compatible with MVC for consistent behavior.
  • Returns HTTP 422 Unprocessable Entity for failed validations.

Installation

Install via NuGet:

dotnet add package ReValidator.Validation.Mvc

Usage

  1. Add ReValidator services in Program.cs
var builder = WebApplication.CreateBuilder(args);

// Add core ReValidator services
builder.Services.AddReValidator();

// Add MVC support
builder.Services.AddControllers();
builder.Services.AddReValidatorMvc();

// Optional: add global filter
// or add the mentioned filter per controller's action
builder.Services.AddControllers(options =>
{
    options.Filters.Add<ReValidatorActionFilter>();
});

var app = builder.Build();
app.MapControllers();
app.Run();

  1. Create a validator
public sealed class PersonValidator : IValidator<Person>
{
    public ValidationResult Validate(Person model)
    {
        var result = new ValidationResult();

        if (string.IsNullOrWhiteSpace(model.Name))
            result.Errors.Add(new ValidationError
            {
                Rule = "FullNameRequired",
                Message = "The 'Name' property is required."
            });

        return result;
    }
}
  1. Apply [Validate] to controller parameters
[ApiController]
[Route("persons")]
public sealed class PersonsController : ControllerBase
{
    [HttpPost("create")]
    public IActionResult Create([Validate] Person person)
    {
        // This action will only execute
    }
}
  1. Dynamic rule injection
app.Services.ApplyReconfiguration(new DynamicReconfiguration
{
    RuleName = "FullNameRequired",
    PropertyName = "Name",
    ErrorMessage = "The \"{PropertyName}\" is required",
    Expression = "x => !string.IsNullOrWhiteSpace(x.Name)",
    FullPathToModel = typeof(Person).FullName
});

This allows you to inject or update rules at runtime, without recompiling validators.

  1. Validation response

When a validation fails, the API returns HTTP 422 with a structured payload:

{
  "type": "about:blank",
  "title": "Validation failed",
  "status": 422,
  "detail": "One or more validation rules were violated.",
  "errors": [
    {
      "rule": "FullNameRequired",
      "message": "The 'Name' property is required."
    }
  ]
}

Unit Testing

You can use Microsoft.AspNetCore.TestHost to test MVC endpoints:

using var app = await CreateApplication();
var client = app.GetTestClient();

var response = await client.PostAsJsonAsync("/persons/create", new Person());
Assert.Equal(422, (int)response.StatusCode);

Requirements

  • NET 6.0 or higher
  • ReValidator.Contracts
  • ASP.NET Core MVC (Abstractions + Core)
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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 is compatible.  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.0.0 100 1/12/2026