FluentValidation.Schema generates JSON schemas from FluentValidation rules, ensuring consistent client- and server-side validation. Highly customizable for any use case.
$ dotnet add package Extensions.FluentValidation.SchemaFluentValidation.Schema generates JSON schemas from FluentValidation rules, ensuring consistent validation on both client- and server-side. Highly customizable and extensible, it bridges the gap between backend and frontend validation logic.
Using .NET CLI:
dotnet add package Extensions.FluentValidation.Schema
Or via NuGet Package Manager:
Install-Package Extensions.FluentValidation.Schema
public class CustomerValidator : CustomAbstractionValidator<Customer>
{
public CustomerValidator()
{
RuleFor(c => c.Id)
.GreaterThan(0)
.WithMessage("Customer ID must be greater than 0.");
RuleFor(c => c.Surname)
.NotEmpty()
.WithMessage("Surname is required.")
.MaximumLength(50)
.WithMessage("Surname cannot exceed 50 characters.");
RuleFor(c => c.Forename)
.NotEmpty()
.WithMessage("Forename is required.")
.MaximumLength(50)
.WithMessage("Forename cannot exceed 50 characters.");
RuleFor(c => c.Discount)
.InclusiveBetween(0, 100)
.WithMessage("Discount must be between 0 and 100.");
RuleFor(c => c.Address)
.MaximumLength(200)
.WithMessage("Address cannot exceed 200 characters.");
}
}
public class Customer
{
public int Id { get; set; }
public string Surname { get; set; }
public string Forename { get; set; }
public decimal Discount { get; set; }
public string Address { get; set; }
}
var validator = new CustomerValidator();
Console.WriteLine(validator.GetJsonSchema());
The library supports the following FluentValidation converters out-of-the-box:
Custom converters can also be added for more complex scenarios.
{
"Id": [
{
"ErrorMessage": "Customer ID must be greater than 0.",
"Type": "GreaterThan",
"ValueToCompare": 0
}
],
"Surname": [
{
"ErrorMessage": "Surname is required.",
"Type": "NotEmpty"
},
{
"ErrorMessage": "Surname cannot exceed 50 characters.",
"Type": "MaximumLength",
"Value": 50
}
],
"Forename": [
{
"ErrorMessage": "Forename is required.",
"Type": "NotEmpty"
},
{
"ErrorMessage": "Forename cannot exceed 50 characters.",
"Type": "MaximumLength",
"Value": 50
}
],
"Discount": [
{
"ErrorMessage": "Discount must be between 0 and 100.",
"Type": "InclusiveBetween",
"From": 0,
"To": 100
}
],
"Address": [
{
"ErrorMessage": "Address cannot exceed 200 characters.",
"Type": "MaximumLength",
"Value": 200
}
]
}
Contributions are welcome! You can:
MIT License — free to use, modify, and distribute.