properties.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. 'use strict';
  2. module.exports = function generate_properties(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 $errs = 'errs__' + $lvl;
  12. var $it = it.util.copy(it);
  13. var $closingBraces = '';
  14. $it.level++;
  15. var $nextValid = 'valid' + $it.level;
  16. var $key = 'key' + $lvl,
  17. $idx = 'idx' + $lvl,
  18. $dataNxt = $it.dataLevel = it.dataLevel + 1,
  19. $nextData = 'data' + $dataNxt,
  20. $dataProperties = 'dataProperties' + $lvl;
  21. var $schemaKeys = Object.keys($schema || {}),
  22. $pProperties = it.schema.patternProperties || {},
  23. $pPropertyKeys = Object.keys($pProperties),
  24. $aProperties = it.schema.additionalProperties,
  25. $someProperties = $schemaKeys.length || $pPropertyKeys.length,
  26. $noAdditional = $aProperties === false,
  27. $additionalIsSchema = typeof $aProperties == 'object' && Object.keys($aProperties).length,
  28. $removeAdditional = it.opts.removeAdditional,
  29. $checkAdditional = $noAdditional || $additionalIsSchema || $removeAdditional,
  30. $ownProperties = it.opts.ownProperties,
  31. $currentBaseId = it.baseId;
  32. var $required = it.schema.required;
  33. if ($required && !(it.opts.$data && $required.$data) && $required.length < it.opts.loopRequired) var $requiredHash = it.util.toHash($required);
  34. out += 'var ' + ($errs) + ' = errors;var ' + ($nextValid) + ' = true;';
  35. if ($ownProperties) {
  36. out += ' var ' + ($dataProperties) + ' = undefined;';
  37. }
  38. if ($checkAdditional) {
  39. if ($ownProperties) {
  40. out += ' ' + ($dataProperties) + ' = ' + ($dataProperties) + ' || Object.keys(' + ($data) + '); for (var ' + ($idx) + '=0; ' + ($idx) + '<' + ($dataProperties) + '.length; ' + ($idx) + '++) { var ' + ($key) + ' = ' + ($dataProperties) + '[' + ($idx) + ']; ';
  41. } else {
  42. out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
  43. }
  44. if ($someProperties) {
  45. out += ' var isAdditional' + ($lvl) + ' = !(false ';
  46. if ($schemaKeys.length) {
  47. if ($schemaKeys.length > 8) {
  48. out += ' || validate.schema' + ($schemaPath) + '.hasOwnProperty(' + ($key) + ') ';
  49. } else {
  50. var arr1 = $schemaKeys;
  51. if (arr1) {
  52. var $propertyKey, i1 = -1,
  53. l1 = arr1.length - 1;
  54. while (i1 < l1) {
  55. $propertyKey = arr1[i1 += 1];
  56. out += ' || ' + ($key) + ' == ' + (it.util.toQuotedString($propertyKey)) + ' ';
  57. }
  58. }
  59. }
  60. }
  61. if ($pPropertyKeys.length) {
  62. var arr2 = $pPropertyKeys;
  63. if (arr2) {
  64. var $pProperty, $i = -1,
  65. l2 = arr2.length - 1;
  66. while ($i < l2) {
  67. $pProperty = arr2[$i += 1];
  68. out += ' || ' + (it.usePattern($pProperty)) + '.test(' + ($key) + ') ';
  69. }
  70. }
  71. }
  72. out += ' ); if (isAdditional' + ($lvl) + ') { ';
  73. }
  74. if ($removeAdditional == 'all') {
  75. out += ' delete ' + ($data) + '[' + ($key) + ']; ';
  76. } else {
  77. var $currentErrorPath = it.errorPath;
  78. var $additionalProperty = '\' + ' + $key + ' + \'';
  79. if (it.opts._errorDataPathProperty) {
  80. it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
  81. }
  82. if ($noAdditional) {
  83. if ($removeAdditional) {
  84. out += ' delete ' + ($data) + '[' + ($key) + ']; ';
  85. } else {
  86. out += ' ' + ($nextValid) + ' = false; ';
  87. var $currErrSchemaPath = $errSchemaPath;
  88. $errSchemaPath = it.errSchemaPath + '/additionalProperties';
  89. var $$outStack = $$outStack || [];
  90. $$outStack.push(out);
  91. out = ''; /* istanbul ignore else */
  92. if (it.createErrors !== false) {
  93. out += ' { keyword: \'' + ('additionalProperties') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { additionalProperty: \'' + ($additionalProperty) + '\' } ';
  94. if (it.opts.messages !== false) {
  95. out += ' , message: \'';
  96. if (it.opts._errorDataPathProperty) {
  97. out += 'is an invalid additional property';
  98. } else {
  99. out += 'should NOT have additional properties';
  100. }
  101. out += '\' ';
  102. }
  103. if (it.opts.verbose) {
  104. out += ' , schema: false , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  105. }
  106. out += ' } ';
  107. } else {
  108. out += ' {} ';
  109. }
  110. var __err = out;
  111. out = $$outStack.pop();
  112. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  113. if (it.async) {
  114. out += ' throw new ValidationError([' + (__err) + ']); ';
  115. } else {
  116. out += ' validate.errors = [' + (__err) + ']; return false; ';
  117. }
  118. } else {
  119. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  120. }
  121. $errSchemaPath = $currErrSchemaPath;
  122. if ($breakOnError) {
  123. out += ' break; ';
  124. }
  125. }
  126. } else if ($additionalIsSchema) {
  127. if ($removeAdditional == 'failing') {
  128. out += ' var ' + ($errs) + ' = errors; ';
  129. var $wasComposite = it.compositeRule;
  130. it.compositeRule = $it.compositeRule = true;
  131. $it.schema = $aProperties;
  132. $it.schemaPath = it.schemaPath + '.additionalProperties';
  133. $it.errSchemaPath = it.errSchemaPath + '/additionalProperties';
  134. $it.errorPath = it.opts._errorDataPathProperty ? it.errorPath : it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
  135. var $passData = $data + '[' + $key + ']';
  136. $it.dataPathArr[$dataNxt] = $key;
  137. var $code = it.validate($it);
  138. $it.baseId = $currentBaseId;
  139. if (it.util.varOccurences($code, $nextData) < 2) {
  140. out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
  141. } else {
  142. out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
  143. }
  144. out += ' if (!' + ($nextValid) + ') { errors = ' + ($errs) + '; if (validate.errors !== null) { if (errors) validate.errors.length = errors; else validate.errors = null; } delete ' + ($data) + '[' + ($key) + ']; } ';
  145. it.compositeRule = $it.compositeRule = $wasComposite;
  146. } else {
  147. $it.schema = $aProperties;
  148. $it.schemaPath = it.schemaPath + '.additionalProperties';
  149. $it.errSchemaPath = it.errSchemaPath + '/additionalProperties';
  150. $it.errorPath = it.opts._errorDataPathProperty ? it.errorPath : it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
  151. var $passData = $data + '[' + $key + ']';
  152. $it.dataPathArr[$dataNxt] = $key;
  153. var $code = it.validate($it);
  154. $it.baseId = $currentBaseId;
  155. if (it.util.varOccurences($code, $nextData) < 2) {
  156. out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
  157. } else {
  158. out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
  159. }
  160. if ($breakOnError) {
  161. out += ' if (!' + ($nextValid) + ') break; ';
  162. }
  163. }
  164. }
  165. it.errorPath = $currentErrorPath;
  166. }
  167. if ($someProperties) {
  168. out += ' } ';
  169. }
  170. out += ' } ';
  171. if ($breakOnError) {
  172. out += ' if (' + ($nextValid) + ') { ';
  173. $closingBraces += '}';
  174. }
  175. }
  176. var $useDefaults = it.opts.useDefaults && !it.compositeRule;
  177. if ($schemaKeys.length) {
  178. var arr3 = $schemaKeys;
  179. if (arr3) {
  180. var $propertyKey, i3 = -1,
  181. l3 = arr3.length - 1;
  182. while (i3 < l3) {
  183. $propertyKey = arr3[i3 += 1];
  184. var $sch = $schema[$propertyKey];
  185. if (it.util.schemaHasRules($sch, it.RULES.all)) {
  186. var $prop = it.util.getProperty($propertyKey),
  187. $passData = $data + $prop,
  188. $hasDefault = $useDefaults && $sch.default !== undefined;
  189. $it.schema = $sch;
  190. $it.schemaPath = $schemaPath + $prop;
  191. $it.errSchemaPath = $errSchemaPath + '/' + it.util.escapeFragment($propertyKey);
  192. $it.errorPath = it.util.getPath(it.errorPath, $propertyKey, it.opts.jsonPointers);
  193. $it.dataPathArr[$dataNxt] = it.util.toQuotedString($propertyKey);
  194. var $code = it.validate($it);
  195. $it.baseId = $currentBaseId;
  196. if (it.util.varOccurences($code, $nextData) < 2) {
  197. $code = it.util.varReplace($code, $nextData, $passData);
  198. var $useData = $passData;
  199. } else {
  200. var $useData = $nextData;
  201. out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ';
  202. }
  203. if ($hasDefault) {
  204. out += ' ' + ($code) + ' ';
  205. } else {
  206. if ($requiredHash && $requiredHash[$propertyKey]) {
  207. out += ' if ( ' + ($useData) + ' === undefined ';
  208. if ($ownProperties) {
  209. out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
  210. }
  211. out += ') { ' + ($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 ';
  257. if ($ownProperties) {
  258. out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
  259. }
  260. out += ') { ' + ($nextValid) + ' = true; } else { ';
  261. } else {
  262. out += ' if (' + ($useData) + ' !== undefined ';
  263. if ($ownProperties) {
  264. out += ' && Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
  265. }
  266. out += ' ) { ';
  267. }
  268. }
  269. out += ' ' + ($code) + ' } ';
  270. }
  271. }
  272. if ($breakOnError) {
  273. out += ' if (' + ($nextValid) + ') { ';
  274. $closingBraces += '}';
  275. }
  276. }
  277. }
  278. }
  279. if ($pPropertyKeys.length) {
  280. var arr4 = $pPropertyKeys;
  281. if (arr4) {
  282. var $pProperty, i4 = -1,
  283. l4 = arr4.length - 1;
  284. while (i4 < l4) {
  285. $pProperty = arr4[i4 += 1];
  286. var $sch = $pProperties[$pProperty];
  287. if (it.util.schemaHasRules($sch, it.RULES.all)) {
  288. $it.schema = $sch;
  289. $it.schemaPath = it.schemaPath + '.patternProperties' + it.util.getProperty($pProperty);
  290. $it.errSchemaPath = it.errSchemaPath + '/patternProperties/' + it.util.escapeFragment($pProperty);
  291. if ($ownProperties) {
  292. out += ' ' + ($dataProperties) + ' = ' + ($dataProperties) + ' || Object.keys(' + ($data) + '); for (var ' + ($idx) + '=0; ' + ($idx) + '<' + ($dataProperties) + '.length; ' + ($idx) + '++) { var ' + ($key) + ' = ' + ($dataProperties) + '[' + ($idx) + ']; ';
  293. } else {
  294. out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
  295. }
  296. out += ' if (' + (it.usePattern($pProperty)) + '.test(' + ($key) + ')) { ';
  297. $it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
  298. var $passData = $data + '[' + $key + ']';
  299. $it.dataPathArr[$dataNxt] = $key;
  300. var $code = it.validate($it);
  301. $it.baseId = $currentBaseId;
  302. if (it.util.varOccurences($code, $nextData) < 2) {
  303. out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
  304. } else {
  305. out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
  306. }
  307. if ($breakOnError) {
  308. out += ' if (!' + ($nextValid) + ') break; ';
  309. }
  310. out += ' } ';
  311. if ($breakOnError) {
  312. out += ' else ' + ($nextValid) + ' = true; ';
  313. }
  314. out += ' } ';
  315. if ($breakOnError) {
  316. out += ' if (' + ($nextValid) + ') { ';
  317. $closingBraces += '}';
  318. }
  319. }
  320. }
  321. }
  322. }
  323. if ($breakOnError) {
  324. out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';
  325. }
  326. out = it.util.cleanUpCode(out);
  327. return out;
  328. }