Skip to content

Commit

Permalink
refactor: utils and validate to use best practices
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Jul 12, 2024
1 parent 2db50d4 commit ec68e03
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 107 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 3 additions & 15 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 59 additions & 34 deletions src/main/java/io/github/multiform_validator/Utils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.multiform_validator;

import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.regex.*;

Expand Down Expand Up @@ -36,14 +38,34 @@ public static class GetOnlyEmailOptionsParams {
* @return The extracted email addresses.
*/
public static Object getOnlyEmail(String text, GetOnlyEmailOptionsParams options) {
options = validateOptions(options);

List<String> matches = getEmailMatches(text);

if (matches.isEmpty()) {
return "No email found";
}

if (options.cleanDomain != null && !options.cleanDomain.equals(false)) {
matches = cleanDomain(matches, options.cleanDomain);
}

return handleRepeatEmail(matches, options);
}

private static @NotNull GetOnlyEmailOptionsParams validateOptions(GetOnlyEmailOptionsParams options) {
if (options == null) {
options = getOnlyEmailDefaultOptionsParams;
}

Boolean multiple = options.multiple != null ? options.multiple : getOnlyEmailDefaultOptionsParams.multiple;
Object cleanDomain = options.cleanDomain != null ? options.cleanDomain : getOnlyEmailDefaultOptionsParams.cleanDomain;
Boolean repeatEmail = options.repeatEmail != null ? options.repeatEmail : getOnlyEmailDefaultOptionsParams.repeatEmail;
options.multiple = options.multiple != null ? options.multiple : getOnlyEmailDefaultOptionsParams.multiple;
options.cleanDomain = options.cleanDomain != null ? options.cleanDomain : getOnlyEmailDefaultOptionsParams.cleanDomain;
options.repeatEmail = options.repeatEmail != null ? options.repeatEmail : getOnlyEmailDefaultOptionsParams.repeatEmail;

return options;
}

private static @NotNull List<String> getEmailMatches(String text) {
Pattern emailPattern = Pattern.compile("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}");
Matcher matcher = emailPattern.matcher(text);

Expand All @@ -52,46 +74,49 @@ public static Object getOnlyEmail(String text, GetOnlyEmailOptionsParams options
matches.add(matcher.group());
}

if (matches.isEmpty()) {
return "No email found";
}
return matches;
}

if (cleanDomain != null && !cleanDomain.equals(false)) {
List<String> domainsToClean = cleanDomain instanceof List ? (List<String>) cleanDomain : GetOnlyEmailCleanAfterDefaultDomain;

List<String> cleanedEmails = new ArrayList<>();
for (String email : matches) {
for (String domain : domainsToClean) {
int index = email.lastIndexOf(domain);
if (index != -1) {
email = email.substring(0, index + domain.length());
break;
}
}
private static @NotNull List<String> cleanDomain(@NotNull List<String> emails, Object cleanDomain) {
List<String> domainsToClean;

for (String domain : domainsToClean) {
int index = email.indexOf(domain);
if (index != -1) {
email = email.substring(0, index + domain.length());
break;
}
if (cleanDomain instanceof List) {
@SuppressWarnings("unchecked")
List<String> castedList = (List<String>) cleanDomain;
domainsToClean = castedList;
} else {
domainsToClean = GetOnlyEmailCleanAfterDefaultDomain;
}

List<String> cleanedEmails = new ArrayList<>();
for (String email : emails) {
for (String domain : domainsToClean) {
int index = email.lastIndexOf(domain);
if (index != -1) {
email = email.substring(0, index + domain.length());
break;
}
cleanedEmails.add(email);
}

if (Boolean.FALSE.equals(repeatEmail)) {
Set<String> uniqueEmails = new LinkedHashSet<>(cleanedEmails);
return Boolean.TRUE.equals(multiple) ? new ArrayList<>(uniqueEmails) : uniqueEmails.iterator().next();
for (String domain : domainsToClean) {
int index = email.indexOf(domain);
if (index != -1) {
email = email.substring(0, index + domain.length());
break;
}
}

return Boolean.TRUE.equals(multiple) ? cleanedEmails : cleanedEmails.get(0);
cleanedEmails.add(email);
}

if (Boolean.FALSE.equals(repeatEmail)) {
Set<String> uniqueEmails = new LinkedHashSet<>(matches);
return Boolean.TRUE.equals(multiple) ? new ArrayList<>(uniqueEmails) : uniqueEmails.iterator().next();
return cleanedEmails;
}

private static Object handleRepeatEmail(List<String> emails, @NotNull GetOnlyEmailOptionsParams options) {
if (Boolean.FALSE.equals(options.repeatEmail)) {
Set<String> uniqueEmails = new LinkedHashSet<>(emails);
return Boolean.TRUE.equals(options.multiple) ? new ArrayList<>(uniqueEmails) : uniqueEmails.iterator().next();
}

return Boolean.TRUE.equals(multiple) ? matches : matches.get(0);
return Boolean.TRUE.equals(options.multiple) ? emails : emails.get(0);
}
}
41 changes: 26 additions & 15 deletions src/main/java/io/github/multiform_validator/Validate.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.multiform_validator;

import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.regex.*;

Expand Down Expand Up @@ -66,36 +68,46 @@ public static boolean validateEmail(String email) {
* @throws IllegalArgumentException if the input value is empty or if both validDomains and validDomainsList are used at the same time.
*/
public static boolean validateEmail(String email, ValidateEmailOptionsParams options) {
if (options == null) {
options = validateEmailDefaultOptionsParams;
}
options = validateOptions(options);

if (email == null || email.isEmpty()) {
throw new IllegalArgumentException("Input value cannot be empty.");
}

Pattern regex = createRegexPattern(options);

return validateEmailWithRegex(email, regex, options);
}

private static @NotNull ValidateEmailOptionsParams validateOptions(ValidateEmailOptionsParams options) {
if (options == null) {
options = validateEmailDefaultOptionsParams;
}

if (options.validDomains && !options.validDomainsList.isEmpty()) {
throw new IllegalArgumentException("Cannot use validDomains and validDomainsList at the same time.");
}

int maxLength = options.maxLength > 0 ? options.maxLength : validateEmailDefaultOptionsParams.maxLength;
String country = options.country != null ? options.country : validateEmailDefaultOptionsParams.country;
return options;
}

private static @NotNull Pattern createRegexPattern(@NotNull ValidateEmailOptionsParams options) {
List<String> validDomains = options.validDomains ? validateEmailDefaultOptionsParams.validDomainsList : options.validDomainsList;

// Initialize with an empty regular expression
Pattern regex = Pattern.compile("");
List<String> validDomainsCustom = new ArrayList<>();

if (validDomains != null && !validDomains.isEmpty()) {
for (String domain : validDomains) {
validDomainsCustom.add(domain.replaceAll("[.*+?^${}()|\\[\\]\\\\]", "\\\\$0"));
}
regex = Pattern.compile(String.join("|", validDomainsCustom) + "$", Pattern.CASE_INSENSITIVE);
return Pattern.compile(String.join("|", validDomainsCustom) + "$", Pattern.CASE_INSENSITIVE);
} else if (validDomains != null) {
regex = Pattern.compile(String.join("|", validateEmailValidDomainsDefault) + "$", Pattern.CASE_INSENSITIVE);
return Pattern.compile(String.join("|", validateEmailValidDomainsDefault) + "$", Pattern.CASE_INSENSITIVE);
}

// Use the Matcher to check if the email ends with one of the valid domains
return Pattern.compile("");
}

private static boolean validateEmailWithRegex(String email, @NotNull Pattern regex, ValidateEmailOptionsParams options) {
Matcher matcher = regex.matcher(email);
if (!matcher.find()) {
return false;
Expand All @@ -105,13 +117,12 @@ public static boolean validateEmail(String email, ValidateEmailOptionsParams opt
return false;
}

if (email.length() > maxLength) {
if (email.length() > options.maxLength) {
return false;
}

// If country is provided, check if the email ends with the country code
if (country != null && !country.isEmpty()) {
return email.endsWith("." + country);
if (options.country != null && !options.country.isEmpty()) {
return email.endsWith("." + options.country);
}

return true;
Expand Down
38 changes: 11 additions & 27 deletions src/main/java/io/github/multiform_validator/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,33 +311,17 @@ public static boolean isPostalCode(String postalCode) {
throw new IllegalArgumentException("Input value must be a string.");
}

final String usZipCodeRegex = "^\\d{5}(-\\d{4})?$";
final String canadaPostalCodeRegex = "^[A-Za-z]\\d[A-Za-z] \\d[A-Za-z]\\d$";
final String ukPostalCodeRegex = "^[A-Za-z]{1,2}\\d[A-Za-z\\d]? \\d[A-Za-z]{2}$";
final String francePostalCodeRegex = "^\\d{5}$";
final String netherlandsPostalCodeRegex = "^\\d{4}$";
final String japanPostalCodeRegex = "^\\d{3}-\\d{4}$";
final String spainPostalCodeRegex = "^\\d{5}$";
final String southAfricaPostalCodeRegex = "^\\d{4}$";
final String germanyPostalCodeRegex = "^\\d{5}$";
final String switzerlandPostalCodeRegex = "^\\d{4}$";
final String brazilPostalCodeRegex = "^\\d{5}-\\d{3}$";
final String italyPostalCodeRegex = "^\\d{5}$";
final String usZipCodeOnlyRegex = "^\\d{5}$";

return postalCode.matches(usZipCodeRegex) ||
postalCode.matches(canadaPostalCodeRegex) ||
postalCode.matches(ukPostalCodeRegex) ||
postalCode.matches(francePostalCodeRegex) ||
postalCode.matches(netherlandsPostalCodeRegex) ||
postalCode.matches(japanPostalCodeRegex) ||
postalCode.matches(spainPostalCodeRegex) ||
postalCode.matches(southAfricaPostalCodeRegex) ||
postalCode.matches(germanyPostalCodeRegex) ||
postalCode.matches(switzerlandPostalCodeRegex) ||
postalCode.matches(brazilPostalCodeRegex) ||
postalCode.matches(italyPostalCodeRegex) ||
postalCode.matches(usZipCodeOnlyRegex);
final String REGEX_TEST1 = "^\\d{5}(-\\d{4})?$"; // US ZIP code
final String REGEX_TEST2 = "^[A-Za-z]\\d[A-Za-z] \\d[A-Za-z]\\d$"; // Canada postal code
final String REGEX_TEST3 = "^[A-Za-z]{1,2}\\d[A-Za-z\\d]? \\d[A-Za-z]{2}$"; // UK postal code
final String REGEX_TEST4 = "^\\d{5}$"; // France, Spain, Italy, Germany, US postal code
final String REGEX_TEST5 = "^\\d{4}$"; // Netherlands, South Africa, Switzerland postal code
final String REGEX_TEST6 = "^\\d{3}-\\d{4}$"; // Japan postal code
final String REGEX_TEST7 = "^\\d{5}-\\d{3}$"; // Brazil postal code

return postalCode.matches(REGEX_TEST1) || postalCode.matches(REGEX_TEST2) || postalCode.matches(REGEX_TEST3)
|| postalCode.matches(REGEX_TEST4) || postalCode.matches(REGEX_TEST5) || postalCode.matches(REGEX_TEST6)
|| postalCode.matches(REGEX_TEST7);
}

// ##############################################################################################################
Expand Down
Loading

0 comments on commit ec68e03

Please sign in to comment.