BlueGit

blue-star danique-bot

Files

danique-bot / node_modules / json-schema-traverse / spec / index.spec.js

index.spec.js

Import template
timfa Tim · pushed by timfa · 4 years ago
c788ad20 History
index.spec.js 3.1 KiB Download Raw
1'use strict';
2
3var traverse = require('../index');
4var assert = require('assert');
5
6describe('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, callback);
18 assert.deepStrictEqual(calls, expectedCalls);
19 });
20
21
22 describe('allKeys option', function() {
23 var schema = {
24 someObject: {
25 minimum: 1,
26 maximum: 2
27 }
28 };
29
30 it('should traverse objects with allKeys: true option', function() {
31 // schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex
32 var expectedCalls = [
33 [schema, '', schema, undefined, undefined, undefined, undefined],
34 [schema.someObject, '/someObject', schema, '', 'someObject', schema, undefined]
35 ];
36
37 traverse(schema, {allKeys: true}, callback);
38 assert.deepStrictEqual(calls, expectedCalls);
39 });
40
41
42 it('should NOT traverse objects with allKeys: false option', function() {
43 // schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex
44 var expectedCalls = [
45 [schema, '', schema, undefined, undefined, undefined, undefined]
46 ];
47
48 traverse(schema, {allKeys: false}, callback);
49 assert.deepStrictEqual(calls, expectedCalls);
50 });
51
52
53 it('should NOT traverse objects without allKeys option', function() {
54 // schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex
55 var expectedCalls = [
56 [schema, '', schema, undefined, undefined, undefined, undefined]
57 ];
58
59 traverse(schema, callback);
60 assert.deepStrictEqual(calls, expectedCalls);
61 });
62
63
64 it('should NOT travers objects in standard keywords which value is not a schema', function() {
65 var schema2 = {
66 const: {foo: 'bar'},
67 enum: ['a', 'b'],
68 required: ['foo'],
69 another: {
70
71 },
72 patternProperties: {}, // will not traverse - no properties
73 dependencies: true, // will not traverse - invalid
74 properties: {
75 smaller: {
76 type: 'number'
77 },
78 larger: {
79 type: 'number',
80 minimum: {$data: '1/smaller'}
81 }
82 }
83 };
84
85 // schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex
86 var expectedCalls = [
87 [schema2, '', schema2, undefined, undefined, undefined, undefined],
88 [schema2.another, '/another', schema2, '', 'another', schema2, undefined],
89 [schema2.properties.smaller, '/properties/smaller', schema2, '', 'properties', schema2, 'smaller'],
90 [schema2.properties.larger, '/properties/larger', schema2, '', 'properties', schema2, 'larger'],
91 ];
92
93 traverse(schema2, {allKeys: true}, callback);
94 assert.deepStrictEqual(calls, expectedCalls);
95 });
96 });
97
98
99 function callback() {
100 calls.push(Array.prototype.slice.call(arguments));
101 }
102});