Skip to content
This repository was archived by the owner on Feb 10, 2023. It is now read-only.

Commit 01cdcd6

Browse files
committed
Date and Time formats are now strict.
Dates of the right format that make no sense (such as 2012-07-44) now do not validate. Times of the right format that make no sense (such as 13:44:75) now do not validate.
1 parent bd26004 commit 01cdcd6

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/main/java/uk/co/o2/json/schema/SimpleTypeSchema.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ public boolean isCompatibleType(SimpleType type) {
256256
public boolean isValid(JsonNode node) {
257257
String value = SimpleType.STRING.getValue(node).toString();
258258
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
259+
format.setLenient(false);
259260
ParsePosition position = new ParsePosition(0);
260261

261262
Date result = format.parse(value, position);
@@ -273,6 +274,7 @@ public boolean isCompatibleType(SimpleType type) {
273274
public boolean isValid(JsonNode node) {
274275
String value = SimpleType.STRING.getValue(node).toString();
275276
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
277+
format.setLenient(false);
276278
ParsePosition position = new ParsePosition(0);
277279

278280
Date result = format.parse(value, position);

src/test/java/uk/co/o2/json/schema/SimpleTypeSchemaTest.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void validate_shouldNotReturnAnErrorMessage_givenAFormatOfDateTimeAndAStr
124124

125125
@Test
126126
public void validate_shouldReturnAnErrorMessage_givenAFormatOfDateTimeAndAStringValueThatIsNotAValidIso8601DateTime() throws Exception {
127-
String invalidDateTime = "2011-May-10T165:11:17z";
127+
String invalidDateTime = "2011-05-44T11:11:17Z";
128128
SimpleTypeSchema schema = new SimpleTypeSchema();
129129
schema.setType(SimpleType.STRING);
130130
schema.setFormat("date-time");
@@ -138,6 +138,22 @@ public void validate_shouldReturnAnErrorMessage_givenAFormatOfDateTimeAndAString
138138
assertTrue(result.get(0).getMessage().contains("date-time"));
139139
}
140140

141+
@Test
142+
public void validate_shouldReturnAnErrorMessage_givenAFormatOfDateTimeAndAStringValueThatIsAnAlmostValidIso8601DateTime() throws Exception {
143+
String invalidDateTime = "2011-May-10T165:11:17z";
144+
SimpleTypeSchema schema = new SimpleTypeSchema();
145+
schema.setType(SimpleType.STRING);
146+
schema.setFormat("date-time");
147+
JsonNode nodeToValidate = new TextNode(invalidDateTime);
148+
149+
List<ErrorMessage> result = schema.validate(nodeToValidate);
150+
151+
assertEquals(1, result.size());
152+
assertEquals("", result.get(0).getLocation());
153+
assertTrue(result.get(0).getMessage().contains(invalidDateTime));
154+
assertTrue(result.get(0).getMessage().contains("date-time"));
155+
}
156+
141157
@Test
142158
public void setFormat_shouldThrowAnException_whenTypeIsNotStringAndFormatIsDateTime() throws Exception {
143159
SimpleTypeSchema schema = new SimpleTypeSchema();
@@ -178,6 +194,22 @@ public void validate_shouldReturnAnErrorMessage_givenAFormatOfDateAndAStringValu
178194
assertTrue(result.get(0).getMessage().contains("date"));
179195
}
180196

197+
@Test
198+
public void validate_shouldReturnAnErrorMessage_givenAFormatOfDateAndAStringValueThatIsAlmostAValidDate() throws Exception {
199+
String invalidDate = "2011-05-44";
200+
SimpleTypeSchema schema = new SimpleTypeSchema();
201+
schema.setType(SimpleType.STRING);
202+
schema.setFormat("date");
203+
JsonNode nodeToValidate = new TextNode(invalidDate);
204+
205+
List<ErrorMessage> result = schema.validate(nodeToValidate);
206+
207+
assertEquals(1, result.size());
208+
assertEquals("", result.get(0).getLocation());
209+
assertTrue( result.get(0).getMessage().contains(invalidDate));
210+
assertTrue(result.get(0).getMessage().contains("date"));
211+
}
212+
181213
@Test
182214
public void validate_shouldReturnAnErrorMessage_givenAFormatOfDateAndAStringValueThatIsAFullDateTime() throws Exception {
183215
String invalidDate = "2011-05-10T11:47:16Z";
@@ -234,6 +266,22 @@ public void validate_shouldReturnAnErrorMessage_givenAFormatOfTimeAndAStringValu
234266
assertTrue(result.get(0).getMessage().contains("time"));
235267
}
236268

269+
@Test
270+
public void validate_shouldReturnAnErrorMessage_givenAFormatOfTimeAndAStringValueThatIsAlmostAValidTime() throws Exception {
271+
String invalidTime = "13:75:47";
272+
SimpleTypeSchema schema = new SimpleTypeSchema();
273+
schema.setType(SimpleType.STRING);
274+
schema.setFormat("time");
275+
JsonNode nodeToValidate = new TextNode(invalidTime);
276+
277+
List<ErrorMessage> result = schema.validate(nodeToValidate);
278+
279+
assertEquals(1, result.size());
280+
assertEquals("", result.get(0).getLocation());
281+
assertTrue(result.get(0).getMessage().contains(invalidTime));
282+
assertTrue(result.get(0).getMessage().contains("time"));
283+
}
284+
237285
@Test
238286
public void validate_shouldReturnAnErrorMessage_givenAFormatOfTimeAndAStringValueThatHasExtraInformationAfterTheValidTime() throws Exception {
239287
String invalidTime = "11:47:16-blah";

0 commit comments

Comments
 (0)