properties.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. 'use strict';
  2. module.exports = function generate_properties(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 $errs = 'errs__' + $lvl;
  13. var $it = it.util.copy(it);
  14. var $closingBraces = '';
  15. $it.level++;
  16. var $nextValid = 'valid' + $it.level;
  17. var $key = 'key' + $lvl,
  18. $dataNxt = $it.dataLevel = it.dataLevel + 1,
  19. $nextData = 'data' + $dataNxt;
  20. var $schemaKeys = Object.keys($schema || {}),
  21. $pProperties = it.schema.patternProperties || {},
  22. $pPropertyKeys = Object.keys($pProperties),
  23. $aProperties = it.schema.additionalProperties,
  24. $someProperties = $schemaKeys.length || $pPropertyKeys.length,
  25. $noAdditional = $aProperties === false,
  26. $additionalIsSchema = typeof $aProperties == 'object' && Object.keys($aProperties).length,
  27. $removeAdditional = it.opts.removeAdditional,
  28. $checkAdditional = $noAdditional || $additionalIsSchema || $removeAdditional,
  29. $ownProperties = it.opts.ownProperties,
  30. $currentBaseId = it.baseId;
  31. var $required = it.schema.required;
  32. if ($required && !(it.opts.v5 && $required.$data) && $required.length < it.opts.loopRequired) var $requiredHash = it.util.toHash($required);
  33. if (it.opts.v5) {
  34. var $pgProperties = it.schema.patternGroups || {},
  35. $pgPropertyKeys = Object.keys($pgProperties);
  36. }
  37. out += 'var ' + ($errs) + ' = errors;var ' + ($nextValid) + ' = true;';
  38. if ($checkAdditional) {
  39. out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
  40. if ($ownProperties) {
  41. out += ' if (!Object.prototype.hasOwnProperty.call(' + ($data) + ', ' + ($key) + ')) continue; ';
  42. }
  43. if ($someProperties) {
  44. out += ' var isAdditional' + ($lvl) + ' = !(false ';
  45. if ($schemaKeys.length) {
  46. if ($schemaKeys.length > 5) {
  47. out += ' || validate.schema' + ($schemaPath) + '[' + ($key) + '] ';
  48. } else {
  49. var arr1 = $schemaKeys;
  50. if (arr1) {
  51. var $propertyKey, i1 = -1,
  52. l1 = arr1.length - 1;
  53. while (i1 < l1) {
  54. $propertyKey = arr1[i1 += 1];
  55. out += ' || ' + ($key) + ' == ' + (it.util.toQuotedString($propertyKey)) + ' ';
  56. }
  57. }
  58. }
  59. }
  60. if ($pPropertyKeys.length) {
  61. var arr2 = $pPropertyKeys;
  62. if (arr2) {
  63. var $pProperty, $i = -1,
  64. l2 = arr2.length - 1;
  65. while ($i < l2) {
  66. $pProperty = arr2[$i += 1];
  67. out += ' || ' + (it.usePattern($pProperty)) + '.test(' + ($key) + ') ';
  68. }
  69. }
  70. }
  71. if (it.opts.v5 && $pgPropertyKeys && $pgPropertyKeys.length) {
  72. var arr3 = $pgPropertyKeys;
  73. if (arr3) {
  74. var $pgProperty, $i = -1,
  75. l3 = arr3.length - 1;
  76. while ($i < l3) {
  77. $pgProperty = arr3[$i += 1];
  78. out += ' || ' + (it.usePattern($pgProperty)) + '.test(' + ($key) + ') ';
  79. }
  80. }
  81. }
  82. out += ' ); if (isAdditional' + ($lvl) + ') { ';
  83. }
  84. if ($removeAdditional == 'all') {
  85. out += ' delete ' + ($data) + '[' + ($key) + ']; ';
  86. } else {
  87. var $currentErrorPath = it.errorPath;
  88. var $additionalProperty = '\' + ' + $key + ' + \'';
  89. if (it.opts._errorDataPathProperty) {
  90. it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
  91. }
  92. if ($noAdditional) {
  93. if ($removeAdditional) {
  94. out += ' delete ' + ($data) + '[' + ($key) + ']; ';
  95. } else {
  96. out += ' ' + ($nextValid) + ' = false; ';
  97. var $currErrSchemaPath = $errSchemaPath;
  98. $errSchemaPath = it.errSchemaPath + '/additionalProperties';
  99. var $$outStack = $$outStack || [];
  100. $$outStack.push(out);
  101. out = ''; /* istanbul ignore else */
  102. if (it.createErrors !== false) {
  103. out += ' { keyword: \'' + ('additionalProperties') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { additionalProperty: \'' + ($additionalProperty) + '\' } ';
  104. if (it.opts.messages !== false) {
  105. out += ' , message: \'should NOT have additional properties\' ';
  106. }
  107. if (it.opts.verbose) {
  108. out += ' , schema: false , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  109. }
  110. out += ' } ';
  111. } else {
  112. out += ' {} ';
  113. }
  114. var __err = out;
  115. out = $$outStack.pop();
  116. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  117. if (it.async) {
  118. out += ' throw new ValidationError([' + (__err) + ']); ';
  119. } else {
  120. out += ' validate.errors = [' + (__err) + ']; return false; ';
  121. }
  122. } else {
  123. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  124. }
  125. $errSchemaPath = $currErrSchemaPath;
  126. if ($breakOnError) {
  127. out += ' break; ';
  128. }
  129. }
  130. } else if ($additionalIsSchema) {
  131. if ($removeAdditional == 'failing') {
  132. out += ' var ' + ($errs) + ' = errors; ';
  133. var $wasComposite = it.compositeRule;
  134. it.compositeRule = $it.compositeRule = true;
  135. $it.schema = $aProperties;
  136. $it.schemaPath = it.schemaPath + '.additionalProperties';
  137. $it.errSchemaPath = it.errSchemaPath + '/additionalProperties';
  138. $it.errorPath = it.opts._errorDataPathProperty ? it.errorPath : it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
  139. var $passData = $data + '[' + $key + ']';
  140. $it.dataPathArr[$dataNxt] = $key;
  141. var $code = it.validate($it);
  142. $it.baseId = $currentBaseId;
  143. if (it.util.varOccurences($code, $nextData) < 2) {
  144. out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
  145. } else {
  146. out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
  147. }
  148. out += ' if (!' + ($nextValid) + ') { errors = ' + ($errs) + '; if (validate.errors !== null) { if (errors) validate.errors.length = errors; else validate.errors = null; } delete ' + ($data) + '[' + ($key) + ']; } ';
  149. it.compositeRule = $it.compositeRule = $wasComposite;
  150. } else {
  151. $it.schema = $aProperties;
  152. $it.schemaPath = it.schemaPath + '.additionalProperties';
  153. $it.errSchemaPath = it.errSchemaPath + '/additionalProperties';
  154. $it.errorPath = it.opts._errorDataPathProperty ? it.errorPath : it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
  155. var $passData = $data + '[' + $key + ']';
  156. $it.dataPathArr[$dataNxt] = $key;
  157. var $code = it.validate($it);
  158. $it.baseId = $currentBaseId;
  159. if (it.util.varOccurences($code, $nextData) < 2) {
  160. out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
  161. } else {
  162. out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
  163. }
  164. if ($breakOnError) {
  165. out += ' if (!' + ($nextValid) + ') break; ';
  166. }
  167. }
  168. }
  169. it.errorPath = $currentErrorPath;
  170. }
  171. if ($someProperties) {
  172. out += ' } ';
  173. }
  174. out += ' } ';
  175. if ($breakOnError) {
  176. out += ' if (' + ($nextValid) + ') { ';
  177. $closingBraces += '}';
  178. }
  179. }
  180. var $useDefaults = it.opts.useDefaults && !it.compositeRule;
  181. if ($schemaKeys.length) {
  182. var arr4 = $schemaKeys;
  183. if (arr4) {
  184. var $propertyKey, i4 = -1,
  185. l4 = arr4.length - 1;
  186. while (i4 < l4) {
  187. $propertyKey = arr4[i4 += 1];
  188. var $sch = $schema[$propertyKey];
  189. if (it.util.schemaHasRules($sch, it.RULES.all)) {
  190. var $prop = it.util.getProperty($propertyKey),
  191. $passData = $data + $prop,
  192. $hasDefault = $useDefaults && $sch.default !== undefined;
  193. $it.schema = $sch;
  194. $it.schemaPath = $schemaPath + $prop;
  195. $it.errSchemaPath = $errSchemaPath + '/' + it.util.escapeFragment($propertyKey);
  196. $it.errorPath = it.util.getPath(it.errorPath, $propertyKey, it.opts.jsonPointers);
  197. $it.dataPathArr[$dataNxt] = it.util.toQuotedString($propertyKey);
  198. var $code = it.validate($it);
  199. $it.baseId = $currentBaseId;
  200. if (it.util.varOccurences($code, $nextData) < 2) {
  201. $code = it.util.varReplace($code, $nextData, $passData);
  202. var $useData = $passData;
  203. } else {
  204. var $useData = $nextData;
  205. out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ';
  206. }
  207. if ($hasDefault) {
  208. out += ' ' + ($code) + ' ';
  209. } else {
  210. if ($requiredHash && $requiredHash[$propertyKey]) {
  211. out += ' if (' + ($useData) + ' === undefined) { ' + ($nextValid) + ' = false; ';
  212. var $currentErrorPath = it.errorPath,
  213. $currErrSchemaPath = $errSchemaPath,
  214. $missingProperty = it.util.escapeQuotes($propertyKey);
  215. if (it.opts._errorDataPathProperty) {
  216. it.errorPath = it.util.getPath($currentErrorPath, $propertyKey, it.opts.jsonPointers);
  217. }
  218. $errSchemaPath = it.errSchemaPath + '/required';
  219. var $$outStack = $$outStack || [];
  220. $$outStack.push(out);
  221. out = ''; /* istanbul ignore else */
  222. if (it.createErrors !== false) {
  223. out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
  224. if (it.opts.messages !== false) {
  225. out += ' , message: \'';
  226. if (it.opts._errorDataPathProperty) {
  227. out += 'is a required property';
  228. } else {
  229. out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
  230. }
  231. out += '\' ';
  232. }
  233. if (it.opts.verbose) {
  234. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  235. }
  236. out += ' } ';
  237. } else {
  238. out += ' {} ';
  239. }
  240. var __err = out;
  241. out = $$outStack.pop();
  242. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  243. if (it.async) {
  244. out += ' throw new ValidationError([' + (__err) + ']); ';
  245. } else {
  246. out += ' validate.errors = [' + (__err) + ']; return false; ';
  247. }
  248. } else {
  249. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  250. }
  251. $errSchemaPath = $currErrSchemaPath;
  252. it.errorPath = $currentErrorPath;
  253. out += ' } else { ';
  254. } else {
  255. if ($breakOnError) {
  256. out += ' if (' + ($useData) + ' === undefined) { ' + ($nextValid) + ' = true; } else { ';
  257. } else {
  258. out += ' if (' + ($useData) + ' !== undefined) { ';
  259. }
  260. }
  261. out += ' ' + ($code) + ' } ';
  262. }
  263. }
  264. if ($breakOnError) {
  265. out += ' if (' + ($nextValid) + ') { ';
  266. $closingBraces += '}';
  267. }
  268. }
  269. }
  270. }
  271. var arr5 = $pPropertyKeys;
  272. if (arr5) {
  273. var $pProperty, i5 = -1,
  274. l5 = arr5.length - 1;
  275. while (i5 < l5) {
  276. $pProperty = arr5[i5 += 1];
  277. var $sch = $pProperties[$pProperty];
  278. if (it.util.schemaHasRules($sch, it.RULES.all)) {
  279. $it.schema = $sch;
  280. $it.schemaPath = it.schemaPath + '.patternProperties' + it.util.getProperty($pProperty);
  281. $it.errSchemaPath = it.errSchemaPath + '/patternProperties/' + it.util.escapeFragment($pProperty);
  282. out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
  283. if ($ownProperties) {
  284. out += ' if (!Object.prototype.hasOwnProperty.call(' + ($data) + ', ' + ($key) + ')) continue; ';
  285. }
  286. out += ' if (' + (it.usePattern($pProperty)) + '.test(' + ($key) + ')) { ';
  287. $it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
  288. var $passData = $data + '[' + $key + ']';
  289. $it.dataPathArr[$dataNxt] = $key;
  290. var $code = it.validate($it);
  291. $it.baseId = $currentBaseId;
  292. if (it.util.varOccurences($code, $nextData) < 2) {
  293. out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
  294. } else {
  295. out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
  296. }
  297. if ($breakOnError) {
  298. out += ' if (!' + ($nextValid) + ') break; ';
  299. }
  300. out += ' } ';
  301. if ($breakOnError) {
  302. out += ' else ' + ($nextValid) + ' = true; ';
  303. }
  304. out += ' } ';
  305. if ($breakOnError) {
  306. out += ' if (' + ($nextValid) + ') { ';
  307. $closingBraces += '}';
  308. }
  309. }
  310. }
  311. }
  312. if (it.opts.v5) {
  313. var arr6 = $pgPropertyKeys;
  314. if (arr6) {
  315. var $pgProperty, i6 = -1,
  316. l6 = arr6.length - 1;
  317. while (i6 < l6) {
  318. $pgProperty = arr6[i6 += 1];
  319. var $pgSchema = $pgProperties[$pgProperty],
  320. $sch = $pgSchema.schema;
  321. if (it.util.schemaHasRules($sch, it.RULES.all)) {
  322. $it.schema = $sch;
  323. $it.schemaPath = it.schemaPath + '.patternGroups' + it.util.getProperty($pgProperty) + '.schema';
  324. $it.errSchemaPath = it.errSchemaPath + '/patternGroups/' + it.util.escapeFragment($pgProperty) + '/schema';
  325. out += ' var pgPropCount' + ($lvl) + ' = 0; for (var ' + ($key) + ' in ' + ($data) + ') { ';
  326. if ($ownProperties) {
  327. out += ' if (!Object.prototype.hasOwnProperty.call(' + ($data) + ', ' + ($key) + ')) continue; ';
  328. }
  329. out += ' if (' + (it.usePattern($pgProperty)) + '.test(' + ($key) + ')) { pgPropCount' + ($lvl) + '++; ';
  330. $it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
  331. var $passData = $data + '[' + $key + ']';
  332. $it.dataPathArr[$dataNxt] = $key;
  333. var $code = it.validate($it);
  334. $it.baseId = $currentBaseId;
  335. if (it.util.varOccurences($code, $nextData) < 2) {
  336. out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
  337. } else {
  338. out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
  339. }
  340. if ($breakOnError) {
  341. out += ' if (!' + ($nextValid) + ') break; ';
  342. }
  343. out += ' } ';
  344. if ($breakOnError) {
  345. out += ' else ' + ($nextValid) + ' = true; ';
  346. }
  347. out += ' } ';
  348. if ($breakOnError) {
  349. out += ' if (' + ($nextValid) + ') { ';
  350. $closingBraces += '}';
  351. }
  352. var $pgMin = $pgSchema.minimum,
  353. $pgMax = $pgSchema.maximum;
  354. if ($pgMin !== undefined || $pgMax !== undefined) {
  355. out += ' var ' + ($valid) + ' = true; ';
  356. var $currErrSchemaPath = $errSchemaPath;
  357. if ($pgMin !== undefined) {
  358. var $limit = $pgMin,
  359. $reason = 'minimum',
  360. $moreOrLess = 'less';
  361. out += ' ' + ($valid) + ' = pgPropCount' + ($lvl) + ' >= ' + ($pgMin) + '; ';
  362. $errSchemaPath = it.errSchemaPath + '/patternGroups/minimum';
  363. out += ' if (!' + ($valid) + ') { ';
  364. var $$outStack = $$outStack || [];
  365. $$outStack.push(out);
  366. out = ''; /* istanbul ignore else */
  367. if (it.createErrors !== false) {
  368. out += ' { keyword: \'' + ('patternGroups') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { reason: \'' + ($reason) + '\', limit: ' + ($limit) + ', pattern: \'' + (it.util.escapeQuotes($pgProperty)) + '\' } ';
  369. if (it.opts.messages !== false) {
  370. out += ' , message: \'should NOT have ' + ($moreOrLess) + ' than ' + ($limit) + ' properties matching pattern "' + (it.util.escapeQuotes($pgProperty)) + '"\' ';
  371. }
  372. if (it.opts.verbose) {
  373. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  374. }
  375. out += ' } ';
  376. } else {
  377. out += ' {} ';
  378. }
  379. var __err = out;
  380. out = $$outStack.pop();
  381. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  382. if (it.async) {
  383. out += ' throw new ValidationError([' + (__err) + ']); ';
  384. } else {
  385. out += ' validate.errors = [' + (__err) + ']; return false; ';
  386. }
  387. } else {
  388. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  389. }
  390. out += ' } ';
  391. if ($pgMax !== undefined) {
  392. out += ' else ';
  393. }
  394. }
  395. if ($pgMax !== undefined) {
  396. var $limit = $pgMax,
  397. $reason = 'maximum',
  398. $moreOrLess = 'more';
  399. out += ' ' + ($valid) + ' = pgPropCount' + ($lvl) + ' <= ' + ($pgMax) + '; ';
  400. $errSchemaPath = it.errSchemaPath + '/patternGroups/maximum';
  401. out += ' if (!' + ($valid) + ') { ';
  402. var $$outStack = $$outStack || [];
  403. $$outStack.push(out);
  404. out = ''; /* istanbul ignore else */
  405. if (it.createErrors !== false) {
  406. out += ' { keyword: \'' + ('patternGroups') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { reason: \'' + ($reason) + '\', limit: ' + ($limit) + ', pattern: \'' + (it.util.escapeQuotes($pgProperty)) + '\' } ';
  407. if (it.opts.messages !== false) {
  408. out += ' , message: \'should NOT have ' + ($moreOrLess) + ' than ' + ($limit) + ' properties matching pattern "' + (it.util.escapeQuotes($pgProperty)) + '"\' ';
  409. }
  410. if (it.opts.verbose) {
  411. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  412. }
  413. out += ' } ';
  414. } else {
  415. out += ' {} ';
  416. }
  417. var __err = out;
  418. out = $$outStack.pop();
  419. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  420. if (it.async) {
  421. out += ' throw new ValidationError([' + (__err) + ']); ';
  422. } else {
  423. out += ' validate.errors = [' + (__err) + ']; return false; ';
  424. }
  425. } else {
  426. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  427. }
  428. out += ' } ';
  429. }
  430. $errSchemaPath = $currErrSchemaPath;
  431. if ($breakOnError) {
  432. out += ' if (' + ($valid) + ') { ';
  433. $closingBraces += '}';
  434. }
  435. }
  436. }
  437. }
  438. }
  439. }
  440. if ($breakOnError) {
  441. out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';
  442. }
  443. out = it.util.cleanUpCode(out);
  444. return out;
  445. }