Ohm-Management - Projektarbeit B-ME
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.

validate.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. 'use strict';
  2. module.exports = function generate_validate(it, $keyword, $ruleType) {
  3. var out = '';
  4. var $async = it.schema.$async === true,
  5. $refKeywords = it.util.schemaHasRulesExcept(it.schema, it.RULES.all, '$ref'),
  6. $id = it.self._getId(it.schema);
  7. if (it.isTop) {
  8. out += ' var validate = ';
  9. if ($async) {
  10. it.async = true;
  11. out += 'async ';
  12. }
  13. out += 'function(data, dataPath, parentData, parentDataProperty, rootData) { \'use strict\'; ';
  14. if ($id && (it.opts.sourceCode || it.opts.processCode)) {
  15. out += ' ' + ('/\*# sourceURL=' + $id + ' */') + ' ';
  16. }
  17. }
  18. if (typeof it.schema == 'boolean' || !($refKeywords || it.schema.$ref)) {
  19. var $keyword = 'false schema';
  20. var $lvl = it.level;
  21. var $dataLvl = it.dataLevel;
  22. var $schema = it.schema[$keyword];
  23. var $schemaPath = it.schemaPath + it.util.getProperty($keyword);
  24. var $errSchemaPath = it.errSchemaPath + '/' + $keyword;
  25. var $breakOnError = !it.opts.allErrors;
  26. var $errorKeyword;
  27. var $data = 'data' + ($dataLvl || '');
  28. var $valid = 'valid' + $lvl;
  29. if (it.schema === false) {
  30. if (it.isTop) {
  31. $breakOnError = true;
  32. } else {
  33. out += ' var ' + ($valid) + ' = false; ';
  34. }
  35. var $$outStack = $$outStack || [];
  36. $$outStack.push(out);
  37. out = ''; /* istanbul ignore else */
  38. if (it.createErrors !== false) {
  39. out += ' { keyword: \'' + ($errorKeyword || 'false schema') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: {} ';
  40. if (it.opts.messages !== false) {
  41. out += ' , message: \'boolean schema is false\' ';
  42. }
  43. if (it.opts.verbose) {
  44. out += ' , schema: false , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  45. }
  46. out += ' } ';
  47. } else {
  48. out += ' {} ';
  49. }
  50. var __err = out;
  51. out = $$outStack.pop();
  52. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  53. if (it.async) {
  54. out += ' throw new ValidationError([' + (__err) + ']); ';
  55. } else {
  56. out += ' validate.errors = [' + (__err) + ']; return false; ';
  57. }
  58. } else {
  59. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  60. }
  61. } else {
  62. if (it.isTop) {
  63. if ($async) {
  64. out += ' return data; ';
  65. } else {
  66. out += ' validate.errors = null; return true; ';
  67. }
  68. } else {
  69. out += ' var ' + ($valid) + ' = true; ';
  70. }
  71. }
  72. if (it.isTop) {
  73. out += ' }; return validate; ';
  74. }
  75. return out;
  76. }
  77. if (it.isTop) {
  78. var $top = it.isTop,
  79. $lvl = it.level = 0,
  80. $dataLvl = it.dataLevel = 0,
  81. $data = 'data';
  82. it.rootId = it.resolve.fullPath(it.self._getId(it.root.schema));
  83. it.baseId = it.baseId || it.rootId;
  84. delete it.isTop;
  85. it.dataPathArr = [undefined];
  86. out += ' var vErrors = null; ';
  87. out += ' var errors = 0; ';
  88. out += ' if (rootData === undefined) rootData = data; ';
  89. } else {
  90. var $lvl = it.level,
  91. $dataLvl = it.dataLevel,
  92. $data = 'data' + ($dataLvl || '');
  93. if ($id) it.baseId = it.resolve.url(it.baseId, $id);
  94. if ($async && !it.async) throw new Error('async schema in sync schema');
  95. out += ' var errs_' + ($lvl) + ' = errors;';
  96. }
  97. var $valid = 'valid' + $lvl,
  98. $breakOnError = !it.opts.allErrors,
  99. $closingBraces1 = '',
  100. $closingBraces2 = '';
  101. var $errorKeyword;
  102. var $typeSchema = it.schema.type,
  103. $typeIsArray = Array.isArray($typeSchema);
  104. if ($typeIsArray && $typeSchema.length == 1) {
  105. $typeSchema = $typeSchema[0];
  106. $typeIsArray = false;
  107. }
  108. if (it.schema.$ref && $refKeywords) {
  109. if (it.opts.extendRefs == 'fail') {
  110. throw new Error('$ref: validation keywords used in schema at path "' + it.errSchemaPath + '" (see option extendRefs)');
  111. } else if (it.opts.extendRefs !== true) {
  112. $refKeywords = false;
  113. it.logger.warn('$ref: keywords ignored in schema at path "' + it.errSchemaPath + '"');
  114. }
  115. }
  116. if (it.schema.$comment && it.opts.$comment) {
  117. out += ' ' + (it.RULES.all.$comment.code(it, '$comment'));
  118. }
  119. if ($typeSchema) {
  120. if (it.opts.coerceTypes) {
  121. var $coerceToTypes = it.util.coerceToTypes(it.opts.coerceTypes, $typeSchema);
  122. }
  123. var $rulesGroup = it.RULES.types[$typeSchema];
  124. if ($coerceToTypes || $typeIsArray || $rulesGroup === true || ($rulesGroup && !$shouldUseGroup($rulesGroup))) {
  125. var $schemaPath = it.schemaPath + '.type',
  126. $errSchemaPath = it.errSchemaPath + '/type';
  127. var $schemaPath = it.schemaPath + '.type',
  128. $errSchemaPath = it.errSchemaPath + '/type',
  129. $method = $typeIsArray ? 'checkDataTypes' : 'checkDataType';
  130. out += ' if (' + (it.util[$method]($typeSchema, $data, true)) + ') { ';
  131. if ($coerceToTypes) {
  132. var $dataType = 'dataType' + $lvl,
  133. $coerced = 'coerced' + $lvl;
  134. out += ' var ' + ($dataType) + ' = typeof ' + ($data) + '; ';
  135. if (it.opts.coerceTypes == 'array') {
  136. out += ' if (' + ($dataType) + ' == \'object\' && Array.isArray(' + ($data) + ')) ' + ($dataType) + ' = \'array\'; ';
  137. }
  138. out += ' var ' + ($coerced) + ' = undefined; ';
  139. var $bracesCoercion = '';
  140. var arr1 = $coerceToTypes;
  141. if (arr1) {
  142. var $type, $i = -1,
  143. l1 = arr1.length - 1;
  144. while ($i < l1) {
  145. $type = arr1[$i += 1];
  146. if ($i) {
  147. out += ' if (' + ($coerced) + ' === undefined) { ';
  148. $bracesCoercion += '}';
  149. }
  150. if (it.opts.coerceTypes == 'array' && $type != 'array') {
  151. out += ' if (' + ($dataType) + ' == \'array\' && ' + ($data) + '.length == 1) { ' + ($coerced) + ' = ' + ($data) + ' = ' + ($data) + '[0]; ' + ($dataType) + ' = typeof ' + ($data) + '; } ';
  152. }
  153. if ($type == 'string') {
  154. out += ' if (' + ($dataType) + ' == \'number\' || ' + ($dataType) + ' == \'boolean\') ' + ($coerced) + ' = \'\' + ' + ($data) + '; else if (' + ($data) + ' === null) ' + ($coerced) + ' = \'\'; ';
  155. } else if ($type == 'number' || $type == 'integer') {
  156. out += ' if (' + ($dataType) + ' == \'boolean\' || ' + ($data) + ' === null || (' + ($dataType) + ' == \'string\' && ' + ($data) + ' && ' + ($data) + ' == +' + ($data) + ' ';
  157. if ($type == 'integer') {
  158. out += ' && !(' + ($data) + ' % 1)';
  159. }
  160. out += ')) ' + ($coerced) + ' = +' + ($data) + '; ';
  161. } else if ($type == 'boolean') {
  162. out += ' if (' + ($data) + ' === \'false\' || ' + ($data) + ' === 0 || ' + ($data) + ' === null) ' + ($coerced) + ' = false; else if (' + ($data) + ' === \'true\' || ' + ($data) + ' === 1) ' + ($coerced) + ' = true; ';
  163. } else if ($type == 'null') {
  164. out += ' if (' + ($data) + ' === \'\' || ' + ($data) + ' === 0 || ' + ($data) + ' === false) ' + ($coerced) + ' = null; ';
  165. } else if (it.opts.coerceTypes == 'array' && $type == 'array') {
  166. out += ' if (' + ($dataType) + ' == \'string\' || ' + ($dataType) + ' == \'number\' || ' + ($dataType) + ' == \'boolean\' || ' + ($data) + ' == null) ' + ($coerced) + ' = [' + ($data) + ']; ';
  167. }
  168. }
  169. }
  170. out += ' ' + ($bracesCoercion) + ' if (' + ($coerced) + ' === undefined) { ';
  171. var $$outStack = $$outStack || [];
  172. $$outStack.push(out);
  173. out = ''; /* istanbul ignore else */
  174. if (it.createErrors !== false) {
  175. out += ' { keyword: \'' + ($errorKeyword || 'type') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { type: \'';
  176. if ($typeIsArray) {
  177. out += '' + ($typeSchema.join(","));
  178. } else {
  179. out += '' + ($typeSchema);
  180. }
  181. out += '\' } ';
  182. if (it.opts.messages !== false) {
  183. out += ' , message: \'should be ';
  184. if ($typeIsArray) {
  185. out += '' + ($typeSchema.join(","));
  186. } else {
  187. out += '' + ($typeSchema);
  188. }
  189. out += '\' ';
  190. }
  191. if (it.opts.verbose) {
  192. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  193. }
  194. out += ' } ';
  195. } else {
  196. out += ' {} ';
  197. }
  198. var __err = out;
  199. out = $$outStack.pop();
  200. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  201. if (it.async) {
  202. out += ' throw new ValidationError([' + (__err) + ']); ';
  203. } else {
  204. out += ' validate.errors = [' + (__err) + ']; return false; ';
  205. }
  206. } else {
  207. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  208. }
  209. out += ' } else { ';
  210. var $parentData = $dataLvl ? 'data' + (($dataLvl - 1) || '') : 'parentData',
  211. $parentDataProperty = $dataLvl ? it.dataPathArr[$dataLvl] : 'parentDataProperty';
  212. out += ' ' + ($data) + ' = ' + ($coerced) + '; ';
  213. if (!$dataLvl) {
  214. out += 'if (' + ($parentData) + ' !== undefined)';
  215. }
  216. out += ' ' + ($parentData) + '[' + ($parentDataProperty) + '] = ' + ($coerced) + '; } ';
  217. } else {
  218. var $$outStack = $$outStack || [];
  219. $$outStack.push(out);
  220. out = ''; /* istanbul ignore else */
  221. if (it.createErrors !== false) {
  222. out += ' { keyword: \'' + ($errorKeyword || 'type') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { type: \'';
  223. if ($typeIsArray) {
  224. out += '' + ($typeSchema.join(","));
  225. } else {
  226. out += '' + ($typeSchema);
  227. }
  228. out += '\' } ';
  229. if (it.opts.messages !== false) {
  230. out += ' , message: \'should be ';
  231. if ($typeIsArray) {
  232. out += '' + ($typeSchema.join(","));
  233. } else {
  234. out += '' + ($typeSchema);
  235. }
  236. out += '\' ';
  237. }
  238. if (it.opts.verbose) {
  239. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  240. }
  241. out += ' } ';
  242. } else {
  243. out += ' {} ';
  244. }
  245. var __err = out;
  246. out = $$outStack.pop();
  247. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  248. if (it.async) {
  249. out += ' throw new ValidationError([' + (__err) + ']); ';
  250. } else {
  251. out += ' validate.errors = [' + (__err) + ']; return false; ';
  252. }
  253. } else {
  254. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  255. }
  256. }
  257. out += ' } ';
  258. }
  259. }
  260. if (it.schema.$ref && !$refKeywords) {
  261. out += ' ' + (it.RULES.all.$ref.code(it, '$ref')) + ' ';
  262. if ($breakOnError) {
  263. out += ' } if (errors === ';
  264. if ($top) {
  265. out += '0';
  266. } else {
  267. out += 'errs_' + ($lvl);
  268. }
  269. out += ') { ';
  270. $closingBraces2 += '}';
  271. }
  272. } else {
  273. var arr2 = it.RULES;
  274. if (arr2) {
  275. var $rulesGroup, i2 = -1,
  276. l2 = arr2.length - 1;
  277. while (i2 < l2) {
  278. $rulesGroup = arr2[i2 += 1];
  279. if ($shouldUseGroup($rulesGroup)) {
  280. if ($rulesGroup.type) {
  281. out += ' if (' + (it.util.checkDataType($rulesGroup.type, $data)) + ') { ';
  282. }
  283. if (it.opts.useDefaults && !it.compositeRule) {
  284. if ($rulesGroup.type == 'object' && it.schema.properties) {
  285. var $schema = it.schema.properties,
  286. $schemaKeys = Object.keys($schema);
  287. var arr3 = $schemaKeys;
  288. if (arr3) {
  289. var $propertyKey, i3 = -1,
  290. l3 = arr3.length - 1;
  291. while (i3 < l3) {
  292. $propertyKey = arr3[i3 += 1];
  293. var $sch = $schema[$propertyKey];
  294. if ($sch.default !== undefined) {
  295. var $passData = $data + it.util.getProperty($propertyKey);
  296. out += ' if (' + ($passData) + ' === undefined) ' + ($passData) + ' = ';
  297. if (it.opts.useDefaults == 'shared') {
  298. out += ' ' + (it.useDefault($sch.default)) + ' ';
  299. } else {
  300. out += ' ' + (JSON.stringify($sch.default)) + ' ';
  301. }
  302. out += '; ';
  303. }
  304. }
  305. }
  306. } else if ($rulesGroup.type == 'array' && Array.isArray(it.schema.items)) {
  307. var arr4 = it.schema.items;
  308. if (arr4) {
  309. var $sch, $i = -1,
  310. l4 = arr4.length - 1;
  311. while ($i < l4) {
  312. $sch = arr4[$i += 1];
  313. if ($sch.default !== undefined) {
  314. var $passData = $data + '[' + $i + ']';
  315. out += ' if (' + ($passData) + ' === undefined) ' + ($passData) + ' = ';
  316. if (it.opts.useDefaults == 'shared') {
  317. out += ' ' + (it.useDefault($sch.default)) + ' ';
  318. } else {
  319. out += ' ' + (JSON.stringify($sch.default)) + ' ';
  320. }
  321. out += '; ';
  322. }
  323. }
  324. }
  325. }
  326. }
  327. var arr5 = $rulesGroup.rules;
  328. if (arr5) {
  329. var $rule, i5 = -1,
  330. l5 = arr5.length - 1;
  331. while (i5 < l5) {
  332. $rule = arr5[i5 += 1];
  333. if ($shouldUseRule($rule)) {
  334. var $code = $rule.code(it, $rule.keyword, $rulesGroup.type);
  335. if ($code) {
  336. out += ' ' + ($code) + ' ';
  337. if ($breakOnError) {
  338. $closingBraces1 += '}';
  339. }
  340. }
  341. }
  342. }
  343. }
  344. if ($breakOnError) {
  345. out += ' ' + ($closingBraces1) + ' ';
  346. $closingBraces1 = '';
  347. }
  348. if ($rulesGroup.type) {
  349. out += ' } ';
  350. if ($typeSchema && $typeSchema === $rulesGroup.type && !$coerceToTypes) {
  351. out += ' else { ';
  352. var $schemaPath = it.schemaPath + '.type',
  353. $errSchemaPath = it.errSchemaPath + '/type';
  354. var $$outStack = $$outStack || [];
  355. $$outStack.push(out);
  356. out = ''; /* istanbul ignore else */
  357. if (it.createErrors !== false) {
  358. out += ' { keyword: \'' + ($errorKeyword || 'type') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { type: \'';
  359. if ($typeIsArray) {
  360. out += '' + ($typeSchema.join(","));
  361. } else {
  362. out += '' + ($typeSchema);
  363. }
  364. out += '\' } ';
  365. if (it.opts.messages !== false) {
  366. out += ' , message: \'should be ';
  367. if ($typeIsArray) {
  368. out += '' + ($typeSchema.join(","));
  369. } else {
  370. out += '' + ($typeSchema);
  371. }
  372. out += '\' ';
  373. }
  374. if (it.opts.verbose) {
  375. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  376. }
  377. out += ' } ';
  378. } else {
  379. out += ' {} ';
  380. }
  381. var __err = out;
  382. out = $$outStack.pop();
  383. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  384. if (it.async) {
  385. out += ' throw new ValidationError([' + (__err) + ']); ';
  386. } else {
  387. out += ' validate.errors = [' + (__err) + ']; return false; ';
  388. }
  389. } else {
  390. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  391. }
  392. out += ' } ';
  393. }
  394. }
  395. if ($breakOnError) {
  396. out += ' if (errors === ';
  397. if ($top) {
  398. out += '0';
  399. } else {
  400. out += 'errs_' + ($lvl);
  401. }
  402. out += ') { ';
  403. $closingBraces2 += '}';
  404. }
  405. }
  406. }
  407. }
  408. }
  409. if ($breakOnError) {
  410. out += ' ' + ($closingBraces2) + ' ';
  411. }
  412. if ($top) {
  413. if ($async) {
  414. out += ' if (errors === 0) return data; ';
  415. out += ' else throw new ValidationError(vErrors); ';
  416. } else {
  417. out += ' validate.errors = vErrors; ';
  418. out += ' return errors === 0; ';
  419. }
  420. out += ' }; return validate;';
  421. } else {
  422. out += ' var ' + ($valid) + ' = errors === errs_' + ($lvl) + ';';
  423. }
  424. out = it.util.cleanUpCode(out);
  425. if ($top) {
  426. out = it.util.finalCleanUpCode(out, $async);
  427. }
  428. function $shouldUseGroup($rulesGroup) {
  429. var rules = $rulesGroup.rules;
  430. for (var i = 0; i < rules.length; i++)
  431. if ($shouldUseRule(rules[i])) return true;
  432. }
  433. function $shouldUseRule($rule) {
  434. return it.schema[$rule.keyword] !== undefined || ($rule.implements && $ruleImplementsSomeKeyword($rule));
  435. }
  436. function $ruleImplementsSomeKeyword($rule) {
  437. var impl = $rule.implements;
  438. for (var i = 0; i < impl.length; i++)
  439. if (it.schema[impl[i]] !== undefined) return true;
  440. }
  441. return out;
  442. }