-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add multiform_validator package with CnpjValidator, CpfValidato…
…r, EmailValidator, and Validator classes
- Loading branch information
1 parent
17b923b
commit f4be705
Showing
31 changed files
with
446 additions
and
868 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
packages/java/src/main/java/io/multiform_validator/Ascii.java
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
packages/java/src/main/java/io/multiform_validator/Base64.java
This file was deleted.
Oops, something went wrong.
35 changes: 0 additions & 35 deletions
35
packages/java/src/main/java/io/multiform_validator/CEP.java
This file was deleted.
Oops, something went wrong.
70 changes: 0 additions & 70 deletions
70
packages/java/src/main/java/io/multiform_validator/Cnpj.java
This file was deleted.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
packages/java/src/main/java/io/multiform_validator/CnpjValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.multiform_validator; | ||
|
||
import java.util.Arrays; | ||
public class CnpjValidator { | ||
private CnpjValidator() { | ||
throw new IllegalStateException("Utility class"); | ||
} | ||
private static int calculateFirstVerifier(int[] cnpjBase) { | ||
final int[] weight = {5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2}; | ||
int sum = 0; | ||
|
||
for (int i = 0; i < 12; i++) { | ||
sum += cnpjBase[i] * weight[i]; | ||
} | ||
final int remainder = sum % 11; | ||
|
||
return remainder < 2 ? 0 : 11 - remainder; | ||
} | ||
|
||
private static int calculateSecondVerifier(int[] cnpjBase, int firstVerifier) { | ||
final int[] weight = {6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2}; | ||
int sum = 0; | ||
|
||
for (int i = 0; i < 12; i++) { | ||
sum += cnpjBase[i] * weight[i]; | ||
} | ||
sum += firstVerifier * weight[12]; | ||
|
||
final int remainder = sum % 11; | ||
|
||
return remainder < 2 ? 0 : 11 - remainder; | ||
} | ||
|
||
public static boolean cnpjIsValid(String cnpj) { | ||
if (cnpj == null) { | ||
throw new NullPointerException("CNPJ cannot be null or empty"); | ||
} | ||
|
||
final String cnpjClean = cnpj.replaceAll("\\D", ""); | ||
|
||
if (cnpjClean.isEmpty()) { | ||
return false; | ||
} | ||
|
||
if (cnpjClean.length() != 14) { | ||
return false; | ||
} | ||
|
||
// Convert the string to an array of integers | ||
final int[] cnpjArray = cnpjClean.chars().map(Character::getNumericValue).toArray(); | ||
|
||
// Calculate the first verifier and second verifier | ||
final int[] cnpjBase = Arrays.copyOfRange(cnpjArray, 0, 12); | ||
final int firstVerifier = calculateFirstVerifier(cnpjBase); | ||
|
||
final int[] cnpjBaseWithFirstVerifier = Arrays.copyOf(cnpjBase, cnpjBase.length + 1); | ||
cnpjBaseWithFirstVerifier[cnpjBaseWithFirstVerifier.length - 1] = firstVerifier; | ||
|
||
final int secondVerifier = calculateSecondVerifier(cnpjBaseWithFirstVerifier, firstVerifier); | ||
|
||
return cnpjArray[12] == firstVerifier && cnpjArray[13] == secondVerifier; | ||
} | ||
} |
76 changes: 0 additions & 76 deletions
76
packages/java/src/main/java/io/multiform_validator/Cpf.java
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
packages/java/src/main/java/io/multiform_validator/CpfValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.multiform_validator; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.IntStream; | ||
|
||
public class CpfValidator { | ||
private CpfValidator() { | ||
throw new IllegalStateException("Utility class"); | ||
} | ||
public static boolean cpfIsValid(String cpf) { | ||
if (cpf == null) { | ||
throw new NullPointerException("CPF cannot be null or empty"); | ||
} | ||
|
||
final String cpfClean = cpf.replaceAll("\\D", ""); | ||
|
||
if (cpfClean.length() != 11) { | ||
return false; | ||
} | ||
|
||
Pattern pattern = Pattern.compile("(\\d)\\1{10}"); | ||
Matcher matcher = pattern.matcher(cpfClean); | ||
|
||
if (matcher.find()) { | ||
return false; | ||
} | ||
|
||
final int[] cpfArray = cpfClean.chars().map(Character::getNumericValue).toArray(); | ||
|
||
final int sum1 = IntStream.range(0, 9).map(i -> cpfArray[i] * (10 - i)).sum(); | ||
final int sum2 = IntStream.range(0, 10).map(i -> cpfArray[i] * (11 - i)).sum(); | ||
|
||
final int validator1 = sum1 % 11 < 2 ? 0 : 11 - (sum1 % 11); | ||
final int validator2 = sum2 % 11 < 2 ? 0 : 11 - (sum2 % 11); | ||
|
||
return cpfArray[9] == validator1 && cpfArray[10] == validator2; | ||
} | ||
} |
Oops, something went wrong.