diff --git a/docs/Publishing.txt b/docs/Publishing.txt index 255c4ad..0276cad 100644 --- a/docs/Publishing.txt +++ b/docs/Publishing.txt @@ -1 +1 @@ -nuget.exe push -Source "nuvtools" -ApiKey az NuvTools.Validation.7.0.0.nupkg \ No newline at end of file +nuget.exe push -Source "nuvtools" -ApiKey az NuvTools.Validation.8.0.0.nupkg \ No newline at end of file diff --git a/src/NuvTools.Validation/Brazil/Validator.cs b/src/NuvTools.Validation/Brazil/Validator.cs index 5d97bb6..1c8df9b 100644 --- a/src/NuvTools.Validation/Brazil/Validator.cs +++ b/src/NuvTools.Validation/Brazil/Validator.cs @@ -1,5 +1,3 @@ -using System.Globalization; - namespace NuvTools.Validation.Brazil; /// @@ -8,11 +6,11 @@ namespace NuvTools.Validation.Brazil; public static class Validator { /// - /// Método para validar um CPF ou um CNPJ. + /// Validates a CPF or CNPJ. /// - /// Informe um CPF ou um CNPJ. Retorno True: CPF ou CNPJ válido. False: CPF ou CNPJ inválido. - /// True: CPF ou CNPJ válido. False: CPF ou CNPJ inválido. - public static bool IsCPForCNPJ(string value) + /// Enter a CPF or a CNPJ. + /// True: CPF or CNPJ is valid. False: CPF or CNPJ is invalid. + public static bool IsCPForCNPJ(this string value) { string numbersOnly = value.GetNumbersOnly(); @@ -24,46 +22,51 @@ public static bool IsCPForCNPJ(string value) } /// - /// Método para validar um CPF. + /// Validates a CPF. /// - /// Informe um CPF. Retorno True: CPF válido. False: CPF inválido. - /// True: CPF válido. False: CPF inválido. - public static bool IsCPF(string value) + /// Enter a CPF. + /// True: CPF or CNPJ is valid. False: CPF or CNPJ is invalid. + 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); } /// @@ -71,42 +74,38 @@ public static bool IsCPF(string value) /// /// Informe um CNPJ. Retorno True: CNPJ válido. False: CNPJ inválido. /// True: CNPJ válido. False: CNPJ inválido. - 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); } } \ No newline at end of file diff --git a/src/NuvTools.Validation/NuvTools.Validation.csproj b/src/NuvTools.Validation/NuvTools.Validation.csproj index e8f4228..9bdfd4d 100644 --- a/src/NuvTools.Validation/NuvTools.Validation.csproj +++ b/src/NuvTools.Validation/NuvTools.Validation.csproj @@ -2,14 +2,15 @@ net6;net7;net8 + latest true Nuv Tools Copyright © 2024 Nuv Tools - https://nuv.tools + https://nuvtools.com True NuvTools.Validation.snk Validation library for Web, Desktop and Mobile (MAUI) applications. - 8.0.0 + 8.0.1 True True icon.png diff --git a/tests/NuvTools.Validation.Test/Brazil/CNPJandCPFExtensions.cs b/tests/NuvTools.Validation.Test/Brazil/CNPJandCPFExtensions.cs new file mode 100644 index 0000000..4450786 --- /dev/null +++ b/tests/NuvTools.Validation.Test/Brazil/CNPJandCPFExtensions.cs @@ -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()); + } + +} \ No newline at end of file diff --git a/tests/NuvTools.Validation.Test/NuvTools.Validation.Tests.csproj b/tests/NuvTools.Validation.Test/NuvTools.Validation.Tests.csproj index d6020e5..0528601 100644 --- a/tests/NuvTools.Validation.Test/NuvTools.Validation.Tests.csproj +++ b/tests/NuvTools.Validation.Test/NuvTools.Validation.Tests.csproj @@ -3,10 +3,10 @@ net8.0 false - 8.0.0 + 8.0.1 Nuv Tools Copyright © 2024 Nuv Tools - https://nuv.tools + https://nuvtools.com true NuvTools.Validation.Tests.snk @@ -18,7 +18,7 @@ - +