MinimalApis.Validators
The easiest way to use validations with Web-Apis to .NET 6 Minimal Apis.
Install / Use
/learn @juniorporfirio/MinimalApis.ValidatorsREADME
MinimalApis.Validators
The easiest way to validate web apis from .NET6 MinimalApis.
Inspired in an example of Nick Chapsa.
<b>Thank's a lot Nick Chapsa !!!</b>
Nuget Packages
Core Library
- MinimalApis.Validators - Validator using DataAnnotation.
- MiimalApis.Validators.FluentValidation - Validator using package FluentValidation.
How can use it
Only use the key <b>WithValidator<></b> in your endpoint and finish:
app.MapPost("/customer",(Customer customer) =>
{
return Results.Created(nameof(customer), customer);
}).WithValidator<Customer>();
Basic Usage DataAnnotation
var app = WebApplication.Create(args);
app.MapPost("/customer",(Customer customer) =>
{
return Results.Created(nameof(customer), customer);
}).WithValidator<Customer>();
app.Run("http://localhost:5010");
public class Customer
{
[Required]
public string Name { get; set; } = "";
[Required]
public int Age { get; set; }
[EmailAddress]
public string Email { get; set; } = "";
}
More details in source example Example With DataAnnotation
Basic Usage FluentValidation
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddValidatorsFromAssembly(typeof(Program).Assembly);
var app = builder.Build();
app.MapPost("/customer",(Customer customer) =>
{
return Results.Created(nameof(customer),customer);
}).WithValidator<Customer>();
app.Run("http://localhost:5005");
public class Customer
{
public string Name { get; set; } = "";
public int Age { get; set; }
public string Email { get; set; } = "";
}
public class CustomerValidator : AbstractValidator<Customer>
{
public CustomerValidator()
{
RuleFor(x => x.Name).NotEmpty();
RuleFor(x => x.Age).GreaterThan(0);
RuleFor(x => x.Email).EmailAddress();
}
}
More details in source example Example With FluentValidation
Credits and contributors
<b>Special credit to Nick Chapsa to made it's possible.</b>
Maintained by Junior Porfirio - follow me on twitter @juniorporfirio for updates.
