Sindika.AspNet.Validation
1.2.3
dotnet add package Sindika.AspNet.Validation --version 1.2.3
NuGet\Install-Package Sindika.AspNet.Validation -Version 1.2.3
<PackageReference Include="Sindika.AspNet.Validation" Version="1.2.3" />
<PackageVersion Include="Sindika.AspNet.Validation" Version="1.2.3" />
<PackageReference Include="Sindika.AspNet.Validation" />
paket add Sindika.AspNet.Validation --version 1.2.3
#r "nuget: Sindika.AspNet.Validation, 1.2.3"
#addin nuget:?package=Sindika.AspNet.Validation&version=1.2.3
#tool nuget:?package=Sindika.AspNet.Validation&version=1.2.3
Sindika.AspNet.Validation
Sindika.AspNet.Validation is a library designed to streamline validation in ASP.NET applications. It provides robust tools, including middleware, action filters, and configuration options, to handle validation scenarios effectively, ensuring clean and maintainable APIs.
Features
- Model Validation: Automatically validates models using attributes and provides structured error responses.
- Custom Middleware: Includes middleware for input validation to handle complex validation needs.
- Service Integration: Easy-to-use extensions for integrating validation into the ASP.NET service pipeline.
- Customizable Settings: Configure input validators, allowed content types, and other behaviors via
appsettings.json
. - Scalable Architecture: Designed for extensibility and modularity in large-scale projects.
Installation
Install the package via NuGet:
dotnet add package Sindika.AspNet.Validation
Usage
1. Configure Validation Services
Add the following code in your Program.cs
or Startup.cs
file to configure validation services:
builder.Services.AddControllers(options =>
{
options.Filters.Add<ValidateModelStateFilter>();
});
builder.Services.Configure<ValidationSettings>(builder.Configuration.GetSection("ValidationSettings"));
builder.Services.AddValidation();
2. Define Validation Settings
Define your validation settings in the appsettings.json
file:
{
"ValidationSettings": {
"InputValidators": {
"JsonFormatValidator": [],
"AllowedContentTypes": [
"application/json",
"text/plain"
]
}
}
}
3. Apply Validation Attributes
Use standard or custom validation attributes in your models:
public class UserModel
{
[Required]
[StringLength(50)]
public string Name { get; set; }
[EmailAddress]
public string Email { get; set; }
[Range(18, 99)]
public int Age { get; set; }
}
4. Middleware for Input Validation
Add the InputValidationMiddleware
in the middleware pipeline:
var app = builder.Build();
app.UseMiddleware<InputValidationMiddleware>();
app.MapControllers();
app.Run();
Example: Validating User Input
Here is an example controller showcasing validation:
[ApiController]
[Route("api/users")]
public class UsersController : ControllerBase
{
[HttpPost]
public IActionResult CreateUser([FromBody] UserModel user)
{
// ValidateModelStateFilter handles validation errors automatically.
return Ok(new { message = "User created successfully!" });
}
}
Advanced Features
ValidationSettings Configuration
Customize validation behavior through ValidationSettings
:
JsonFormatValidator
: Reserved for future use.AllowedContentTypes
: Specifies acceptable content types (e.g.,application/json
,text/plain
).
Custom Middleware
Chain custom middleware to extend validation logic:
app.UseMiddleware<FieldValidationMiddleware>();
app.UseMiddleware<InputValidationMiddleware>();
Upload to NuGet
dotnet clean
dotnet build --configuration Release
dotnet pack --configuration Release
Contributing
Contributions are welcome! Please fork the repository and submit a pull request with your enhancements or bug fixes.
License
This project is licensed under the MIT License. See the LICENSE file for more details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
-
net9.0
- Microsoft.Extensions.Configuration (>= 9.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.0)
- Microsoft.Extensions.Logging (>= 9.0.0)
- Polly (>= 8.5.0)
- Scrutor (>= 5.1.0)
- System.Text.Encodings.Web (>= 6.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Sindika.AspNet.Validation:
Package | Downloads |
---|---|
Sindika.AspNet.RequestResponse
The Sindika.AspNet.RequestResponse library provides a standarized Request and Response of Web Api. |
GitHub repositories
This package is not used by any popular GitHub repositories.