Skip to content

Commit 3af631f

Browse files
committed
bug fix related to union type in anyOf; test cases for union types
1 parent 506ec3f commit 3af631f

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
5151
if (schema.validators.containsKey(typeValidatorName)) {
5252
TypeValidator typeValidator = ((TypeValidator) schema.validators.get(typeValidatorName));
5353
//If schema has type validator and node type doesn't match with schemaType then ignore it
54-
if (!typeValidator.equalsToSchemaType(node)) {
54+
//For union type, it is must to call TypeValidator
55+
if (typeValidator.getSchemaType() != JsonType.UNION && !typeValidator.equalsToSchemaType(node)) {
5556
expectedTypeList.add(typeValidator.getSchemaType().toString());
5657
continue;
5758
}

src/test/java/com/networknt/schema/JsonSchemaTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ public void testTypeValidator() throws Exception {
269269
runTestFile("tests/type.json");
270270
}
271271

272+
@Test
273+
public void testUnionTypeValidator() throws Exception {
274+
runTestFile("tests/union_type.json");
275+
}
276+
272277
@Test
273278
public void testUniqueItemsValidator() throws Exception {
274279
runTestFile("tests/uniqueItems.json");
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
[
2+
{
3+
"description": "test case with union types",
4+
"schema": {
5+
"type": "object",
6+
"properties": {
7+
"vars": {
8+
"type": "array",
9+
"items": {
10+
"anyOf": [
11+
{
12+
"type": [
13+
"string",
14+
"number"
15+
]
16+
}
17+
]
18+
}
19+
}
20+
},
21+
"required": [
22+
"vars"
23+
]
24+
},
25+
"tests": [
26+
{
27+
"description": "an item from valid union types",
28+
"data": {"vars":[232]},
29+
"valid": true
30+
},
31+
{
32+
"description": "an item from valid union types",
33+
"data": {"vars":["test"]},
34+
"valid": true
35+
},
36+
{
37+
"description": "an item other than valid union types",
38+
"data": {"vars":[true]},
39+
"valid": false
40+
}
41+
]
42+
},
43+
{
44+
"description": "test case with union types",
45+
"schema": {
46+
"type": "object",
47+
"properties": {
48+
"vars": {
49+
"type": [
50+
"string",
51+
"number"
52+
]
53+
}
54+
},
55+
"required": [
56+
"vars"
57+
]
58+
},
59+
"tests": [
60+
{
61+
"description": "an item other than valid union types",
62+
"data": {"vars": true},
63+
"valid": false
64+
},
65+
{
66+
"description": "an item from valid union types",
67+
"data": {"vars":232},
68+
"valid": true
69+
},
70+
{
71+
"description": "an item from valid union types",
72+
"data": {"vars":"test"},
73+
"valid": true
74+
}
75+
]
76+
77+
}
78+
]

0 commit comments

Comments
 (0)