Skip to content

Commit c1b12bf

Browse files
author
Evgeny Poberezkin
authored
Merge pull request #160 from epoberezkin/ref-tests
Additional $ref tests for draft-04/06
2 parents 73a0593 + 25836f7 commit c1b12bf

File tree

6 files changed

+390
-8
lines changed

6 files changed

+390
-8
lines changed

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ var assert = require('assert');
77
var refs = {
88
'http://localhost:1234/integer.json': require('./remotes/integer.json'),
99
'http://localhost:1234/subSchemas.json': require('./remotes/subSchemas.json'),
10-
'http://localhost:1234/folder/folderInteger.json': require('./remotes/folder/folderInteger.json')
10+
'http://localhost:1234/folder/folderInteger.json': require('./remotes/folder/folderInteger.json'),
11+
'http://localhost:1234/name.json': require('./remotes/name.json')
1112
};
1213

1314
runTest(4);
1415
runTest(6);
1516

1617
function runTest(draft) {
17-
var opts = {addUsedSchema: false};
18+
var opts = {};
1819
if (draft == 4) opts.meta = false;
1920
var ajv = new Ajv(opts);
2021
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));

remotes/name.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"definitions": {
3+
"orNull": {
4+
"anyOf": [
5+
{"type": "null"},
6+
{"$ref": "#"}
7+
]
8+
}
9+
},
10+
"type": "string"
11+
}

tests/draft4/ref.json

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,5 +208,93 @@
208208
"valid": false
209209
}
210210
]
211+
},
212+
{
213+
"description": "Recursive references between schemas",
214+
"schema": {
215+
"id": "http://localhost:1234/tree",
216+
"description": "tree of nodes",
217+
"type": "object",
218+
"properties": {
219+
"meta": {"type": "string"},
220+
"nodes": {
221+
"type": "array",
222+
"items": {"$ref": "node"}
223+
}
224+
},
225+
"required": ["meta", "nodes"],
226+
"definitions": {
227+
"node": {
228+
"id": "http://localhost:1234/node",
229+
"description": "node",
230+
"type": "object",
231+
"properties": {
232+
"value": {"type": "number"},
233+
"subtree": {"$ref": "tree"}
234+
},
235+
"required": ["value"]
236+
}
237+
}
238+
},
239+
"tests": [
240+
{
241+
"description": "valid tree",
242+
"data": {
243+
"meta": "root",
244+
"nodes": [
245+
{
246+
"value": 1,
247+
"subtree": {
248+
"meta": "child",
249+
"nodes": [
250+
{"value": 1.1},
251+
{"value": 1.2}
252+
]
253+
}
254+
},
255+
{
256+
"value": 2,
257+
"subtree": {
258+
"meta": "child",
259+
"nodes": [
260+
{"value": 2.1},
261+
{"value": 2.2}
262+
]
263+
}
264+
}
265+
]
266+
},
267+
"valid": true
268+
},
269+
{
270+
"description": "invalid tree",
271+
"data": {
272+
"meta": "root",
273+
"nodes": [
274+
{
275+
"value": 1,
276+
"subtree": {
277+
"meta": "child",
278+
"nodes": [
279+
{"value": "string is invalid"},
280+
{"value": 1.2}
281+
]
282+
}
283+
},
284+
{
285+
"value": 2,
286+
"subtree": {
287+
"meta": "child",
288+
"nodes": [
289+
{"value": 2.1},
290+
{"value": 2.2}
291+
]
292+
}
293+
}
294+
]
295+
},
296+
"valid": false
297+
}
298+
]
211299
}
212300
]

tests/draft4/refRemote.json

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
]
5151
},
5252
{
53-
"description": "change resolution scope",
53+
"description": "base URI change",
5454
"schema": {
5555
"id": "http://localhost:1234/",
5656
"items": {
@@ -60,15 +60,112 @@
6060
},
6161
"tests": [
6262
{
63-
"description": "changed scope ref valid",
63+
"description": "base URI change ref valid",
6464
"data": [[1]],
6565
"valid": true
6666
},
6767
{
68-
"description": "changed scope ref invalid",
68+
"description": "base URI change ref invalid",
6969
"data": [["a"]],
7070
"valid": false
7171
}
7272
]
73+
},
74+
{
75+
"description": "base URI change - change folder",
76+
"schema": {
77+
"id": "http://localhost:1234/scope_change_defs1.json",
78+
"type" : "object",
79+
"properties": {
80+
"list": {"$ref": "#/definitions/baz"}
81+
},
82+
"definitions": {
83+
"baz": {
84+
"id": "folder/",
85+
"type": "array",
86+
"items": {"$ref": "folderInteger.json"}
87+
}
88+
}
89+
},
90+
"tests": [
91+
{
92+
"description": "number is valid",
93+
"data": {"list": [1]},
94+
"valid": true
95+
},
96+
{
97+
"description": "string is invalid",
98+
"data": {"list": ["a"]},
99+
"valid": false
100+
}
101+
]
102+
},
103+
{
104+
"description": "base URI change - change folder in subschema",
105+
"schema": {
106+
"id": "http://localhost:1234/scope_change_defs2.json",
107+
"type" : "object",
108+
"properties": {
109+
"list": {"$ref": "#/definitions/baz/definitions/bar"}
110+
},
111+
"definitions": {
112+
"baz": {
113+
"id": "folder/",
114+
"definitions": {
115+
"bar": {
116+
"type": "array",
117+
"items": {"$ref": "folderInteger.json"}
118+
}
119+
}
120+
}
121+
}
122+
},
123+
"tests": [
124+
{
125+
"description": "number is valid",
126+
"data": {"list": [1]},
127+
"valid": true
128+
},
129+
{
130+
"description": "string is invalid",
131+
"data": {"list": ["a"]},
132+
"valid": false
133+
}
134+
]
135+
},
136+
{
137+
"description": "root ref in remote ref",
138+
"schema": {
139+
"id": "http://localhost:1234/object",
140+
"type": "object",
141+
"properties": {
142+
"name": {"$ref": "name.json#/definitions/orNull"}
143+
}
144+
},
145+
"tests": [
146+
{
147+
"description": "string is valid",
148+
"data": {
149+
"name": "foo"
150+
},
151+
"valid": true
152+
},
153+
{
154+
"description": "null is valid",
155+
"data": {
156+
"name": null
157+
},
158+
"valid": true
159+
},
160+
{
161+
"description": "object is invalid",
162+
"data": {
163+
"name": {
164+
"name": null
165+
}
166+
},
167+
"valid": false
168+
}
169+
]
73170
}
74171
]

tests/draft6/ref.json

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,5 +240,93 @@
240240
"valid": false
241241
}
242242
]
243+
},
244+
{
245+
"description": "Recursive references between schemas",
246+
"schema": {
247+
"$id": "http://localhost:1234/tree",
248+
"description": "tree of nodes",
249+
"type": "object",
250+
"properties": {
251+
"meta": {"type": "string"},
252+
"nodes": {
253+
"type": "array",
254+
"items": {"$ref": "node"}
255+
}
256+
},
257+
"required": ["meta", "nodes"],
258+
"definitions": {
259+
"node": {
260+
"$id": "http://localhost:1234/node",
261+
"description": "node",
262+
"type": "object",
263+
"properties": {
264+
"value": {"type": "number"},
265+
"subtree": {"$ref": "tree"}
266+
},
267+
"required": ["value"]
268+
}
269+
}
270+
},
271+
"tests": [
272+
{
273+
"description": "valid tree",
274+
"data": {
275+
"meta": "root",
276+
"nodes": [
277+
{
278+
"value": 1,
279+
"subtree": {
280+
"meta": "child",
281+
"nodes": [
282+
{"value": 1.1},
283+
{"value": 1.2}
284+
]
285+
}
286+
},
287+
{
288+
"value": 2,
289+
"subtree": {
290+
"meta": "child",
291+
"nodes": [
292+
{"value": 2.1},
293+
{"value": 2.2}
294+
]
295+
}
296+
}
297+
]
298+
},
299+
"valid": true
300+
},
301+
{
302+
"description": "invalid tree",
303+
"data": {
304+
"meta": "root",
305+
"nodes": [
306+
{
307+
"value": 1,
308+
"subtree": {
309+
"meta": "child",
310+
"nodes": [
311+
{"value": "string is invalid"},
312+
{"value": 1.2}
313+
]
314+
}
315+
},
316+
{
317+
"value": 2,
318+
"subtree": {
319+
"meta": "child",
320+
"nodes": [
321+
{"value": 2.1},
322+
{"value": 2.2}
323+
]
324+
}
325+
}
326+
]
327+
},
328+
"valid": false
329+
}
330+
]
243331
}
244332
]

0 commit comments

Comments
 (0)