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
                    
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="Sindika.AspNet.Validation" Version="1.2.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sindika.AspNet.Validation" Version="1.2.3" />
                    
Directory.Packages.props
<PackageReference Include="Sindika.AspNet.Validation" />
                    
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 Sindika.AspNet.Validation --version 1.2.3
                    
#r "nuget: Sindika.AspNet.Validation, 1.2.3"
                    
#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.
#addin nuget:?package=Sindika.AspNet.Validation&version=1.2.3
                    
Install Sindika.AspNet.Validation as a Cake Addin
#tool nuget:?package=Sindika.AspNet.Validation&version=1.2.3
                    
Install Sindika.AspNet.Validation as a Cake Tool

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last updated
1.2.3 818 2 months ago
1.2.2 113 2 months ago
1.2.1 170 2 months ago
1.1.2 118 3 months ago
1.1.1 91 3 months ago
1.1.0 103 3 months ago
1.0.0 655 3 months ago
0.1.0 116 4 months ago