DataValidators 1.2.0
dotnet add package DataValidators --version 1.2.0
NuGet\Install-Package DataValidators -Version 1.2.0
<PackageReference Include="DataValidators" Version="1.2.0" />
<PackageVersion Include="DataValidators" Version="1.2.0" />
<PackageReference Include="DataValidators" />
paket add DataValidators --version 1.2.0
#r "nuget: DataValidators, 1.2.0"
#:package DataValidators@1.2.0
#addin nuget:?package=DataValidators&version=1.2.0
#tool nuget:?package=DataValidators&version=1.2.0
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 | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
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.