validate.js 15 KB

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