DataValidators 1.2.0

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

DataValidators

A small utility package for ASP.NET Core that automatically validates incoming request data. If a model is invalid, the action never runs — the client immediately gets a 400 Bad Request response with the validation errors instead. This isn't new tho — ASP.NET already supports this out of the box. What sets this library apart is that each attribute can define its own custom response for when validation fails.

Installation

dotnet add package DataValidators

Setup

In your Program.cs:

builder.Services.AddControllers(options =>
{
    options.Filters.Add<ValidationFilter>();
})
.AddJsonOptions(options =>
{
    options.JsonSerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonContext.Default);
});

That's it — the filter now runs automatically before every controller action.

Example

Model with some of the validation attributes:

public class RegisterUserRequest
{
    [Required("Username missing")]
    public string Username { get; init; }

    [Required("Name missing")]
    [MinLength(3, "Name too short")]
    [MaxLength(20, "Name too long")]
    public string Name { get; init; }
 
    [Range(18, 120, "Age not valid")]  -> (MinValue, MaxValue, What gets returned when validation fails)
    public int Age { get; init; }

    [Regex("abc", "Regex not mathced", 10)] -> (RegexPattern, What gets returned when validation fails, RegexEngineTimeout)
    public string FirstName { get; init; }
}

Controller action — no extra code needed:

[HttpPost("register")]
public IActionResult Register(RegisterUserRequest request)
{
    // Only reached if "request" is valid
    return Ok();
}

If the client sends, for example, a Username that's too short or an invalid Email, the filter automatically stops processing and returns 400 Bad Request with the corresponding errors — the action itself is never executed.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net11.0

    • No dependencies.

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.2.0 72 8/30/2026
1.1.2 74 8/29/2026
1.0.2 74 8/28/2026
1.0.1 68 8/28/2026