CleanArchitecture.ValidationRules.Types.Analyzers 1.0.1

dotnet add package CleanArchitecture.ValidationRules.Types.Analyzers --version 1.0.1                
NuGet\Install-Package CleanArchitecture.ValidationRules.Types.Analyzers -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CleanArchitecture.ValidationRules.Types.Analyzers" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CleanArchitecture.ValidationRules.Types.Analyzers --version 1.0.1                
#r "nuget: CleanArchitecture.ValidationRules.Types.Analyzers, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CleanArchitecture.ValidationRules.Types.Analyzers as a Cake Addin
#addin nuget:?package=CleanArchitecture.ValidationRules.Types.Analyzers&version=1.0.1

// Install CleanArchitecture.ValidationRules.Types.Analyzers as a Cake Tool
#tool nuget:?package=CleanArchitecture.ValidationRules.Types.Analyzers&version=1.0.1                

Clean-Architecture-Fluent-Validation

Source Generator to automatically validate persistence layer data annotations without manual duplication.

Overview

This source generator was inspired by Clean Architecture. One of the problems that we have encountered is needing to have both database constraints and input constraints. We wouldn't want to create a database with a string with no max length when the field calls for only a few characters. We also wouldn't want to have to keep both the persistence entity and the data transfer object in sync for these max character lengths.

So CleanArchitecture.ValidatonRules.Types.Analyzers was born and it will automatically generate the Fluent Validation files that you can call to validate the persistence layer's object. In order to allow for the most flexibility, Fluent Validation's abstract validator isn't generated automatically but rather the rules that can be easily called by your AbstractValdiator for the dto. See Usage for more information.

Installation

$> dotnet add package CleanArchitecture.ValidationRules.Types.Analyzers

Usage

Add FluentValidation validator

//Application Layer Code
namespace MoviesExample.Application.Genres.Commands.CreateGenre
{
	[ExtendValidation(typeof(Genre))]
    public record CreateGenreCommand : IRequest<CreateGenrePayload>
    {
        public string? Name { get; init; }
    }
    public class CreateGenreCommandValidator : AbstractValidator<CreateGenreCommand>
    {
        public CreateGenreCommandValidator()
        {
            RuleFor(v => v.Name).Name();//The Name method is created automatically by the source generator.
        }
    }
}
//Domain Layer Entities

namespace MoviesExample.Domain.Entities;
{
	public class Genre : BaseAuditableEntity
	{
		[Required]
		[MaxLength(20)]
		public string? Name { get; set; }
	}
}

//Generated code by the source generator
namespace MoviesExample.Application.Genres.Commands.CreateGenre
{
    public static class CreateGenreCommandValidators
    {
        public static IRuleBuilderOptions<CreateGenreCommand, string> Name<CreateGenreCommand>(this IRuleBuilder<CreateGenreCommand, string?> ruleBuilder)
        {
            return ruleBuilder
                .NotEmpty().WithMessage("{0} is required.")
                .MaximumLength(20).WithMessage("{0} must not exceed 20 characters.");
        }
    }
}

Examples

Support

If you are having problems, please let us know by raising a new issue.

License

This project is licensed with the MIT license.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 246 11/28/2022