Skip to content

Commit 602472c

Browse files
committed
Handle locale tags in date validator
1 parent 3a75f7d commit 602472c

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/main/java/org/owasp/esapi/reference/validation/annotations/ValidDateValidator.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public boolean isValid(String input, ConstraintValidatorContext constraintValida
3030
if (input == null) {
3131
return true;
3232
}
33-
Locale locale = new Locale(localeString);
33+
Locale locale = toLocale(localeString);
3434
DateFormat dateFormat = DateFormat.getDateInstance(dateStyle, locale);
3535

3636
ValidationErrorList errorList = new ValidationErrorList();
@@ -42,4 +42,16 @@ public boolean isValid(String input, ConstraintValidatorContext constraintValida
4242

4343
return valid;
4444
}
45+
46+
private static Locale toLocale(String localeValue) {
47+
if (localeValue == null) {
48+
return Locale.getDefault();
49+
}
50+
String normalized = localeValue.trim();
51+
if (normalized.isEmpty()) {
52+
return Locale.getDefault();
53+
}
54+
Locale locale = Locale.forLanguageTag(normalized.replace('_', '-'));
55+
return Locale.ROOT.equals(locale) ? Locale.getDefault() : locale;
56+
}
4557
}

0 commit comments

Comments
 (0)