Skip to content

Commit 56a36ac

Browse files
committed
Merge branch 'master' of github.com:networknt/json-schema-validator
2 parents 34c4fbb + dcc76e4 commit 56a36ac

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static PatternFormat pattern(String name, String regex) {
4040

4141
// this section contains formats that is common for all specification versions.
4242
static {
43-
COMMON_BUILTIN_FORMATS.add(pattern("time", "^\\d{2}:\\d{2}:\\d{2}(?:\\.\\d+)?$"));
43+
COMMON_BUILTIN_FORMATS.add(pattern("time", "^(([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]|(24:00:00))(?:\\.\\d+)?(Z|[+-]((0[0-9]|1[0-3]):[0-5][0-9]|14:00))?$"));
4444
COMMON_BUILTIN_FORMATS.add(pattern("ip-address",
4545
"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"));
4646
COMMON_BUILTIN_FORMATS.add(pattern("ipv4", "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$"));
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.networknt.schema;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.BeforeAll;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
import java.io.InputStream;
12+
import java.util.Set;
13+
import java.util.stream.Stream;
14+
15+
public class Issue669Test {
16+
private static JsonSchema schema;
17+
18+
@BeforeAll
19+
static void init() {
20+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
21+
String schemaPath = "/draft7/issue669.json";
22+
InputStream schemaInputStream = Issue669Test.class.getResourceAsStream(schemaPath);
23+
schema = factory.getSchema(schemaInputStream);
24+
}
25+
26+
public static Stream<Arguments> validTimeZoneOffsets() {
27+
String json1 = "{\"testTime\":\"08:27:53-05:00\"}"; // America/New_York
28+
String json2 = "{\"testTime\":\"08:27:53-04:00\"}"; // America/New_York (DST)
29+
String json3 = "{\"testTime\":\"08:27:53-03:30\"}"; // America/St_Johns
30+
String json4 = "{\"testTime\":\"08:27:53-02:30\"}"; // America/St_Johns (DST)
31+
String json5 = "{\"testTime\":\"08:27:53+02:00\"}"; // Africa/Cairo
32+
String json6 = "{\"testTime\":\"08:27:53+03:30\"}"; // Asia/Tehran
33+
String json7 = "{\"testTime\":\"08:27:53+04:30\"}"; // Asia/Tehran (DST)
34+
String json8 = "{\"testTime\":\"08:27:53+04:00\"}"; // Asia/Dubai
35+
String json9 = "{\"testTime\":\"08:27:53+05:00\"}"; // Indian/Maldives
36+
String json10 = "{\"testTime\":\"08:27:53+05:30\"}"; // Asia/Kolkata
37+
String json11 = "{\"testTime\":\"08:27:53+10:00\"}"; // Australia/Sydney
38+
String json12 = "{\"testTime\":\"08:27:53+11:00\"}"; // Australia/Sydney (DST)
39+
String json13 = "{\"testTime\":\"08:27:53+14:00\"}"; // Pacific/Kiritimati
40+
String json14 = "{\"testTime\":\"18:45:32.123-05:00\"}"; // America/New_York
41+
String json15 = "{\"testTime\":\"18:45:32.123456-05:00\"}"; // America/New_York
42+
String json16 = "{\"testTime\":\"08:27:53Z\"}"; // UTC
43+
String json17 = "{\"testTime\":\"08:27:53+00:00\"}"; // UTC
44+
45+
return Stream.of(
46+
Arguments.of(json1),
47+
Arguments.of(json2),
48+
Arguments.of(json3),
49+
Arguments.of(json4),
50+
Arguments.of(json5),
51+
Arguments.of(json6),
52+
Arguments.of(json7),
53+
Arguments.of(json8),
54+
Arguments.of(json9),
55+
Arguments.of(json10),
56+
Arguments.of(json11),
57+
Arguments.of(json12),
58+
Arguments.of(json13),
59+
Arguments.of(json14),
60+
Arguments.of(json15),
61+
Arguments.of(json16),
62+
Arguments.of(json17)
63+
);
64+
}
65+
66+
/**
67+
* Confirms that valid time zone offsets do not result in a JSON validation error.
68+
*
69+
* @param jsonObject a sample JSON payload to test
70+
*/
71+
@ParameterizedTest
72+
@MethodSource("validTimeZoneOffsets")
73+
void testValidTimeZoneOffsets(String jsonObject) throws JsonProcessingException {
74+
Set<ValidationMessage> errors = schema.validate(new ObjectMapper().readTree(jsonObject));
75+
Assertions.assertTrue(errors.isEmpty());
76+
}
77+
78+
public static Stream<Arguments> invalidTimeRepresentations() {
79+
80+
// Invalid JSON payload: seconds missing from time
81+
String json1 = "{\"testTime\":\"11:23Z\"}";
82+
// Invalid JSON payload: Text instead of date-time value
83+
String json2 = "{\"testTime\":\"Orlando\"}";
84+
// Invalid JSON payload: A time zone offset of +23:00 is not valid
85+
String json3 = "{\"testTime\":\"08:27:53+23:00\"}";
86+
// Invalid JSON payload: A time zone offset of -23:00 is not valid
87+
String json4 = "{\"testTime\":\"08:27:53-23:00\"}";
88+
89+
return Stream.of(
90+
Arguments.of(json1),
91+
Arguments.of(json2),
92+
Arguments.of(json3),
93+
Arguments.of(json4)
94+
);
95+
}
96+
97+
/**
98+
* Confirms that invalid time representations result in one or more a JSON validation errors.
99+
*
100+
* @param jsonObject a sample JSON payload to test
101+
*/
102+
@ParameterizedTest
103+
@MethodSource("invalidTimeRepresentations")
104+
void testInvalidTimeRepresentations(String jsonObject) throws JsonProcessingException {
105+
Set<ValidationMessage> errors = schema.validate(new ObjectMapper().readTree(jsonObject));
106+
Assertions.assertFalse(errors.isEmpty());
107+
}
108+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "Test Time Zone Schema (for testing time zones with offsets)",
4+
"type": "object",
5+
"properties": {
6+
"testTime": {
7+
"description": "A time value with offsets.",
8+
"type": "string",
9+
"format": "time"
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)