Skip to content

Generates Fluent Validation C# Classes based on MediatR commands

License

Notifications You must be signed in to change notification settings

DawnDevelop/FluentValidationGenerator

Repository files navigation

FluentValidationGenerator

Nuget (with prereleases) Nuget Downloads GitHub Workflow Status

Maintainability codecov

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.

Plans

✅ 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

Usage

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:

var generator = new FluentValidationGenerator.Generator(
	typeof(CreateWeatherForecastCommand).Assembly,
	Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory()))!);

generator.GenerateValidators();

Having defined Commands or Queries like the following:

public record CreateWeatherForecastCommand : IRequest<IEnumerable<WeatherForecast>>
{
    public required string[] Summaries { get; set; }

    public int TemperatureC { get; set; }

    public string? NullableString { get; set; }

    public required string NotNullableString { get; set; }

}

Should generate a File looking similar to the following Code, depending on the amount of Parameters

using FluentValidation;

namespace SampleNET7.Application.Messages.Commands;

public class CreateWeatherForecastCommandValidator : AbstractValidator<CreateWeatherForecastCommand>
{
  public CreateWeatherForecastCommandValidator()
  {
        
	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


Medium publication

About

Generates Fluent Validation C# Classes based on MediatR commands

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published