validate.js 18 KB

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