Skip to content

The easiest way to use validations with Web-Apis to .NET 6 Minimal Apis.

License

Notifications You must be signed in to change notification settings

juniorporfirio/MinimalApis.Validators

Repository files navigation

MinimalApis.Validators

The easiest way to validate web apis from .NET6 MinimalApis.

Inspired in an example of Nick Chapsa.

Thank's a lot Nick Chapsa !!!

Nuget Packages

Core Library

How can use it

Only use the key WithValidator<> 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

Special credit to Nick Chapsa to made it's possible.

Maintained by Junior Porfirio - follow me on twitter @juniorporfirio for updates.

About

The easiest way to use validations with Web-Apis to .NET 6 Minimal Apis.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages