index.spec.js 5.9 KB

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