Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: cpf and cnpj validators in FluentValidator #59

Merged
merged 3 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ A framework to simplify application creation by improving dependency injection,
- Fluent api
- Reliable library
- Easy usage
- CPF and CNPJ validators
- Dependency Injection
- Life time configurable
- Easy manipulation
Expand Down Expand Up @@ -139,11 +140,14 @@ public class UserConfiguration : IEntityTypeConfiguration<User>
```csharp
using FluentValidation;
using TakasakiStudio.Lina.Common;
using TakasakiStudio.Lina.Common.Extensions;
using TakasakiStudio.Lina.Database.Models;

var user = new User()
{
Name = ""
Name = "",
Cpf = "",
Cnpj = "",
};

if (!await user.IsValid())
Expand All @@ -152,34 +156,40 @@ if (!await user.IsValid())
}

user.Name = "Foo";
user.Cpf = "349.306.930-80";
user.Cnpj = "82.099.001/0001-08";

await user.Validate();

Console.WriteLine("Valid");

public class User : BaseEntityValidate<User, UserValidation, int>
{
public string Name { get; set; } = string.Empty;
public required string Name { get; set; }
public required string Cpf { get; set; }
public required string Cnpj { get; set; }
}

public class UserValidation : AbstractValidator<User>
{
public UserValidation()
{
RuleFor(x => x.Name).NotEmpty();
RuleFor(x => x.Cpf).ValidCpf();
RuleFor(x => x.Cnpj).ValidCnpj();
}
}

public record UserViewModel() : BaseValidationRecord<UserViewModel, UserViewModelValidation>
{
public string Name { get; set; } = string.Empty;
}
public record UserViewModel(string Name, string Cpf, string Cnpj)
: BaseValidationRecord<UserViewModel, UserViewModelValidation>;

public class UserViewModelValidation : AbstractValidator<UserViewModel>
{
public UserViewModelValidation()
{
RuleFor(x => x.Name).NotEmpty();
RuleFor(x => x.Cpf).ValidCpf();
RuleFor(x => x.Cnpj).ValidCnpj();
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.0.12</Version>
<Version>2.0.13</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>TakasakiStudio.Lina.AspNet</PackageId>
<Authors>TakasakiStudio</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.0.12</Version>
<Version>2.0.13</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>TakasakiStudio.Lina.AutoDependencyInjection</PackageId>
<Authors>TakasakiStudio</Authors>
Expand Down
79 changes: 79 additions & 0 deletions TakasakiStudio.Lina.Common/Extensions/RuleBuilderExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System.Text.RegularExpressions;
using FluentValidation;

namespace TakasakiStudio.Lina.Common.Extensions;

public static partial class RuleBuilderExtension
{
public static IRuleBuilder<T, string> ValidCpf<T>(this IRuleBuilder<T, string> ruleBuilder)
{
return ruleBuilder.Must(BeAValidCpf);
}

public static IRuleBuilder<T, string> ValidCnpj<T>(this IRuleBuilder<T, string> ruleBuilder)
{
return ruleBuilder.Must(BeAValidCnpj);
}

private static bool BeAValidCpf(string input)
{
var numbers = ToNumbers(input);
if (numbers.Length != 11 || numbers.Distinct().Count() == 1)
return false;

var v1 = 0;
var v2 = 0;

for (var i = 0; i < 9; i++) {
var number = numbers[8 - i];
v1 += number * (9 - i % 10);
v2 += number * (9 - (i + 1) % 10);
}

v1 %= 11;
v1 %= 10;

v2 += v1 * 9;
v2 %= 11;
v2 %= 10;

return numbers[9] == v1 && numbers[10] == v2;
}

private static bool BeAValidCnpj(string input)
{
var numbers = ToNumbers(input);
if (numbers.Length != 14)
return false;

var v1 = 5 * numbers[0] + 4 * numbers[1] + 3 * numbers[2] + 2 * numbers[3] +
9 * numbers[4] + 8 * numbers[5] + 7 * numbers[6] + 6 * numbers[7] +
5 * numbers[8] + 4 * numbers[9] + 3 * numbers[10] + 2 * numbers[11];

var v2 = 6 * numbers[0] + 5 * numbers[1] + 4 * numbers[2] + 3 * numbers[3] +
2 * numbers[4] + 9 * numbers[5] + 8 * numbers[6] + 7 * numbers[7] +
6 * numbers[8] + 5 * numbers[9] + 4 * numbers[10] + 3 * numbers[11] +
2 * numbers[12];

v1 = 11 - v1 % 11;
if (v1 >= 10)
v1 = 0;

v2 = 11 - v2 % 11;
if (v2 >= 10)
v2 = 0;

return numbers[12] == v1 && numbers[13] == v2;
}

private static int[] ToNumbers(string input)
{
var clean = NotNumberRegex().Replace(input, string.Empty);
var numbers = clean.ToCharArray().Select(x => int.Parse(x.ToString()));

return numbers.ToArray();
}

[GeneratedRegex(@"[^\d]")]
private static partial Regex NotNumberRegex();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.0.12</Version>
<Version>2.0.13</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>TakasakiStudio.Lina.Common</PackageId>
<Title>TakasakiStudio.Lina.Common</Title>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.0.12</Version>
<Version>2.0.13</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>TakasakiStudio.Lina.Database</PackageId>
<Title>TakasakiStudio.Lina.Database</Title>
Expand Down
2 changes: 1 addition & 1 deletion TakasakiStudio.Lina.Utils/TakasakiStudio.Lina.Utils.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.0.12</Version>
<Version>2.0.13</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>TakasakiStudio.Lina.Utils</PackageId>
<Title>TakasakiStudio.Lina.Utils</Title>
Expand Down
2 changes: 1 addition & 1 deletion TakasakiStudio.Lina/TakasakiStudio.Lina.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.0.12</Version>
<Version>2.0.13</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>TakasakiStudio.Lina</PackageId>
<Title>TakasakiStudio.Lina</Title>
Expand Down