Hi there, there are a lot of ways to add validation to your models when you are building a REST API and you want to add validation (max length, regular expression, required, etc) to your DTO models, but if you want to mantain your DTO models as POCO, in my opinion you should use Fluent Validation; in this post we are going to try to explain how to configure Fluent Validation in your project.
And you can find more details about this WhiteLabel Web API project here https://oscarchelo.blogspot.com/2020/04/net-core-31-whilelabel-web-api-project.html
References
https://www.anexinet.com/blog/asp-net-core-fluentvalidation-swagger/
https://oscarchelo.blogspot.com/2020/04/net-core-31-whilelabel-web-api-project.html
- Create a new folder "Validators" in your project to organize all your validators in the same place. Use namespaces to organize this folder.
- Install the last version of the nuget FluentValidation
- Inside this new folder create a new class like this:
- In the ConfigureServices method, tag a call to AddFluentValidation onto the AddMvc method.
- Register FluentValidation in Swagger. To do that, we'll need to add the MicroElements.Swashbuckle.FluentValidation NuGet package and modify the AddSwaggerGent like this:
- Add the validation in your endpoint:
using Core.DTO.Security; using Core.Interfaces.Security; using FluentValidation; using System; using System.Threading; using System.Threading.Tasks; namespace Core.Validators.Security { public class UserDTOValidator : AbstractValidator{ private readonly ISecurityService _securityService; public UserDTOValidator(ISecurityService securityService) { _securityService = securityService; RuleFor(x => x.UserReference).NotEmpty(); RuleFor(x => x.UserReference).MaximumLength(100); RuleFor(x => x.Name).MaximumLength(100); RuleFor(x => x.Name).NotEmpty(); RuleFor(x => x.Email).NotEmpty(); RuleFor(x => x.Email).Matches(@"^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$"); RuleFor(x => x.Email).MustAsync(IsEmailAlreadyInDatabase); } private async Task IsEmailAlreadyInDatabase(string abbreviation, CancellationToken token) { var user = await _securityService.GetUserByEmail(abbreviation); return user != null; } } }
//Fluent Validation services.AddMvc() .AddFluentValidation(o => { o.RegisterValidatorsFromAssemblyContaining(); });
// Register the Swagger generator, defining 1 or more Swagger documents services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = "WhiteLabel API", Description = "WhiteLabel ASP.NET Core Web API", TermsOfService = new Uri("https://www.yourwebsite.com/termsandservice") }); c.AddSecurityDefinition("basic", new OpenApiSecurityScheme { Name = "Authorization", Type = SecuritySchemeType.Http, Scheme = "basic", In = ParameterLocation.Header, Description = "Basic Authorization header using the Bearer scheme." }); c.DocumentFilterThis single line of code (c.AddFluentValidationRules())will let swagger check your validator classes to show the data annotations documentation. Then, when you run your project, you will be able to see a schema like this in Swagger:(); c.AddFluentValidationRules(); });
[HttpPost, Route("users")] public async TaskAddUser([FromBody] UserDTO user) { Guard.Requires(user, nameof(user)).IsNotNull(); if (ModelState.IsValid) { return Ok(await _securityService.AddUser(_mapper.Map (user))); } return BadRequest(ModelState); }
And you can find more details about this WhiteLabel Web API project here https://oscarchelo.blogspot.com/2020/04/net-core-31-whilelabel-web-api-project.html
References
https://www.anexinet.com/blog/asp-net-core-fluentvalidation-swagger/
https://oscarchelo.blogspot.com/2020/04/net-core-31-whilelabel-web-api-project.html
Comentarios
Publicar un comentario