Skip to content

Commit 3f0478b

Browse files
committed
Add schema tests (#1260)
Add extra tests for stricter schema validation. These tests pass against 3.0.4 (pre #1250) as well as 3.0.5 (post #1250).
1 parent 9575509 commit 3f0478b

8 files changed

Lines changed: 344 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.networknt.schema;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.io.IOException;
7+
8+
import org.junit.jupiter.api.BeforeAll;
9+
import org.junit.jupiter.params.ParameterizedTest;
10+
import org.junit.jupiter.params.provider.ValueSource;
11+
12+
class Issue1260Test extends BaseJsonSchemaValidatorTest {
13+
14+
private static final String RESOURCE_PREFIX = "issues/1260/";
15+
private static Schema schema;
16+
17+
@BeforeAll
18+
static void setup() {
19+
schema = getJsonSchemaFromClasspath(resource("schema.json"), SpecificationVersion.DRAFT_2020_12);
20+
}
21+
22+
@ParameterizedTest
23+
@ValueSource(strings = {"valid1.json", "valid2.json", "valid3.json"})
24+
void testNoErrorsForValidInput(String validJson) throws IOException {
25+
final var node = getJsonNodeFromClasspath(resource(validJson));
26+
final var errors = schema.validate(node);
27+
assertTrue(errors.isEmpty(), "No validation errors for valid input");
28+
}
29+
30+
@ParameterizedTest
31+
@ValueSource(strings = {"invalid1.json", "invalid2.json", "invalid3.json"})
32+
void testErrorsForInvalidInput(String invalidJson) throws IOException {
33+
final var node = getJsonNodeFromClasspath(resource(invalidJson));
34+
final var errors = schema.validate(node);
35+
assertFalse(errors.isEmpty(), "Validation errors for invalid input");
36+
}
37+
38+
private static String resource(String name) {
39+
return RESOURCE_PREFIX + name;
40+
}
41+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"fields": {
3+
"entity": {
4+
"status": {
5+
"label": "Status"
6+
},
7+
"assessments": {
8+
"entityOndemand": {
9+
"label": "Ondemand assessment of entity",
10+
"recurrence": "ondemand",
11+
"questions": null
12+
}
13+
}
14+
}
15+
}
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"fields": {
3+
"entity": {
4+
"status": {
5+
"label": "Status"
6+
},
7+
"assessments": {
8+
"entityOndemand": {
9+
"label": "Ondemand assessment of entity",
10+
"recurrence": "ondemand",
11+
"onDemandAssessmentExpirationDays": 180,
12+
"questions": {
13+
"assessmentDate": {
14+
"type": "date"
15+
}
16+
}
17+
}
18+
}
19+
}
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"fields": {
3+
"entity": {
4+
"status": {
5+
"label": "Status"
6+
},
7+
"assessments": {
8+
"entityOndemand": {
9+
"label": "Ondemand assessment of entity",
10+
"recurrence": "ondemand",
11+
"onDemandAssessmentExpirationDays": 180,
12+
"questions": {
13+
"expirationDate": {
14+
"type": "date"
15+
}
16+
}
17+
}
18+
}
19+
}
20+
}
21+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"$defs": {
4+
"labeledField": {
5+
"type": "object",
6+
"properties": {
7+
"label": {
8+
"type": "string"
9+
}
10+
}
11+
},
12+
"question": {
13+
"type": "object",
14+
"unevaluatedProperties": false,
15+
"required": [
16+
"type"
17+
],
18+
"properties": {
19+
"type": {
20+
"type": "string",
21+
"enum": [
22+
"date"
23+
]
24+
}
25+
}
26+
},
27+
"questions": {
28+
"type": "object",
29+
"additionalProperties": {
30+
"$ref": "#/$defs/question"
31+
}
32+
},
33+
"assessmentDef": {
34+
"type": "object",
35+
"unevaluatedProperties": false,
36+
"required": [
37+
"recurrence"
38+
],
39+
"properties": {
40+
"recurrence": {
41+
"type": "string",
42+
"enum": [
43+
"once",
44+
"monthly",
45+
"ondemand"
46+
],
47+
"default": "once"
48+
},
49+
"relatedObjectType": {
50+
"type": "string",
51+
"enum": [
52+
"otherEntity"
53+
]
54+
},
55+
"onDemandAssessmentExpirationDays": {
56+
"type": "integer",
57+
"minimum": 1
58+
},
59+
"label": {
60+
"type": "string"
61+
},
62+
"questions": {
63+
"$ref": "#/$defs/questions"
64+
}
65+
},
66+
"dependentSchemas": {
67+
"recurrence": {
68+
"oneOf": [
69+
{
70+
"properties": {
71+
"recurrence": {
72+
"enum": [
73+
"monthly"
74+
]
75+
}
76+
}
77+
},
78+
{
79+
"properties": {
80+
"recurrence": {
81+
"enum": [
82+
"once"
83+
]
84+
}
85+
},
86+
"required": [
87+
"relatedObjectType"
88+
]
89+
},
90+
{
91+
"properties": {
92+
"recurrence": {
93+
"enum": [
94+
"ondemand"
95+
]
96+
},
97+
"questions": {
98+
"allOf": [
99+
{
100+
"$ref": "#/$defs/questions"
101+
},
102+
{
103+
"properties": {
104+
"assessmentDate": {
105+
"$ref": "#/$defs/question"
106+
}
107+
}
108+
},
109+
{
110+
"properties": {
111+
"expirationDate": {
112+
"$ref": "#/$defs/question"
113+
}
114+
}
115+
}
116+
],
117+
"required": [
118+
"assessmentDate"
119+
]
120+
}
121+
}
122+
}
123+
]
124+
},
125+
"onDemandAssessmentExpirationDays": {
126+
"oneOf": [
127+
{
128+
"properties": {
129+
"questions": {
130+
"expirationDate": {
131+
"$ref": "#/$defs/question"
132+
},
133+
"required": [
134+
"expirationDate"
135+
]
136+
}
137+
}
138+
}
139+
]
140+
}
141+
}
142+
}
143+
},
144+
"type": "object",
145+
"additionalProperties": false,
146+
"required": [
147+
"fields"
148+
],
149+
"properties": {
150+
"fields": {
151+
"type": "object",
152+
"additionalProperties": false,
153+
"required": [
154+
"entity"
155+
],
156+
"properties": {
157+
"entity": {
158+
"type": "object",
159+
"additionalProperties": false,
160+
"required": [
161+
"status"
162+
],
163+
"properties": {
164+
"status": {
165+
"$ref": "#/$defs/labeledField"
166+
},
167+
"assessments": {
168+
"type": "object",
169+
"additionalProperties": {
170+
"$ref": "#/$defs/assessmentDef"
171+
}
172+
}
173+
}
174+
}
175+
}
176+
}
177+
}
178+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"fields": {
3+
"entity": {
4+
"status": {
5+
"label": "Status"
6+
},
7+
"assessments": {
8+
"entityOndemand": {
9+
"label": "Ondemand assessment of entity",
10+
"recurrence": "ondemand",
11+
"questions": {
12+
"assessmentDate": {
13+
"type": "date"
14+
}
15+
}
16+
}
17+
}
18+
}
19+
}
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"fields": {
3+
"entity": {
4+
"status": {
5+
"label": "Status"
6+
},
7+
"assessments": {
8+
"entityOndemand": {
9+
"label": "Ondemand assessment of entity",
10+
"recurrence": "ondemand",
11+
"questions": {
12+
"assessmentDate": {
13+
"type": "date"
14+
},
15+
"expirationDate": {
16+
"type": "date"
17+
}
18+
}
19+
}
20+
}
21+
}
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"fields": {
3+
"entity": {
4+
"status": {
5+
"label": "Status"
6+
},
7+
"assessments": {
8+
"entityOndemand": {
9+
"label": "Ondemand assessment of entity",
10+
"recurrence": "ondemand",
11+
"onDemandAssessmentExpirationDays": 180,
12+
"questions": {
13+
"assessmentDate": {
14+
"type": "date"
15+
},
16+
"expirationDate": {
17+
"type": "date"
18+
}
19+
}
20+
}
21+
}
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)