Skip to content

Commit

Permalink
edited regex to handle whitespace,added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arinkulshi-skylight committed Feb 6, 2025
1 parent 965382b commit e23846f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class DateTimeUtils {
DateTimeFormatter.ofPattern("M/d/yyyy[ H:mm]");

public static final String TIMEZONE_SUFFIX_REGEX =
"^(0?[1-9]|1[0-2])/(0?[1-9]|1\\d|2\\d|3[01])/\\d{4}( ([0-1]?\\d|2[0-3]):[0-5]\\d)( \\S+)$";
"^(0?[1-9]|1[0-2])\\s*/\\s*(0?[1-9]|1\\d|2\\d|3[01])\\s*/\\s*\\d{4}\\s+([0-1]?\\d|2[0-3])\\s*:\\s*[0-5]\\d\\s+(\\S+)\\s*$";

public static final ZoneId FALLBACK_TIMEZONE_ID = ZoneId.of("US/Eastern");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public class CsvValidatorUtils {
* @see gov.cdc.usds.simplereport.utils.DateTimeUtils
*/
private static final String DATE_TIME_REGEX =
"^(0{0,1}[1-9]|1[0-2])\\/(0{0,1}[1-9]|1\\d|2\\d|3[01])\\/\\d{4}( ([0-1]?[0-9]|2[0-3]):[0-5][0-9]( \\S+)?)?$";
"^(0{0,1}[1-9]|1[0-2])\\s*/\\s*(0{0,1}[1-9]|1\\d|2\\d|3[01])\\s*/\\s*\\d{4}(\\s+([0-1]?[0-9]|2[0-3])\\s*:\\s*[0-5][0-9](\\s+\\S+)?)?\\s*$";

private static final String LOINC_CODE_REGEX = "([0-9]{5})-[0-9]";
private static final String EMAIL_REGEX = "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$";
Expand Down Expand Up @@ -448,11 +448,16 @@ public static List<FeedbackMessage> validateDateFormat(ValueOrError input) {
}

public static List<FeedbackMessage> validateDateTime(ValueOrError input) {
List<FeedbackMessage> errors = new ArrayList<>(validateRegex(input, DATE_TIME_REGEX));
ValueOrError trimmedInput =
new ValueOrError(
input.getValue() != null ? input.getValue().trim() : null,
input.getHeader(),
input.isRequired());
List<FeedbackMessage> errors = new ArrayList<>(validateRegex(trimmedInput, DATE_TIME_REGEX));
if (input.getValue() != null
&& errors.isEmpty()
&& input.getValue().matches(TIMEZONE_SUFFIX_REGEX)) {
errors.addAll(validateDateTimeZoneCode(input));
&& trimmedInput.getValue().matches(TIMEZONE_SUFFIX_REGEX)) {
errors.addAll(validateDateTimeZoneCode(trimmedInput));
}
return errors;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,40 @@ void validDateFormat() {
}
}

@Test
void validDateTimeWithWhitespace() {
var validDateTimes = new ArrayList<ValueOrError>();
validDateTimes.add(new ValueOrError("01/01/2023 11:11", "datetime"));
validDateTimes.add(new ValueOrError(" 01/01/2023 11:11 ", "datetime"));
validDateTimes.add(new ValueOrError("01 / 01 / 2023 11 : 11", "datetime"));
validDateTimes.add(new ValueOrError("01/01/2023 11:11", "datetime"));
validDateTimes.add(new ValueOrError("01/01/2023 11:11 EST", "datetime"));
validDateTimes.add(new ValueOrError("01/01/2023 11:11 EST", "datetime"));
validDateTimes.add(new ValueOrError("01 / 01 / 2023 11 : 11 EST", "datetime"));
validDateTimes.add(new ValueOrError("01/01/2023 11:11 EST", "datetime"));
validDateTimes.add(new ValueOrError(" 01/01/2023 11:11 EST ", "datetime"));

for (var datetime : validDateTimes) {
assertThat(validateDateTime(datetime)).isEmpty();
}
}

@Test
void invalidDateTimeWithWhitespace() {
var invalidDateTimes = new ArrayList<ValueOrError>();
invalidDateTimes.add(
new ValueOrError("01/01/202311:11", "datetime")); // No space between date and time
invalidDateTimes.add(
new ValueOrError("01 01 2023 11:11", "datetime")); // Spaces instead of slashes
invalidDateTimes.add(
new ValueOrError("01/01/2023 11 11", "datetime")); // Space instead of colon
invalidDateTimes.add(
new ValueOrError("01/01/2023 11:11EST", "datetime")); // No space before timezone
for (var datetime : invalidDateTimes) {
assertThat(validateDateTime(datetime)).hasSize(1);
}
}

@Test
void invalidDateFormat() {
var invalidDates = new ArrayList<ValueOrError>();
Expand Down

0 comments on commit e23846f

Please sign in to comment.