Skip to content

Commit

Permalink
feat: Add multiform_validator package with CnpjValidator, CpfValidato…
Browse files Browse the repository at this point in the history
…r, EmailValidator, and Validator classes
  • Loading branch information
gabriel-logan committed Jul 7, 2024
1 parent 17b923b commit f4be705
Show file tree
Hide file tree
Showing 31 changed files with 446 additions and 868 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/java-pr-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up JDK 11
uses: actions/setup-java@v3
- name: Set up JDK 8
uses: actions/setup-java@v4
with:
java-version: '11'
java-version: '8'
distribution: 'adopt'

- name: Build with Maven
Expand Down
4 changes: 2 additions & 2 deletions packages/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
Expand Down
26 changes: 0 additions & 26 deletions packages/java/src/main/java/io/multiform_validator/Ascii.java

This file was deleted.

27 changes: 0 additions & 27 deletions packages/java/src/main/java/io/multiform_validator/Base64.java

This file was deleted.

35 changes: 0 additions & 35 deletions packages/java/src/main/java/io/multiform_validator/CEP.java

This file was deleted.

70 changes: 0 additions & 70 deletions packages/java/src/main/java/io/multiform_validator/Cnpj.java

This file was deleted.

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 packages/java/src/main/java/io/multiform_validator/Cpf.java

This file was deleted.

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;
}
}
Loading

0 comments on commit f4be705

Please sign in to comment.