EutonTechnologies.ValidationRules.Types.Analyzers
1.0.0
See the version list below for details.
dotnet add package EutonTechnologies.ValidationRules.Types.Analyzers --version 1.0.0
NuGet\Install-Package EutonTechnologies.ValidationRules.Types.Analyzers -Version 1.0.0
<PackageReference Include="EutonTechnologies.ValidationRules.Types.Analyzers" Version="1.0.0" />
paket add EutonTechnologies.ValidationRules.Types.Analyzers --version 1.0.0
#r "nuget: EutonTechnologies.ValidationRules.Types.Analyzers, 1.0.0"
// Install EutonTechnologies.ValidationRules.Types.Analyzers as a Cake Addin #addin nuget:?package=EutonTechnologies.ValidationRules.Types.Analyzers&version=1.0.0 // Install EutonTechnologies.ValidationRules.Types.Analyzers as a Cake Tool #tool nuget:?package=EutonTechnologies.ValidationRules.Types.Analyzers&version=1.0.0
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 EutonTechnologies.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 EutonTechnologies.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
- 📄 GraphQL
Support
If you are having problems, please let us know by raising a new issue.
License
This project is licensed with the MIT license.
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.