Skip to content

Commit

Permalink
Raise descriptive IllegalArgumentException when zipcode is -
Browse files Browse the repository at this point in the history
Summary:
Fixes this issue - facebook/facebook-java-business-sdk#460

> When executing a postal code validation on a String containing only "-" an ArrayIndexOutOfBoundsException is thrown.

X-link: facebook/facebook-java-business-sdk#468

Reviewed By: yoongyj

Differential Revision: D62392830

Pulled By: stcheng

fbshipit-source-id: ba84bf0dd10eb9006dbde0da966f09542857e2c1
  • Loading branch information
ascii-dev authored and facebook-github-bot committed Sep 11, 2024
1 parent 8ef024c commit e0c84ff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static String hash(String input) {
* Checks whether the input is already hashed with SHA256 or MD5.
*
* @param input value that need to be validated.
* @return bool representing the whether the input is hashed
* @return bool representing whether the input is hashed
*/
public static boolean isAlreadyHashed(String input) {

Expand Down Expand Up @@ -170,9 +170,15 @@ private static String normalizePostalCode(String postalCode) {
// Remove space characters in the Zip Code
postalCode = postalCode.replaceAll("[\\s]+", "");

// If the code has more than one part, retain the first part.
postalCode = postalCode.split("-")[0];
// If the code is just a hyphen throw a descriptive IllegalArgumentException instead of
// ArrayIndexOutOfBoundsException below.
String[] postalCodeArray = postalCode.split("-");
if (postalCodeArray.length == 0) {
throw new IllegalArgumentException("Invalid postalcode for the passed postalCode: '" + postalCode + "'.");
}

// If the code has more than one part, retain the first part.
postalCode = postalCodeArray[0];
if (postalCode.length() < 2) {
throw new IllegalArgumentException("Invalid postalcode format for the passed postalCode:" + postalCode + ". Please check the passed postalcode.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,11 @@ public void NormalizeSanitizesEmptyFieldsToNull() {
assertNull(normalize(" \t ", param));
}
}
}

@Test
public void NormalizeHyphenPostalCodeThrowsIllegalArgumentException() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Invalid postalcode for the passed postalCode: '-'.");
normalize("-", ZIP_CODE);
}
}

0 comments on commit e0c84ff

Please sign in to comment.