PosInformatique.FluentValidation.Json
1.0.0-rc.2
Prefix Reserved
See the version list below for details.
dotnet add package PosInformatique.FluentValidation.Json --version 1.0.0-rc.2
NuGet\Install-Package PosInformatique.FluentValidation.Json -Version 1.0.0-rc.2
<PackageReference Include="PosInformatique.FluentValidation.Json" Version="1.0.0-rc.2" />
paket add PosInformatique.FluentValidation.Json --version 1.0.0-rc.2
#r "nuget: PosInformatique.FluentValidation.Json, 1.0.0-rc.2"
// Install PosInformatique.FluentValidation.Json as a Cake Addin #addin nuget:?package=PosInformatique.FluentValidation.Json&version=1.0.0-rc.2&prerelease // Install PosInformatique.FluentValidation.Json as a Cake Tool #tool nuget:?package=PosInformatique.FluentValidation.Json&version=1.0.0-rc.2&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.
JSON serialization library
This library use the JSON property names specified by the [JsonPropertyName]
attributes
with the Microsoft System.Text.Json
.
This library DO NOT use the property names specified by the [JsonProperty]
attributes
of the Newtonsoft.Json
library.
Library dependencies
The PosInformatique.FluentValidation.Json library target the .NET Standard 2.0 and can be used with various of .NET architecture (.NET Core, .NET Framework,...).
The PosInformatique.FluentValidation.Json library use the 4.6.0 version of the System.Text.Json NuGet package and can be used with old projects that target this library version and earlier.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- FluentValidation (>= 11.8.1)
- System.Text.Json (>= 4.6.0)
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