Skip to content

Commit

Permalink
Fixes for CPF and CNPJ validations. Support extensions added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno de Souza Melo committed Apr 8, 2024
1 parent 7d310aa commit 4011b20
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 62 deletions.
2 changes: 1 addition & 1 deletion docs/Publishing.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nuget.exe push -Source "nuvtools" -ApiKey az NuvTools.Validation.7.0.0.nupkg
nuget.exe push -Source "nuvtools" -ApiKey az NuvTools.Validation.8.0.0.nupkg
111 changes: 55 additions & 56 deletions src/NuvTools.Validation/Brazil/Validator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Globalization;

namespace NuvTools.Validation.Brazil;

/// <summary>
Expand All @@ -8,11 +6,11 @@ namespace NuvTools.Validation.Brazil;
public static class Validator
{
/// <summary>
/// Método para validar um CPF ou um CNPJ.
/// Validates a CPF or CNPJ.
/// </summary>
/// <param name="value">Informe um CPF ou um CNPJ. Retorno True: CPF ou CNPJ válido. False: CPF ou CNPJ inválido.</param>
/// <returns>True: CPF ou CNPJ válido. False: CPF ou CNPJ inválido.</returns>
public static bool IsCPForCNPJ(string value)
/// <param name="value">Enter a CPF or a CNPJ.</param>
/// <returns>True: CPF or CNPJ is valid. False: CPF or CNPJ is invalid.</returns>
public static bool IsCPForCNPJ(this string value)
{
string numbersOnly = value.GetNumbersOnly();

Expand All @@ -24,89 +22,90 @@ public static bool IsCPForCNPJ(string value)
}

/// <summary>
/// Método para validar um CPF.
/// Validates a CPF.
/// </summary>
/// <param name="value">Informe um CPF. Retorno True: CPF válido. False: CPF inválido.</param>
/// <returns>True: CPF válido. False: CPF inválido.</returns>
public static bool IsCPF(string value)
/// <param name="value">Enter a CPF.</param>
/// <returns>True: CPF or CNPJ is valid. False: CPF or CNPJ is invalid.</returns>
public static bool IsCPF(this string value)
{
string numbersOnly = value.GetNumbersOnly();
value = value.GetNumbersOnly();

if (numbersOnly.Length != 11)
if (value.Length != 11)
return false;

string number = numbersOnly.Substring(0, 9);
string verifiedNumber = numbersOnly.Substring(9, 2);
for (int j = 0; j < 10; j++)
if (j.ToString().PadLeft(11, char.Parse(j.ToString())) == value)
return false;

int[] multiplier1 = [10, 9, 8, 7, 6, 5, 4, 3, 2];
int[] multiplier2 = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2];

string tempValue = value[..9];
int sum = 0;

for (int i = 0; i < 9; i++)
sum += int.Parse(number.Substring(i, 1), NumberFormatInfo.InvariantInfo) * (10 - i);
sum += int.Parse(tempValue[i].ToString()) * multiplier1[i];

if (sum == 0)
return false;
int remainder = sum % 11;
if (remainder < 2)
remainder = 0;
else
remainder = 11 - remainder;

sum = 11 - sum % 11;
string digit = remainder.ToString();
tempValue += digit;
sum = 0;
for (int i = 0; i < 10; i++)
sum += int.Parse(tempValue[i].ToString()) * multiplier2[i];

if (sum > 9)
sum = 0;
remainder = sum % 11;
if (remainder < 2)
remainder = 0;
else
remainder = 11 - remainder;

if (int.Parse(verifiedNumber.Substring(0, 1), NumberFormatInfo.InvariantInfo) != sum)
return false;

sum *= 2;
for (int i = 0; i < 9; i++)
sum += int.Parse(number.Substring(i, 1), NumberFormatInfo.InvariantInfo) * (11 - i);
digit += remainder.ToString();

sum = 11 - sum % 11;
if (sum > 9)
sum = 0;
if (int.Parse(verifiedNumber.Substring(1, 1), NumberFormatInfo.InvariantInfo) != sum)
return false;

return true;
return value.EndsWith(digit);
}

/// <summary>
/// Método para validar um CNPJ.
/// </summary>
/// <param name="value">Informe um CNPJ. Retorno True: CNPJ válido. False: CNPJ inválido.</param>
/// <returns>True: CNPJ válido. False: CNPJ inválido.</returns>
public static bool IsCNPJ(string value)
public static bool IsCNPJ(this string value)
{
string numbersOnly = value.GetNumbersOnly();
value = value.GetNumbersOnly();

if (numbersOnly.Length != 14)
if (value.Length != 14)
return false;

string number = numbersOnly.Substring(0, 12);
string verifiedNumber = numbersOnly.Substring(12, 2);
int[] multiplier1 = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
int[] multiplier2 = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];

string tempValue = value[..12];
int sum = 0;

for (int i = 0; i < 12; i++)
sum += int.Parse(number.Substring(11 - i, 1), NumberFormatInfo.InvariantInfo) * (2 + i % 8);

if (sum == 0)
return false;
sum += int.Parse(tempValue[i].ToString()) * multiplier1[i];

sum = 11 - sum % 11;
if (sum > 9)
sum = 0;
int remainder = sum % 11;
remainder = remainder < 2 ? 0 : 11 - remainder;

if (int.Parse(verifiedNumber.Substring(0, 1), NumberFormatInfo.InvariantInfo) != sum)
return false;
string digit = remainder.ToString();
tempValue += digit;

sum *= 2;
for (int i = 0; i < 12; i++)
sum += int.Parse(number.Substring(11 - i, 1), NumberFormatInfo.InvariantInfo) * (2 + (i + 1) % 8);
sum = 0;
for (int i = 0; i < 13; i++)
sum += int.Parse(tempValue[i].ToString()) * multiplier2[i];

sum = 11 - sum % 11;
if (sum > 9)
sum = 0;
remainder = sum % 11;
remainder = remainder < 2 ? 0 : 11 - remainder;

if (int.Parse(verifiedNumber.Substring(1, 1), NumberFormatInfo.InvariantInfo) != sum)
return false;
digit += remainder.ToString();

return true;
return value.EndsWith(digit);
}

}
5 changes: 3 additions & 2 deletions src/NuvTools.Validation/NuvTools.Validation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

<PropertyGroup>
<TargetFrameworks>net6;net7;net8</TargetFrameworks>
<LangVersion>latest</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Nuv Tools</Authors>
<Copyright>Copyright © 2024 Nuv Tools</Copyright>
<PackageProjectUrl>https://nuv.tools</PackageProjectUrl>
<PackageProjectUrl>https://nuvtools.com</PackageProjectUrl>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>NuvTools.Validation.snk</AssemblyOriginatorKeyFile>
<Description>Validation library for Web, Desktop and Mobile (MAUI) applications.</Description>
<Version>8.0.0</Version>
<Version>8.0.1</Version>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<PackageIcon>icon.png</PackageIcon>
Expand Down
27 changes: 27 additions & 0 deletions tests/NuvTools.Validation.Test/Brazil/CNPJandCPFExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using NUnit.Framework;
using NuvTools.Validation.Brazil;

namespace NuvTools.Validation.Tests.Brazil;

[TestFixture()]
public class CNPJandCPFExtensions
{
[Test()]
public void ValidateCNPJ()
{
Assert.That(!"11111111111111".IsCNPJ());
Assert.That("03.785.417/0001-03".IsCNPJ());
Assert.That("54243121000193".IsCNPJ());
}

[Test()]
public void ValidateCPF()
{
Assert.That(!"11111111111".IsCPF());
Assert.That(!"22222222222".IsCPF());
Assert.That(!"erro".IsCPF());
Assert.That("583.008.930-08".IsCPF());
Assert.That("83289988074".IsCPF());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<Version>8.0.0</Version>
<Version>8.0.1</Version>
<Authors>Nuv Tools</Authors>
<Copyright>Copyright © 2024 Nuv Tools</Copyright>
<PackageProjectUrl>https://nuv.tools</PackageProjectUrl>
<PackageProjectUrl>https://nuvtools.com</PackageProjectUrl>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>NuvTools.Validation.Tests.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
Expand All @@ -18,7 +18,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\NuvTools.Validation\NuvTools.Validation.csproj" />
<ProjectReference Include="..\..\src\NuvTools.Validation\NuvTools.Validation.csproj" />
</ItemGroup>

</Project>

0 comments on commit 4011b20

Please sign in to comment.