A Code Generator for automatically generating FluentValidation Classes for .NET 7+ with predefined Rules.
The Generator uses MediatR Commands which inherit from the IRequest<> interface to automatically Generate Fluent Validator Classes.
Clean Architecture is the recommended Solution Architecture.
✅ Create Fluent Validators based on MediatR commands which follow the Clean Architecture
❌ Tests
❌ Create Fluent Validators for ASP.NET Core API's based on an OpenAPI specification
For Usage please refer to the Sample Project
FluentValidationGenerator.Generator takes the Assembly which contains the Commands and the Solution Folder as Parameters.
The easiest way to get your Solution Folder should be:
Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory()))
This should result in:
vargenerator=newFluentValidationGenerator.Generator(typeof(CreateWeatherForecastCommand).Assembly,Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory()))!);generator.GenerateValidators();Having defined Commands or Queries like the following:
publicrecordCreateWeatherForecastCommand:IRequest<IEnumerable<WeatherForecast>>{publicrequiredstring[]Summaries{get;set;}publicintTemperatureC{get;set;}publicstring?NullableString{get;set;}publicrequiredstringNotNullableString{get;set;}}Should generate a File looking similar to the following Code, depending on the amount of Parameters
usingFluentValidation;namespaceSampleNET7.Application.Messages.Commands;publicclassCreateWeatherForecastCommandValidator:AbstractValidator<CreateWeatherForecastCommand>{publicCreateWeatherForecastCommandValidator(){RuleFor(t =>t.Summaries).NotEmpty().WithMessage("Summaries Can not be Empty");RuleFor(t =>t.TemperatureC);RuleFor(t =>t.NullableString);RuleFor(t =>t.NotNullableString).NotEmpty().WithMessage("NotNullableString Can not be Empty");}}The Generator will Generate an empty Rule for Nullable Properties or Value Types