PosInformatique.FluentValidation.Json
1.0.0-rc.1
Prefix Reserved
See the version list below for details.
dotnet add package PosInformatique.FluentValidation.Json --version 1.0.0-rc.1
NuGet\Install-Package PosInformatique.FluentValidation.Json -Version 1.0.0-rc.1
<PackageReference Include="PosInformatique.FluentValidation.Json" Version="1.0.0-rc.1" />
paket add PosInformatique.FluentValidation.Json --version 1.0.0-rc.1
#r "nuget: PosInformatique.FluentValidation.Json, 1.0.0-rc.1"
// Install PosInformatique.FluentValidation.Json as a Cake Addin #addin nuget:?package=PosInformatique.FluentValidation.Json&version=1.0.0-rc.1&prerelease // Install PosInformatique.FluentValidation.Json as a Cake Tool #tool nuget:?package=PosInformatique.FluentValidation.Json&version=1.0.0-rc.1&prerelease
PosInformatique.FluentValidation.Json
PosInformatique.FluentValidation.Json is a library based on FluentValidation to validate JSON objects for the Web API.
By default, when using the FluentValidation library to validate an object, the property name (or related display name) are used in the error message. This can be useful for functional validation to display to users on the views of the application.
But when you perform some validations in a Web API context, on JSON DTO objects, using C# property name does not help developers to indicate which properties are invalid. Specially if the C# property name is differents of the JSON property name associated.
For example, imagine you have the following JSON object that represents a product:
{
"description": "Chicken adobo",
"price": 10
}
This JSON object is mapped to the following C# class, using [JsonPropertyName]
attributes
to define the JSON property names.
public class Product
{
public Product()
{
}
[JsonPropertyName("description")]
public string? Description { get; set; }
[JsonPropertyName("price")]
public decimal Price { get; set; }
}
If you want to validate the C# Product
class, you have to create a validator
which inherit from the AbstractValidator<T>
class.
public class ProductValidator : AbstractValidator<Product>
{
public ProductValidator()
{
this.RuleLevelCascadeMode = CascadeMode.Stop;
this.RuleFor(p => p.Description).NotNull().NotEmpty();
this.RuleFor(p => p.Price).GreaterThan(0);
this.RuleFor(p => p.Category).NotNull().SetValidator(new ProductCategoryValidator());
}
}
public class ProductCategoryValidator : AbstractValidator<ProductCategory>
{
public ProductCategoryValidator()
{
this.RuleFor(p => p.Name).NotEmpty();
}
}
When performing the validation of inside a ASP .NET MVC API application the following JSON problem is returned by default:
{
"value": {
"title": "One or more validation errors occurred.",
"errors": {
"Description": [
"'Description' must not be empty."
],
"Price": [
"'Price' must be greater than '0'."
],
"Category.Name": [
"'Name' must not be empty."
]
}
},
"statusCode": 400,
"contentType": "application/problem+json"
}
Here, because we expose this JSON content to developers, we prefered to have the JSON property name path in the errors messages.
This the main goal of this library to return the following JSON result instead:
{
"value": {
"title": "One or more validation errors occurred.",
"errors": {
"description": [
"'description' must not be empty."
],
"price": [
"'price' must be greater than '0'."
],
"category.name": [
"'name' must not be empty."
]
}
},
"statusCode": 400,
"contentType": "application/problem+json"
}
Installing from NuGet
The PosInformatique.FluentValidation.Json
library is available directly on the
official website.
To download and install the library to your Visual Studio unit test projects use the following NuGet command line
Install-Package PosInformatique.FluentValidation.Json
How it is work?
This library is really easy to use and do not need lot of changes if you already implemented
AbstractValidator<T>
validators.
To use JSON property names when validating a DTO class, just inherit from the
JsonAbstractValidator<T>
instead of AbstractValidator<T>
.
For example, to validate the Product
or ProductCategory
classes of the previous example,
use the following JsonAbstractValidator<T>
implementations:
public class ProductValidator : JsonAbstractValidator<Product>
{
public ProductValidator()
{
this.RuleLevelCascadeMode = CascadeMode.Stop;
this.RuleFor(p => p.Description).NotNull().NotEmpty();
this.RuleFor(p => p.Price).GreaterThan(0);
this.RuleFor(p => p.Category).NotNull().SetValidator(new ProductCategoryValidator());
}
}
public class ProductCategoryValidator : JsonAbstractValidator<ProductCategory>
{
public ProductCategoryValidator()
{
this.RuleFor(p => p.Name).NotEmpty();
}
}
And THAT ALL !!.
Next, you use your own validation strategy depending of the context usage. For example, if you ASP .NET Core to create an Web API, you can use the following code and returns an error as JSON problem format:
[ApiController]
[Route("[controller]")]
public class ProductController : ControllerBase
{
private readonly IValidator<Product> validator;
public ProductController(IValidator<Product> validator)
{
this.validator = validator;
}
[HttpPost]
public IResult Post(Product product)
{
var result = this.validator.Validate(product);
if (!result.IsValid)
{
return Results.ValidationProblem(result.ToDictionary());
}
return Results.Ok();
}
}
Do not hesitate to read the FluentValidation ASP .NET Integration documentation for more information.
Product | Versions 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 was computed. 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 was computed. 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. |
-
net6.0
- FluentValidation (>= 11.8.1)
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 | 129 | 6/8/2024 |
1.0.0-rc.3 | 154 | 3/8/2024 |
1.0.0-rc.2 | 559 | 12/2/2023 |
1.0.0-rc.1 | 55 | 12/2/2023 |
1.0.0
- Initial version