1
|
'use strict';
|
2
|
|
3
|
var traverse = require('../index');
|
4
|
var assert = require('assert');
|
5
|
|
6
|
describe('json-schema-traverse', function() {
|
7
|
var calls;
|
8
|
|
9
|
beforeEach(function() {
|
10
|
calls = [];
|
11
|
});
|
12
|
|
13
|
it('should traverse all keywords containing schemas recursively', function() {
|
14
|
var schema = require('./fixtures/schema').schema;
|
15
|
var expectedCalls = require('./fixtures/schema').expectedCalls;
|
16
|
|
17
|
traverse(schema, {cb: callback});
|
18
|
assert.deepStrictEqual(calls, expectedCalls);
|
19
|
});
|
20
|
|
21
|
describe('Legacy v0.3.1 API', function() {
|
22
|
it('should traverse all keywords containing schemas recursively', function() {
|
23
|
var schema = require('./fixtures/schema').schema;
|
24
|
var expectedCalls = require('./fixtures/schema').expectedCalls;
|
25
|
|
26
|
traverse(schema, callback);
|
27
|
assert.deepStrictEqual(calls, expectedCalls);
|
28
|
});
|
29
|
|
30
|
it('should work when an options object is provided', function() {
|
31
|
// schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex
|
32
|
var schema = require('./fixtures/schema').schema;
|
33
|
var expectedCalls = require('./fixtures/schema').expectedCalls;
|
34
|
|
35
|
traverse(schema, {}, callback);
|
36
|
assert.deepStrictEqual(calls, expectedCalls);
|
37
|
});
|
38
|
});
|
39
|
|
40
|
|
41
|
describe('allKeys option', function() {
|
42
|
var schema = {
|
43
|
someObject: {
|
44
|
minimum: 1,
|
45
|
maximum: 2
|
46
|
}
|
47
|
};
|
48
|
|
49
|
it('should traverse objects with allKeys: true option', function() {
|
50
|
// schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex
|
51
|
var expectedCalls = [
|
52
|
[schema, '', schema, undefined, undefined, undefined, undefined],
|
53
|
[schema.someObject, '/someObject', schema, '', 'someObject', schema, undefined]
|
54
|
];
|
55
|
|
56
|
traverse(schema, {allKeys: true, cb: callback});
|
57
|
assert.deepStrictEqual(calls, expectedCalls);
|
58
|
});
|
59
|
|
60
|
|
61
|
it('should NOT traverse objects with allKeys: false option', function() {
|
62
|
// schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex
|
63
|
var expectedCalls = [
|
64
|
[schema, '', schema, undefined, undefined, undefined, undefined]
|
65
|
];
|
66
|
|
67
|
traverse(schema, {allKeys: false, cb: callback});
|
68
|
assert.deepStrictEqual(calls, expectedCalls);
|
69
|
});
|
70
|
|
71
|
|
72
|
it('should NOT traverse objects without allKeys option', function() {
|
73
|
// schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex
|
74
|
var expectedCalls = [
|
75
|
[schema, '', schema, undefined, undefined, undefined, undefined]
|
76
|
];
|
77
|
|
78
|
traverse(schema, {cb: callback});
|
79
|
assert.deepStrictEqual(calls, expectedCalls);
|
80
|
});
|
81
|
|
82
|
|
83
|
it('should NOT travers objects in standard keywords which value is not a schema', function() {
|
84
|
var schema2 = {
|
85
|
const: {foo: 'bar'},
|
86
|
enum: ['a', 'b'],
|
87
|
required: ['foo'],
|
88
|
another: {
|
89
|
|
90
|
},
|
91
|
patternProperties: {}, // will not traverse - no properties
|
92
|
dependencies: true, // will not traverse - invalid
|
93
|
properties: {
|
94
|
smaller: {
|
95
|
type: 'number'
|
96
|
},
|
97
|
larger: {
|
98
|
type: 'number',
|
99
|
minimum: {$data: '1/smaller'}
|
100
|
}
|
101
|
}
|
102
|
};
|
103
|
|
104
|
// schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex
|
105
|
var expectedCalls = [
|
106
|
[schema2, '', schema2, undefined, undefined, undefined, undefined],
|
107
|
[schema2.another, '/another', schema2, '', 'another', schema2, undefined],
|
108
|
[schema2.properties.smaller, '/properties/smaller', schema2, '', 'properties', schema2, 'smaller'],
|
109
|
[schema2.properties.larger, '/properties/larger', schema2, '', 'properties', schema2, 'larger'],
|
110
|
];
|
111
|
|
112
|
traverse(schema2, {allKeys: true, cb: callback});
|
113
|
assert.deepStrictEqual(calls, expectedCalls);
|
114
|
});
|
115
|
});
|
116
|
|
117
|
describe('pre and post', function() {
|
118
|
var schema = {
|
119
|
type: 'object',
|
120
|
properties: {
|
121
|
name: {type: 'string'},
|
122
|
age: {type: 'number'}
|
123
|
}
|
124
|
};
|
125
|
|
126
|
it('should traverse schema in pre-order', function() {
|
127
|
traverse(schema, {cb: {pre}});
|
128
|
var expectedCalls = [
|
129
|
['pre', schema, '', schema, undefined, undefined, undefined, undefined],
|
130
|
['pre', schema.properties.name, '/properties/name', schema, '', 'properties', schema, 'name'],
|
131
|
['pre', schema.properties.age, '/properties/age', schema, '', 'properties', schema, 'age'],
|
132
|
];
|
133
|
assert.deepStrictEqual(calls, expectedCalls);
|
134
|
});
|
135
|
|
136
|
it('should traverse schema in post-order', function() {
|
137
|
traverse(schema, {cb: {post}});
|
138
|
var expectedCalls = [
|
139
|
['post', schema.properties.name, '/properties/name', schema, '', 'properties', schema, 'name'],
|
140
|
['post', schema.properties.age, '/properties/age', schema, '', 'properties', schema, 'age'],
|
141
|
['post', schema, '', schema, undefined, undefined, undefined, undefined],
|
142
|
];
|
143
|
assert.deepStrictEqual(calls, expectedCalls);
|
144
|
});
|
145
|
|
146
|
it('should traverse schema in pre- and post-order at the same time', function() {
|
147
|
traverse(schema, {cb: {pre, post}});
|
148
|
var expectedCalls = [
|
149
|
['pre', schema, '', schema, undefined, undefined, undefined, undefined],
|
150
|
['pre', schema.properties.name, '/properties/name', schema, '', 'properties', schema, 'name'],
|
151
|
['post', schema.properties.name, '/properties/name', schema, '', 'properties', schema, 'name'],
|
152
|
['pre', schema.properties.age, '/properties/age', schema, '', 'properties', schema, 'age'],
|
153
|
['post', schema.properties.age, '/properties/age', schema, '', 'properties', schema, 'age'],
|
154
|
['post', schema, '', schema, undefined, undefined, undefined, undefined],
|
155
|
];
|
156
|
assert.deepStrictEqual(calls, expectedCalls);
|
157
|
});
|
158
|
});
|
159
|
|
160
|
function callback() {
|
161
|
calls.push(Array.prototype.slice.call(arguments));
|
162
|
}
|
163
|
|
164
|
function pre() {
|
165
|
calls.push(['pre'].concat(Array.prototype.slice.call(arguments)));
|
166
|
}
|
167
|
|
168
|
function post() {
|
169
|
calls.push(['post'].concat(Array.prototype.slice.call(arguments)));
|
170
|
}
|
171
|
});
|