required.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. 'use strict';
  2. module.exports = function generate_required(it, $keyword) {
  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 $isData = it.opts.v5 && $schema && $schema.$data,
  13. $schemaValue;
  14. if ($isData) {
  15. out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
  16. $schemaValue = 'schema' + $lvl;
  17. } else {
  18. $schemaValue = $schema;
  19. }
  20. var $vSchema = 'schema' + $lvl;
  21. if (!$isData) {
  22. if ($schema.length < it.opts.loopRequired && it.schema.properties && Object.keys(it.schema.properties).length) {
  23. var $required = [];
  24. var arr1 = $schema;
  25. if (arr1) {
  26. var $property, i1 = -1,
  27. l1 = arr1.length - 1;
  28. while (i1 < l1) {
  29. $property = arr1[i1 += 1];
  30. var $propertySch = it.schema.properties[$property];
  31. if (!($propertySch && it.util.schemaHasRules($propertySch, it.RULES.all))) {
  32. $required[$required.length] = $property;
  33. }
  34. }
  35. }
  36. } else {
  37. var $required = $schema;
  38. }
  39. }
  40. if ($isData || $required.length) {
  41. var $currentErrorPath = it.errorPath,
  42. $loopRequired = $isData || $required.length >= it.opts.loopRequired;
  43. if ($breakOnError) {
  44. out += ' var missing' + ($lvl) + '; ';
  45. if ($loopRequired) {
  46. if (!$isData) {
  47. out += ' var ' + ($vSchema) + ' = validate.schema' + ($schemaPath) + '; ';
  48. }
  49. var $i = 'i' + $lvl,
  50. $propertyPath = 'schema' + $lvl + '[' + $i + ']',
  51. $missingProperty = '\' + ' + $propertyPath + ' + \'';
  52. if (it.opts._errorDataPathProperty) {
  53. it.errorPath = it.util.getPathExpr($currentErrorPath, $propertyPath, it.opts.jsonPointers);
  54. }
  55. out += ' var ' + ($valid) + ' = true; ';
  56. if ($isData) {
  57. out += ' if (schema' + ($lvl) + ' === undefined) ' + ($valid) + ' = true; else if (!Array.isArray(schema' + ($lvl) + ')) ' + ($valid) + ' = false; else {';
  58. }
  59. out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < ' + ($vSchema) + '.length; ' + ($i) + '++) { ' + ($valid) + ' = ' + ($data) + '[' + ($vSchema) + '[' + ($i) + ']] !== undefined; if (!' + ($valid) + ') break; } ';
  60. if ($isData) {
  61. out += ' } ';
  62. }
  63. out += ' if (!' + ($valid) + ') { ';
  64. var $$outStack = $$outStack || [];
  65. $$outStack.push(out);
  66. out = ''; /* istanbul ignore else */
  67. if (it.createErrors !== false) {
  68. out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
  69. if (it.opts.messages !== false) {
  70. out += ' , message: \'';
  71. if (it.opts._errorDataPathProperty) {
  72. out += 'is a required property';
  73. } else {
  74. out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
  75. }
  76. out += '\' ';
  77. }
  78. if (it.opts.verbose) {
  79. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  80. }
  81. out += ' } ';
  82. } else {
  83. out += ' {} ';
  84. }
  85. var __err = out;
  86. out = $$outStack.pop();
  87. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  88. if (it.async) {
  89. out += ' throw new ValidationError([' + (__err) + ']); ';
  90. } else {
  91. out += ' validate.errors = [' + (__err) + ']; return false; ';
  92. }
  93. } else {
  94. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  95. }
  96. out += ' } else { ';
  97. } else {
  98. out += ' if ( ';
  99. var arr2 = $required;
  100. if (arr2) {
  101. var _$property, $i = -1,
  102. l2 = arr2.length - 1;
  103. while ($i < l2) {
  104. _$property = arr2[$i += 1];
  105. if ($i) {
  106. out += ' || ';
  107. }
  108. var $prop = it.util.getProperty(_$property);
  109. out += ' ( ' + ($data) + ($prop) + ' === undefined && (missing' + ($lvl) + ' = ' + (it.util.toQuotedString(it.opts.jsonPointers ? _$property : $prop)) + ') ) ';
  110. }
  111. }
  112. out += ') { ';
  113. var $propertyPath = 'missing' + $lvl,
  114. $missingProperty = '\' + ' + $propertyPath + ' + \'';
  115. if (it.opts._errorDataPathProperty) {
  116. it.errorPath = it.opts.jsonPointers ? it.util.getPathExpr($currentErrorPath, $propertyPath, true) : $currentErrorPath + ' + ' + $propertyPath;
  117. }
  118. var $$outStack = $$outStack || [];
  119. $$outStack.push(out);
  120. out = ''; /* istanbul ignore else */
  121. if (it.createErrors !== false) {
  122. out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
  123. if (it.opts.messages !== false) {
  124. out += ' , message: \'';
  125. if (it.opts._errorDataPathProperty) {
  126. out += 'is a required property';
  127. } else {
  128. out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
  129. }
  130. out += '\' ';
  131. }
  132. if (it.opts.verbose) {
  133. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  134. }
  135. out += ' } ';
  136. } else {
  137. out += ' {} ';
  138. }
  139. var __err = out;
  140. out = $$outStack.pop();
  141. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  142. if (it.async) {
  143. out += ' throw new ValidationError([' + (__err) + ']); ';
  144. } else {
  145. out += ' validate.errors = [' + (__err) + ']; return false; ';
  146. }
  147. } else {
  148. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  149. }
  150. out += ' } else { ';
  151. }
  152. } else {
  153. if ($loopRequired) {
  154. if (!$isData) {
  155. out += ' var ' + ($vSchema) + ' = validate.schema' + ($schemaPath) + '; ';
  156. }
  157. var $i = 'i' + $lvl,
  158. $propertyPath = 'schema' + $lvl + '[' + $i + ']',
  159. $missingProperty = '\' + ' + $propertyPath + ' + \'';
  160. if (it.opts._errorDataPathProperty) {
  161. it.errorPath = it.util.getPathExpr($currentErrorPath, $propertyPath, it.opts.jsonPointers);
  162. }
  163. if ($isData) {
  164. out += ' if (' + ($vSchema) + ' && !Array.isArray(' + ($vSchema) + ')) { var err = '; /* istanbul ignore else */
  165. if (it.createErrors !== false) {
  166. out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
  167. if (it.opts.messages !== false) {
  168. out += ' , message: \'';
  169. if (it.opts._errorDataPathProperty) {
  170. out += 'is a required property';
  171. } else {
  172. out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
  173. }
  174. out += '\' ';
  175. }
  176. if (it.opts.verbose) {
  177. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  178. }
  179. out += ' } ';
  180. } else {
  181. out += ' {} ';
  182. }
  183. out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } else if (' + ($vSchema) + ' !== undefined) { ';
  184. }
  185. out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < ' + ($vSchema) + '.length; ' + ($i) + '++) { if (' + ($data) + '[' + ($vSchema) + '[' + ($i) + ']] === undefined) { var err = '; /* istanbul ignore else */
  186. if (it.createErrors !== false) {
  187. out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
  188. if (it.opts.messages !== false) {
  189. out += ' , message: \'';
  190. if (it.opts._errorDataPathProperty) {
  191. out += 'is a required property';
  192. } else {
  193. out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
  194. }
  195. out += '\' ';
  196. }
  197. if (it.opts.verbose) {
  198. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  199. }
  200. out += ' } ';
  201. } else {
  202. out += ' {} ';
  203. }
  204. out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } } ';
  205. if ($isData) {
  206. out += ' } ';
  207. }
  208. } else {
  209. var arr3 = $required;
  210. if (arr3) {
  211. var $reqProperty, i3 = -1,
  212. l3 = arr3.length - 1;
  213. while (i3 < l3) {
  214. $reqProperty = arr3[i3 += 1];
  215. var $prop = it.util.getProperty($reqProperty),
  216. $missingProperty = it.util.escapeQuotes($reqProperty);
  217. if (it.opts._errorDataPathProperty) {
  218. it.errorPath = it.util.getPath($currentErrorPath, $reqProperty, it.opts.jsonPointers);
  219. }
  220. out += ' if (' + ($data) + ($prop) + ' === undefined) { var err = '; /* istanbul ignore else */
  221. if (it.createErrors !== false) {
  222. out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
  223. if (it.opts.messages !== false) {
  224. out += ' , message: \'';
  225. if (it.opts._errorDataPathProperty) {
  226. out += 'is a required property';
  227. } else {
  228. out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
  229. }
  230. out += '\' ';
  231. }
  232. if (it.opts.verbose) {
  233. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  234. }
  235. out += ' } ';
  236. } else {
  237. out += ' {} ';
  238. }
  239. out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } ';
  240. }
  241. }
  242. }
  243. }
  244. it.errorPath = $currentErrorPath;
  245. } else if ($breakOnError) {
  246. out += ' if (true) {';
  247. }
  248. return out;
  249. }