items.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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) { /* istanbul ignore if */
  47. if (it.async) {
  48. out += ' throw new ValidationError([' + (__err) + ']); ';
  49. } else {
  50. out += ' validate.errors = [' + (__err) + ']; return false; ';
  51. }
  52. } else {
  53. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  54. }
  55. out += ' } ';
  56. $errSchemaPath = $currErrSchemaPath;
  57. if ($breakOnError) {
  58. $closingBraces += '}';
  59. out += ' else { ';
  60. }
  61. }
  62. var arr1 = $schema;
  63. if (arr1) {
  64. var $sch, $i = -1,
  65. l1 = arr1.length - 1;
  66. while ($i < l1) {
  67. $sch = arr1[$i += 1];
  68. if (it.util.schemaHasRules($sch, it.RULES.all)) {
  69. out += ' ' + ($nextValid) + ' = true; if (' + ($data) + '.length > ' + ($i) + ') { ';
  70. var $passData = $data + '[' + $i + ']';
  71. $it.schema = $sch;
  72. $it.schemaPath = $schemaPath + '[' + $i + ']';
  73. $it.errSchemaPath = $errSchemaPath + '/' + $i;
  74. $it.errorPath = it.util.getPathExpr(it.errorPath, $i, it.opts.jsonPointers, true);
  75. $it.dataPathArr[$dataNxt] = $i;
  76. var $code = it.validate($it);
  77. $it.baseId = $currentBaseId;
  78. if (it.util.varOccurences($code, $nextData) < 2) {
  79. out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
  80. } else {
  81. out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
  82. }
  83. out += ' } ';
  84. if ($breakOnError) {
  85. out += ' if (' + ($nextValid) + ') { ';
  86. $closingBraces += '}';
  87. }
  88. }
  89. }
  90. }
  91. if (typeof $additionalItems == 'object' && it.util.schemaHasRules($additionalItems, it.RULES.all)) {
  92. $it.schema = $additionalItems;
  93. $it.schemaPath = it.schemaPath + '.additionalItems';
  94. $it.errSchemaPath = it.errSchemaPath + '/additionalItems';
  95. out += ' ' + ($nextValid) + ' = true; if (' + ($data) + '.length > ' + ($schema.length) + ') { for (var ' + ($idx) + ' = ' + ($schema.length) + '; ' + ($idx) + ' < ' + ($data) + '.length; ' + ($idx) + '++) { ';
  96. $it.errorPath = it.util.getPathExpr(it.errorPath, $idx, it.opts.jsonPointers, true);
  97. var $passData = $data + '[' + $idx + ']';
  98. $it.dataPathArr[$dataNxt] = $idx;
  99. var $code = it.validate($it);
  100. $it.baseId = $currentBaseId;
  101. if (it.util.varOccurences($code, $nextData) < 2) {
  102. out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
  103. } else {
  104. out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
  105. }
  106. if ($breakOnError) {
  107. out += ' if (!' + ($nextValid) + ') break; ';
  108. }
  109. out += ' } } ';
  110. if ($breakOnError) {
  111. out += ' if (' + ($nextValid) + ') { ';
  112. $closingBraces += '}';
  113. }
  114. }
  115. } else if (it.util.schemaHasRules($schema, it.RULES.all)) {
  116. $it.schema = $schema;
  117. $it.schemaPath = $schemaPath;
  118. $it.errSchemaPath = $errSchemaPath;
  119. out += ' for (var ' + ($idx) + ' = ' + (0) + '; ' + ($idx) + ' < ' + ($data) + '.length; ' + ($idx) + '++) { ';
  120. $it.errorPath = it.util.getPathExpr(it.errorPath, $idx, it.opts.jsonPointers, true);
  121. var $passData = $data + '[' + $idx + ']';
  122. $it.dataPathArr[$dataNxt] = $idx;
  123. var $code = it.validate($it);
  124. $it.baseId = $currentBaseId;
  125. if (it.util.varOccurences($code, $nextData) < 2) {
  126. out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
  127. } else {
  128. out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
  129. }
  130. if ($breakOnError) {
  131. out += ' if (!' + ($nextValid) + ') break; ';
  132. }
  133. out += ' }';
  134. }
  135. if ($breakOnError) {
  136. out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';
  137. }
  138. out = it.util.cleanUpCode(out);
  139. return out;
  140. }