schema.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 'use strict';
  2. var schema = {
  3. additionalItems: subschema('additionalItems'),
  4. items: subschema('items'),
  5. contains: subschema('contains'),
  6. additionalProperties: subschema('additionalProperties'),
  7. propertyNames: subschema('propertyNames'),
  8. not: subschema('not'),
  9. allOf: [
  10. subschema('allOf_0'),
  11. subschema('allOf_1'),
  12. {
  13. items: [
  14. subschema('items_0'),
  15. subschema('items_1'),
  16. ]
  17. }
  18. ],
  19. anyOf: [
  20. subschema('anyOf_0'),
  21. subschema('anyOf_1'),
  22. ],
  23. oneOf: [
  24. subschema('oneOf_0'),
  25. subschema('oneOf_1'),
  26. ],
  27. definitions: {
  28. foo: subschema('definitions_foo'),
  29. bar: subschema('definitions_bar'),
  30. },
  31. properties: {
  32. foo: subschema('properties_foo'),
  33. bar: subschema('properties_bar'),
  34. },
  35. patternProperties: {
  36. foo: subschema('patternProperties_foo'),
  37. bar: subschema('patternProperties_bar'),
  38. },
  39. dependencies: {
  40. foo: subschema('dependencies_foo'),
  41. bar: subschema('dependencies_bar'),
  42. },
  43. required: ['foo', 'bar']
  44. };
  45. function subschema(keyword) {
  46. var sch = {
  47. properties: {},
  48. additionalProperties: false,
  49. additionalItems: false,
  50. anyOf: [
  51. {format: 'email'},
  52. {format: 'hostname'}
  53. ]
  54. };
  55. sch.properties['foo_' + keyword] = {title: 'foo'};
  56. sch.properties['bar_' + keyword] = {title: 'bar'};
  57. return sch;
  58. }
  59. module.exports = {
  60. schema: schema,
  61. // schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex
  62. expectedCalls: [[schema, '', schema, undefined, undefined, undefined, undefined]]
  63. .concat(expectedCalls('additionalItems'))
  64. .concat(expectedCalls('items'))
  65. .concat(expectedCalls('contains'))
  66. .concat(expectedCalls('additionalProperties'))
  67. .concat(expectedCalls('propertyNames'))
  68. .concat(expectedCalls('not'))
  69. .concat(expectedCallsChild('allOf', 0))
  70. .concat(expectedCallsChild('allOf', 1))
  71. .concat([
  72. [schema.allOf[2], '/allOf/2', schema, '', 'allOf', schema, 2],
  73. [schema.allOf[2].items[0], '/allOf/2/items/0', schema, '/allOf/2', 'items', schema.allOf[2], 0],
  74. [schema.allOf[2].items[0].properties.foo_items_0, '/allOf/2/items/0/properties/foo_items_0', schema, '/allOf/2/items/0', 'properties', schema.allOf[2].items[0], 'foo_items_0'],
  75. [schema.allOf[2].items[0].properties.bar_items_0, '/allOf/2/items/0/properties/bar_items_0', schema, '/allOf/2/items/0', 'properties', schema.allOf[2].items[0], 'bar_items_0'],
  76. [schema.allOf[2].items[0].anyOf[0], '/allOf/2/items/0/anyOf/0', schema, '/allOf/2/items/0', 'anyOf', schema.allOf[2].items[0], 0],
  77. [schema.allOf[2].items[0].anyOf[1], '/allOf/2/items/0/anyOf/1', schema, '/allOf/2/items/0', 'anyOf', schema.allOf[2].items[0], 1],
  78. [schema.allOf[2].items[1], '/allOf/2/items/1', schema, '/allOf/2', 'items', schema.allOf[2], 1],
  79. [schema.allOf[2].items[1].properties.foo_items_1, '/allOf/2/items/1/properties/foo_items_1', schema, '/allOf/2/items/1', 'properties', schema.allOf[2].items[1], 'foo_items_1'],
  80. [schema.allOf[2].items[1].properties.bar_items_1, '/allOf/2/items/1/properties/bar_items_1', schema, '/allOf/2/items/1', 'properties', schema.allOf[2].items[1], 'bar_items_1'],
  81. [schema.allOf[2].items[1].anyOf[0], '/allOf/2/items/1/anyOf/0', schema, '/allOf/2/items/1', 'anyOf', schema.allOf[2].items[1], 0],
  82. [schema.allOf[2].items[1].anyOf[1], '/allOf/2/items/1/anyOf/1', schema, '/allOf/2/items/1', 'anyOf', schema.allOf[2].items[1], 1]
  83. ])
  84. .concat(expectedCallsChild('anyOf', 0))
  85. .concat(expectedCallsChild('anyOf', 1))
  86. .concat(expectedCallsChild('oneOf', 0))
  87. .concat(expectedCallsChild('oneOf', 1))
  88. .concat(expectedCallsChild('definitions', 'foo'))
  89. .concat(expectedCallsChild('definitions', 'bar'))
  90. .concat(expectedCallsChild('properties', 'foo'))
  91. .concat(expectedCallsChild('properties', 'bar'))
  92. .concat(expectedCallsChild('patternProperties', 'foo'))
  93. .concat(expectedCallsChild('patternProperties', 'bar'))
  94. .concat(expectedCallsChild('dependencies', 'foo'))
  95. .concat(expectedCallsChild('dependencies', 'bar'))
  96. };
  97. function expectedCalls(keyword) {
  98. return [
  99. [schema[keyword], `/${keyword}`, schema, '', keyword, schema, undefined],
  100. [schema[keyword].properties[`foo_${keyword}`], `/${keyword}/properties/foo_${keyword}`, schema, `/${keyword}`, 'properties', schema[keyword], `foo_${keyword}`],
  101. [schema[keyword].properties[`bar_${keyword}`], `/${keyword}/properties/bar_${keyword}`, schema, `/${keyword}`, 'properties', schema[keyword], `bar_${keyword}`],
  102. [schema[keyword].anyOf[0], `/${keyword}/anyOf/0`, schema, `/${keyword}`, 'anyOf', schema[keyword], 0],
  103. [schema[keyword].anyOf[1], `/${keyword}/anyOf/1`, schema, `/${keyword}`, 'anyOf', schema[keyword], 1]
  104. ];
  105. }
  106. function expectedCallsChild(keyword, i) {
  107. return [
  108. [schema[keyword][i], `/${keyword}/${i}`, schema, '', keyword, schema, i],
  109. [schema[keyword][i].properties[`foo_${keyword}_${i}`], `/${keyword}/${i}/properties/foo_${keyword}_${i}`, schema, `/${keyword}/${i}`, 'properties', schema[keyword][i], `foo_${keyword}_${i}`],
  110. [schema[keyword][i].properties[`bar_${keyword}_${i}`], `/${keyword}/${i}/properties/bar_${keyword}_${i}`, schema, `/${keyword}/${i}`, 'properties', schema[keyword][i], `bar_${keyword}_${i}`],
  111. [schema[keyword][i].anyOf[0], `/${keyword}/${i}/anyOf/0`, schema, `/${keyword}/${i}`, 'anyOf', schema[keyword][i], 0],
  112. [schema[keyword][i].anyOf[1], `/${keyword}/${i}/anyOf/1`, schema, `/${keyword}/${i}`, 'anyOf', schema[keyword][i], 1]
  113. ];
  114. }