Skip to content

Commit 25fbfeb

Browse files
committed
Added time and date-time type check
1 parent 66fa314 commit 25fbfeb

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

src/main/java/com/networknt/schema/DateTimeValidator.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@
3131
public class DateTimeValidator extends BaseJsonValidator implements JsonValidator {
3232
private static final Logger logger = LoggerFactory.getLogger(DateTimeValidator.class);
3333

34+
private final String formatName;
35+
private final String DATE = "date";
36+
private final String DATETIME = "date-time";
37+
3438
private static final Pattern RFC3339_PATTERN = Pattern.compile(
3539
"^(\\d{4})-(\\d{2})-(\\d{2})" // yyyy-MM-dd
3640
+ "([Tt](\\d{2}):(\\d{2}):(\\d{2})(\\.\\d+)?)?" // 'T'HH:mm:ss.milliseconds
3741
+ "([Zz]|([+-])(\\d{2}):(\\d{2}))?");
3842

39-
public DateTimeValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
43+
public DateTimeValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, String formatName) {
4044
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.DATETIME, validationContext);
45+
this.formatName = formatName;
4146
parseErrorCode(getValidatorType().getErrorCodeKey());
4247
}
4348

@@ -82,9 +87,15 @@ private boolean isLegalDateTime(String string) {
8287
String timeShiftHour = null;
8388
String timeShiftMinute = null;
8489

90+
if (!isTimeGiven && DATETIME.equals(formatName) || (isTimeGiven && DATE.equals(formatName))) {
91+
logger.error("The supplied date/time format type does not match the specification, expected: " + formatName);
92+
return false;
93+
}
94+
8595
if (!isTimeGiven && isTimeZoneShiftGiven) {
86-
throw new NumberFormatException("Invalid date/time format, cannot specify time zone shift" +
96+
logger.error("Invalid date/time format, cannot specify time zone shift" +
8797
" without specifying time: " + string);
98+
return false;
8899
}
89100

90101
if (isTimeGiven) {

src/main/java/com/networknt/schema/FormatKeyword.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSc
4747
format = formats.get(formatName);
4848
// Validate date and time separately
4949
if (formatName.equals(DATE) || formatName.equals(DATE_TIME)) {
50-
return new DateTimeValidator(schemaPath, schemaNode, parentSchema, validationContext);
50+
return new DateTimeValidator(schemaPath, schemaNode, parentSchema, validationContext, formatName);
5151
}
5252
}
5353
return new FormatValidator(schemaPath, schemaNode, parentSchema, validationContext, format);

src/test/resources/tests/optional/format.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,21 @@
9797
"description": "a valid date-time string",
9898
"data": "2019-02-28T08:30:59.99999z",
9999
"valid": true
100+
},
101+
{
102+
"description": "an incomplete date-time string",
103+
"data": "2019-02-28",
104+
"valid": false
105+
},
106+
{
107+
"description": "an incomplete date-time string",
108+
"data": "2019-02-28-08:00",
109+
"valid": false
110+
},
111+
{
112+
"description": "an incomplete date-time string",
113+
"data": "2019-02-28Z",
114+
"valid": false
100115
}
101116
]
102117
},
@@ -114,6 +129,11 @@
114129
"data": "1963-12-35",
115130
"valid": false
116131
},
132+
{
133+
"description": "an invalid date string",
134+
"data": "2019-02-28T08:30:59.99999z",
135+
"valid": false
136+
},
117137
{
118138
"description": "a valid date string",
119139
"data": "1963-12-31",

0 commit comments

Comments
 (0)