Debian Codegraph @

json-schema-compare/test/specs/index.spec.js

raw
1 var chai = require('chai')
2 var compareModule = require('../../src')
3 var expect = chai.expect
4
5 var compare = function(a, b, expected, options) {
6 var result = compareModule(a, b, options)
7 expect(result).to.equal(expected)
8 }
9
10 describe('comparison', function() {
11 describe('validation only', function() {
12 it('checks the readme example', function() {
13 compare({
14 title: 'title 1',
15 type: ['object'],
16 uniqueItems: false,
17 dependencies: {
18 name: ['age', 'lastName']
19 },
20 required: ['name', 'age', 'name']
21 }, {
22 title: 'title 2',
23 type: 'object',
24 required: ['age', 'name'],
25 dependencies: {
26 name: ['lastName', 'age']
27 },
28 properties: {
29 name: {
30 minLength: 0
31 }
32 }
33 }, false, {
34 ignore: ['title']
35 })
36 })
37
38 it('compares false and undefined', function() {
39 compare(undefined, false, false)
40 })
41 it('compares required unsorted', function() {
42 compare({
43 required: ['test', 'rest']
44 }, {
45 required: ['rest', 'test', 'rest']
46 }, true)
47 })
48 it('compares equal required empty array and undefined', function() {
49 compare({
50 required: []
51 }, {}, true)
52
53 compare({
54 required: ['fds']
55 }, {}, false)
56 })
57 it('compares equal properties empty object and undefined', function() {
58 compare({
59 properties: {}
60 }, {}, true)
61 })
62 it('compares properties', function() {
63 compare({
64 properties: {
65 foo: {
66 type: 'string'
67 }
68 }
69 }, {
70 properties: {
71 foo: {
72 type: 'string'
73 }
74 }
75 }, true)
76 })
77 it('compares equal patternProperties empty object and undefined', function() {
78 compare({
79 patternProperties: {}
80 }, {}, true)
81 })
82 it('compares equal dependencies empty object and undefined', function() {
83 compare({
84 dependencies: {}
85 }, {}, true)
86 })
87 it('compares type unsorted', function() {
88 compare({
89 type: ['string', 'array']
90 }, {
91 type: ['array', 'string', 'array']
92 }, true)
93
94 compare({}, {
95 type: []
96 }, false)
97
98 compare({
99 type: 'string'
100 }, {
101 type: ['string']
102 }, true)
103 })
104 it('compares equal an empty schema, true and undefined', function() {
105 compare({}, true, true)
106 compare({}, undefined, true)
107 compare(false, false, true)
108 compare(true, true, true)
109 })
110 it('ignores any in ignore list', function() {
111 compare({
112 title: 'title'
113 }, {
114 title: 'foobar'
115 }, true, {ignore: ['title']})
116 })
117
118 it('diffs this', function() {
119 compare({
120 type: ['string'],
121 minLength: 5
122 }, {
123 type: ['string']
124 }, false)
125 })
126 it('sorts anyOf before comparing', function() {
127 compare({
128 anyOf: [
129 {
130 type: 'string'
131 }, {
132 type: 'integer'
133 }
134 ]
135 }, {
136 anyOf: [
137 {
138 type: 'integer'
139 }, {
140 type: 'string'
141 }
142 ]
143 }, true)
144
145 compare({
146 anyOf: [
147 {
148 type: 'string'
149 }, {
150 type: 'integer'
151 }
152 ]
153 }, {
154 anyOf: [
155 {
156 type: 'integer'
157 }, {
158 type: 'string'
159 },
160 {
161 type: ['string'],
162 minLength: 5,
163 fdsafads: '34534'
164 }
165 ]
166 }, false)
167
168 compare({
169 anyOf: [
170 {
171 type: 'string'
172 }, {
173 type: 'integer'
174 }
175 ]
176 }, {
177 anyOf: [
178 {
179 type: 'integer'
180 }, {
181 type: 'array'
182 }
183 ]
184 }, false)
185
186 compare({
187 anyOf: [
188 {
189 type: 'string'
190 }, {
191 type: ['string']
192 }, {
193 type: 'integer'
194 }
195 ]
196 }, {
197 anyOf: [
198 {
199 type: 'integer'
200 }, {
201 type: 'string'
202 }
203 ]
204 }, true)
205 })
206 it('sorts allOf before comparing', function() {
207 compare({
208 allOf: [
209 {
210 type: 'string'
211 }, {
212 type: 'integer'
213 }
214 ]
215 }, {
216 allOf: [
217 {
218 type: 'integer'
219 }, {
220 type: 'string'
221 }
222 ]
223 }, true)
224
225 compare({
226 allOf: [
227 {
228 type: 'string'
229 }, {
230 type: 'integer'
231 }
232 ]
233 }, {
234 allOf: [
235 {
236 type: 'integer'
237 }, {
238 type: 'string'
239 },
240 {
241 type: ['string'],
242 minLength: 5,
243 fdsafads: '34534'
244 }
245 ]
246 }, false)
247
248 compare({
249 allOf: [
250 {
251 type: 'string'
252 }, {
253 type: 'integer'
254 }
255 ]
256 }, {
257 allOf: [
258 {
259 type: 'integer'
260 }, {
261 type: 'array'
262 }
263 ]
264 }, false)
265
266 compare({
267 allOf: [
268 {
269 type: 'string'
270 }, {
271 type: ['string']
272 }, {
273 type: 'integer'
274 }
275 ]
276 }, {
277 allOf: [
278 {
279 type: 'integer'
280 }, {
281 type: 'string'
282 }
283 ]
284 }, true)
285 })
286 it('sorts oneOf before comparing', function() {
287 compare({
288 oneOf: [
289 {
290 type: 'string'
291 }, {
292 type: 'integer'
293 }
294 ]
295 }, {
296 oneOf: [
297 {
298 type: 'integer'
299 }, {
300 type: 'string'
301 }
302 ]
303 }, true)
304
305 compare({
306 oneOf: [
307 {
308 type: 'string'
309 }, {
310 type: 'integer'
311 }
312 ]
313 }, {
314 oneOf: [
315 {
316 type: 'integer'
317 }, {
318 type: 'string'
319 },
320 {
321 type: ['string'],
322 minLength: 5,
323 fdsafads: '34534'
324 }
325 ]
326 }, false)
327
328 compare({
329 oneOf: [
330 {
331 type: 'string'
332 }, {
333 type: 'integer'
334 }
335 ]
336 }, {
337 oneOf: [
338 {
339 type: 'integer'
340 }, {
341 type: 'array'
342 }
343 ]
344 }, false)
345
346 compare({
347 oneOf: [
348 {
349 type: 'string'
350 }, {
351 type: ['string']
352 }, {
353 type: 'integer'
354 }
355 ]
356 }, {
357 oneOf: [
358 {
359 type: 'integer'
360 }, {
361 type: 'string'
362 }
363 ]
364 }, true)
365 })
366 it('compares enum unsorted', function() {
367 compare({
368 enum: ['abc', '123']
369 }, {
370 enum: ['123', 'abc', 'abc']
371 }, true)
372 })
373 it('compares dependencies value if array unsorted', function() {
374 compare({
375 dependencies: {
376 foo: ['abc', '123']
377 }
378 }, {
379 dependencies: {
380 foo: ['123', 'abc', 'abc']
381 }
382 }, true)
383 })
384 it('compares items SORTED', function() {
385 compare({
386 items: [true, false]
387 }, {
388 items: [true, true]
389 }, false)
390
391 compare({
392 items: [{}, false]
393 }, {
394 items: [true, false]
395 }, true)
396 })
397 it('compares equal uniqueItems false and undefined', function() {
398 compare({
399 uniqueItems: false
400 }, {}, true)
401 })
402 it('compares equal minLength undefined and 0', function() {
403 compare({
404 minLength: 0
405 }, {}, true)
406 })
407 it('compares equal minItems undefined and 0', function() {
408 compare({
409 minItems: 0
410 }, {}, true)
411 })
412 it('compares equal minProperties undefined and 0', function() {
413 compare({
414 minProperties: 0
415 }, {}, true)
416 })
417 })
418
419 describe('complete', function() {
420 it('includes all properties, like title')
421 })
422 })