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
<PackageReference Include="CleanArchitecture.ValidationRules.Types.Analyzers" Version="1.0.1" />
<PackageVersion Include="CleanArchitecture.ValidationRules.Types.Analyzers" Version="1.0.1" />
<PackageReference Include="CleanArchitecture.ValidationRules.Types.Analyzers" />
paket add CleanArchitecture.ValidationRules.Types.Analyzers --version 1.0.1
#r "nuget: CleanArchitecture.ValidationRules.Types.Analyzers, 1.0.1"
#addin nuget:?package=CleanArchitecture.ValidationRules.Types.Analyzers&version=1.0.1
#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
- 📄 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.
Version | Downloads | Last Updated |
---|---|---|
1.0.1 | 311 | 11/28/2022 |