You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
6.0 KiB

4 years ago
  1. 'use strict';
  2. module.exports = function generate_items(it, $keyword, $ruleType) {
  3. var out = ' ';
  4. var $lvl = it.level;
  5. var $dataLvl = it.dataLevel;
  6. var $schema = it.schema[$keyword];
  7. var $schemaPath = it.schemaPath + it.util.getProperty($keyword);
  8. var $errSchemaPath = it.errSchemaPath + '/' + $keyword;
  9. var $breakOnError = !it.opts.allErrors;
  10. var $data = 'data' + ($dataLvl || '');
  11. var $valid = 'valid' + $lvl;
  12. var $errs = 'errs__' + $lvl;
  13. var $it = it.util.copy(it);
  14. var $closingBraces = '';
  15. $it.level++;
  16. var $nextValid = 'valid' + $it.level;
  17. var $idx = 'i' + $lvl,
  18. $dataNxt = $it.dataLevel = it.dataLevel + 1,
  19. $nextData = 'data' + $dataNxt,
  20. $currentBaseId = it.baseId;
  21. out += 'var ' + ($errs) + ' = errors;var ' + ($valid) + ';';
  22. if (Array.isArray($schema)) {
  23. var $additionalItems = it.schema.additionalItems;
  24. if ($additionalItems === false) {
  25. out += ' ' + ($valid) + ' = ' + ($data) + '.length <= ' + ($schema.length) + '; ';
  26. var $currErrSchemaPath = $errSchemaPath;
  27. $errSchemaPath = it.errSchemaPath + '/additionalItems';
  28. out += ' if (!' + ($valid) + ') { ';
  29. var $$outStack = $$outStack || [];
  30. $$outStack.push(out);
  31. out = ''; /* istanbul ignore else */
  32. if (it.createErrors !== false) {
  33. out += ' { keyword: \'' + ('additionalItems') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { limit: ' + ($schema.length) + ' } ';
  34. if (it.opts.messages !== false) {
  35. out += ' , message: \'should NOT have more than ' + ($schema.length) + ' items\' ';
  36. }
  37. if (it.opts.verbose) {
  38. out += ' , schema: false , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  39. }
  40. out += ' } ';
  41. } else {
  42. out += ' {} ';
  43. }
  44. var __err = out;
  45. out = $$outStack.pop();
  46. if (!it.compositeRule && $breakOnError) {
  47. /* istanbul ignore if */
  48. if (it.async) {
  49. out += ' throw new ValidationError([' + (__err) + ']); ';
  50. } else {
  51. out += ' validate.errors = [' + (__err) + ']; return false; ';
  52. }
  53. } else {
  54. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  55. }
  56. out += ' } ';
  57. $errSchemaPath = $currErrSchemaPath;
  58. if ($breakOnError) {
  59. $closingBraces += '}';
  60. out += ' else { ';
  61. }
  62. }
  63. var arr1 = $schema;
  64. if (arr1) {
  65. var $sch, $i = -1,
  66. l1 = arr1.length - 1;
  67. while ($i < l1) {
  68. $sch = arr1[$i += 1];
  69. if ((it.opts.strictKeywords ? typeof $sch == 'object' && Object.keys($sch).length > 0 : it.util.schemaHasRules($sch, it.RULES.all))) {
  70. out += ' ' + ($nextValid) + ' = true; if (' + ($data) + '.length > ' + ($i) + ') { ';
  71. var $passData = $data + '[' + $i + ']';
  72. $it.schema = $sch;
  73. $it.schemaPath = $schemaPath + '[' + $i + ']';
  74. $it.errSchemaPath = $errSchemaPath + '/' + $i;
  75. $it.errorPath = it.util.getPathExpr(it.errorPath, $i, it.opts.jsonPointers, true);
  76. $it.dataPathArr[$dataNxt] = $i;
  77. var $code = it.validate($it);
  78. $it.baseId = $currentBaseId;
  79. if (it.util.varOccurences($code, $nextData) < 2) {
  80. out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
  81. } else {
  82. out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
  83. }
  84. out += ' } ';
  85. if ($breakOnError) {
  86. out += ' if (' + ($nextValid) + ') { ';
  87. $closingBraces += '}';
  88. }
  89. }
  90. }
  91. }
  92. if (typeof $additionalItems == 'object' && (it.opts.strictKeywords ? typeof $additionalItems == 'object' && Object.keys($additionalItems).length > 0 : it.util.schemaHasRules($additionalItems, it.RULES.all))) {
  93. $it.schema = $additionalItems;
  94. $it.schemaPath = it.schemaPath + '.additionalItems';
  95. $it.errSchemaPath = it.errSchemaPath + '/additionalItems';
  96. out += ' ' + ($nextValid) + ' = true; if (' + ($data) + '.length > ' + ($schema.length) + ') { for (var ' + ($idx) + ' = ' + ($schema.length) + '; ' + ($idx) + ' < ' + ($data) + '.length; ' + ($idx) + '++) { ';
  97. $it.errorPath = it.util.getPathExpr(it.errorPath, $idx, it.opts.jsonPointers, true);
  98. var $passData = $data + '[' + $idx + ']';
  99. $it.dataPathArr[$dataNxt] = $idx;
  100. var $code = it.validate($it);
  101. $it.baseId = $currentBaseId;
  102. if (it.util.varOccurences($code, $nextData) < 2) {
  103. out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
  104. } else {
  105. out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
  106. }
  107. if ($breakOnError) {
  108. out += ' if (!' + ($nextValid) + ') break; ';
  109. }
  110. out += ' } } ';
  111. if ($breakOnError) {
  112. out += ' if (' + ($nextValid) + ') { ';
  113. $closingBraces += '}';
  114. }
  115. }
  116. } else if ((it.opts.strictKeywords ? typeof $schema == 'object' && Object.keys($schema).length > 0 : it.util.schemaHasRules($schema, it.RULES.all))) {
  117. $it.schema = $schema;
  118. $it.schemaPath = $schemaPath;
  119. $it.errSchemaPath = $errSchemaPath;
  120. out += ' for (var ' + ($idx) + ' = ' + (0) + '; ' + ($idx) + ' < ' + ($data) + '.length; ' + ($idx) + '++) { ';
  121. $it.errorPath = it.util.getPathExpr(it.errorPath, $idx, it.opts.jsonPointers, true);
  122. var $passData = $data + '[' + $idx + ']';
  123. $it.dataPathArr[$dataNxt] = $idx;
  124. var $code = it.validate($it);
  125. $it.baseId = $currentBaseId;
  126. if (it.util.varOccurences($code, $nextData) < 2) {
  127. out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
  128. } else {
  129. out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
  130. }
  131. if ($breakOnError) {
  132. out += ' if (!' + ($nextValid) + ') break; ';
  133. }
  134. out += ' }';
  135. }
  136. if ($breakOnError) {
  137. out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';
  138. }
  139. out = it.util.cleanUpCode(out);
  140. return out;
  141. }