Creating complex validation rules with FluentValidation
In this recipe, we will learn how to create complex validation rules using the FluentValidation library. FluentValidation offers a more fluent and flexible way to define validation logic.
Getting ready
We are going to continue working with the same DTO. However, all the validation rules have been removed – both the simple data annotations and the rules created via IValidateObject. The code for this project resides here: chapter02/FluentValidation.
How to do it…
- Open the project in Visual Studio Code.
- Open the terminal and type
dotnet run. - Open the browser and go to
http://localhost:5217/scalar/chapter2.You have a basic API with some
DataAnnotationsand a customDataAnnotationsattribute on the DTO. - Install the
FluentValidationNuGet package:dotnet add package FluentValidation.AspNetCore
- Note that our DTO currently has no validation rules. Add the following code to the
EventRegistrationDTO...