Skip to content

Commit 7aaa5b0

Browse files
committed
fixes #54 pass the common buildin-formats to each spec instance
1 parent b2741f9 commit 7aaa5b0

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

Diff for: src/main/java/com/networknt/schema/JsonMetaSchema.java

+17-13
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,34 @@ public class JsonMetaSchema {
4242
static PatternFormat pattern(String name, String regex) {
4343
return new PatternFormat(name, regex);
4444
}
45-
public static final List<Format> BUILTIN_FORMATS = new ArrayList<Format>();
45+
public static final List<Format> COMMON_BUILTIN_FORMATS = new ArrayList<Format>();
4646

4747
// this section contains formats that is common for all specification versions.
4848
static {
49-
BUILTIN_FORMATS.add(pattern("time", "^\\d{2}:\\d{2}:\\d{2}$"));
50-
BUILTIN_FORMATS.add(pattern("ip-address",
49+
COMMON_BUILTIN_FORMATS.add(pattern("time", "^\\d{2}:\\d{2}:\\d{2}$"));
50+
COMMON_BUILTIN_FORMATS.add(pattern("ip-address",
5151
"^(?:(?: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]?)$"));
52-
BUILTIN_FORMATS.add(pattern("ipv4",
52+
COMMON_BUILTIN_FORMATS.add(pattern("ipv4",
5353
"^(?:(?: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]?)$"));
54-
BUILTIN_FORMATS.add(pattern("ipv6",
54+
COMMON_BUILTIN_FORMATS.add(pattern("ipv6",
5555
"^\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?\\s*$"));
56-
BUILTIN_FORMATS.add(pattern("uri", "(^[a-zA-Z][a-zA-Z0-9+-.]*:[^\\s]*$)|(^//[^\\s]*$)"));
57-
BUILTIN_FORMATS.add(pattern("color",
56+
COMMON_BUILTIN_FORMATS.add(pattern("uri", "(^[a-zA-Z][a-zA-Z0-9+-.]*:[^\\s]*$)|(^//[^\\s]*$)"));
57+
COMMON_BUILTIN_FORMATS.add(pattern("color",
5858
"(#?([0-9A-Fa-f]{3,6})\\b)|(aqua)|(black)|(blue)|(fuchsia)|(gray)|(green)|(lime)|(maroon)|(navy)|(olive)|(orange)|(purple)|(red)|(silver)|(teal)|(white)|(yellow)|(rgb\\(\\s*\\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\b\\s*,\\s*\\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\b\\s*,\\s*\\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\b\\s*\\))|(rgb\\(\\s*(\\d?\\d%|100%)+\\s*,\\s*(\\d?\\d%|100%)+\\s*,\\s*(\\d?\\d%|100%)+\\s*\\))"));
59-
BUILTIN_FORMATS.add(pattern("hostname",
59+
COMMON_BUILTIN_FORMATS.add(pattern("hostname",
6060
"^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])(\\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]))*$"));
61-
BUILTIN_FORMATS.add(pattern("alpha", "^[a-zA-Z]+$"));
62-
BUILTIN_FORMATS.add(pattern("alphanumeric", "^[a-zA-Z0-9]+$"));
63-
BUILTIN_FORMATS.add(pattern("phone", "^\\+(?:[0-9] ?){6,14}[0-9]$"));
64-
BUILTIN_FORMATS.add(pattern("utc-millisec", "^[0-9]+(\\.?[0-9]+)?$"));
65-
BUILTIN_FORMATS.add(pattern("style", "\\s*(.+?):\\s*([^;]+);?"));
61+
COMMON_BUILTIN_FORMATS.add(pattern("alpha", "^[a-zA-Z]+$"));
62+
COMMON_BUILTIN_FORMATS.add(pattern("alphanumeric", "^[a-zA-Z0-9]+$"));
63+
COMMON_BUILTIN_FORMATS.add(pattern("phone", "^\\+(?:[0-9] ?){6,14}[0-9]$"));
64+
COMMON_BUILTIN_FORMATS.add(pattern("utc-millisec", "^[0-9]+(\\.?[0-9]+)?$"));
65+
COMMON_BUILTIN_FORMATS.add(pattern("style", "\\s*(.+?):\\s*([^;]+);?"));
6666
}
6767

6868
private static class V4 {
6969
private static String URI = "http://json-schema.org/draft-04/schema#";
7070
private static final String ID = "id";
7171

72+
public static final List<Format> BUILTIN_FORMATS = new ArrayList<>(JsonMetaSchema.COMMON_BUILTIN_FORMATS);
7273
static {
7374
// add version specific formats here.
7475
//BUILTIN_FORMATS.add(pattern("phone", "^\\+(?:[0-9] ?){6,14}[0-9]$"));
@@ -97,6 +98,7 @@ private static class V6 {
9798
// Draft 6 uses "$id"
9899
private static final String ID = "$id";
99100

101+
public static final List<Format> BUILTIN_FORMATS = new ArrayList<>(JsonMetaSchema.COMMON_BUILTIN_FORMATS);
100102
static {
101103
// add version specific formats here.
102104
//BUILTIN_FORMATS.add(pattern("phone", "^\\+(?:[0-9] ?){6,14}[0-9]$"));
@@ -124,6 +126,7 @@ private static class V7 {
124126
private static String URI = "http://json-schema.org/draft-07/schema#";
125127
private static final String ID = "$id";
126128

129+
public static final List<Format> BUILTIN_FORMATS = new ArrayList<>(JsonMetaSchema.COMMON_BUILTIN_FORMATS);
127130
static {
128131
// add version specific formats here.
129132
//BUILTIN_FORMATS.add(pattern("phone", "^\\+(?:[0-9] ?){6,14}[0-9]$"));
@@ -151,6 +154,7 @@ private static class V201909 {
151154
private static String URI = "http://json-schema.org/draft/2019-09/schema#";
152155
private static final String ID = "$id";
153156

157+
public static final List<Format> BUILTIN_FORMATS = new ArrayList<>(JsonMetaSchema.COMMON_BUILTIN_FORMATS);
154158
static {
155159
// add version specific formats here.
156160
//BUILTIN_FORMATS.add(pattern("phone", "^\\+(?:[0-9] ?){6,14}[0-9]$"));

Diff for: src/test/java/com/networknt/schema/V201909JsonSchemaTest.java

+26
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,32 @@ public void testBignumValidator() throws Exception {
115115
runTestFile("draft2019-09/optional/bignum.json");
116116
}
117117

118+
@Test
119+
public void testFormatDateValidator() throws Exception {
120+
runTestFile("draft2019-09/optional/format/date.json");
121+
}
122+
123+
@Test
124+
public void testFormatDateTimeValidator() throws Exception {
125+
runTestFile("draft2019-09/optional/format/date-time.json");
126+
}
127+
128+
@Test
129+
public void testFormatEmailValidator() throws Exception {
130+
runTestFile("draft2019-09/optional/format/email.json");
131+
}
132+
133+
@Test
134+
public void testFormatHostnameValidator() throws Exception {
135+
runTestFile("draft2019-09/optional/format/hostname.json");
136+
}
137+
138+
@Test
139+
@Ignore
140+
public void testFormatIdnEmailValidator() throws Exception {
141+
runTestFile("draft2019-09/optional/format/idn-email.json");
142+
}
143+
118144
@Test
119145
public void testIfValidator() throws Exception {
120146
runTestFile("draft2019-09/if.json");

0 commit comments

Comments
 (0)