index.spec.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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, callback);
  13. assert.deepStrictEqual(calls, expectedCalls);
  14. });
  15. describe('allKeys option', function() {
  16. var schema = {
  17. someObject: {
  18. minimum: 1,
  19. maximum: 2
  20. }
  21. };
  22. it('should traverse objects with allKeys: true option', function() {
  23. // schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex
  24. var expectedCalls = [
  25. [schema, '', schema, undefined, undefined, undefined, undefined],
  26. [schema.someObject, '/someObject', schema, '', 'someObject', schema, undefined]
  27. ];
  28. traverse(schema, {allKeys: true}, callback);
  29. assert.deepStrictEqual(calls, expectedCalls);
  30. });
  31. it('should NOT traverse objects with allKeys: false option', function() {
  32. // schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex
  33. var expectedCalls = [
  34. [schema, '', schema, undefined, undefined, undefined, undefined]
  35. ];
  36. traverse(schema, {allKeys: false}, callback);
  37. assert.deepStrictEqual(calls, expectedCalls);
  38. });
  39. it('should NOT traverse objects without allKeys option', function() {
  40. // schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex
  41. var expectedCalls = [
  42. [schema, '', schema, undefined, undefined, undefined, undefined]
  43. ];
  44. traverse(schema, callback);
  45. assert.deepStrictEqual(calls, expectedCalls);
  46. });
  47. it('should NOT travers objects in standard keywords which value is not a schema', function() {
  48. var schema2 = {
  49. const: {foo: 'bar'},
  50. enum: ['a', 'b'],
  51. required: ['foo'],
  52. another: {
  53. },
  54. patternProperties: {}, // will not traverse - no properties
  55. dependencies: true, // will not traverse - invalid
  56. properties: {
  57. smaller: {
  58. type: 'number'
  59. },
  60. larger: {
  61. type: 'number',
  62. minimum: {$data: '1/smaller'}
  63. }
  64. }
  65. };
  66. // schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex
  67. var expectedCalls = [
  68. [schema2, '', schema2, undefined, undefined, undefined, undefined],
  69. [schema2.another, '/another', schema2, '', 'another', schema2, undefined],
  70. [schema2.properties.smaller, '/properties/smaller', schema2, '', 'properties', schema2, 'smaller'],
  71. [schema2.properties.larger, '/properties/larger', schema2, '', 'properties', schema2, 'larger'],
  72. ];
  73. traverse(schema2, {allKeys: true}, callback);
  74. assert.deepStrictEqual(calls, expectedCalls);
  75. });
  76. });
  77. function callback() {
  78. calls.push(Array.prototype.slice.call(arguments));
  79. }
  80. });