errorMessage.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. 'use strict';
  2. module.exports = function generate_errorMessage(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. if (it.createErrors !== false) {
  12. var INTERPOLATION = /\$\{[^\}]+\}/;
  13. var INTERPOLATION_REPLACE = /\$\{([^\}]+)\}/g;
  14. var EMPTY_STR = /^\'\'\s*\+\s*|\s*\+\s*\'\'$/g;
  15. var $config = it.self.getKeyword($keyword).config,
  16. $dataPath = '_em_dataPath' + $lvl,
  17. $i = '_em_i' + $lvl,
  18. $key = '_em_key' + $lvl,
  19. $keyProp = '_em_keyProp' + $lvl,
  20. $err = '_em_err' + $lvl,
  21. $child = '_em_child' + $lvl,
  22. $childKeyword = '_em_childKeyword' + $lvl,
  23. $matches = '_em_matches' + $lvl,
  24. $isArray = '_em_isArray' + $lvl,
  25. $errors = '_em_errors' + $lvl,
  26. $message = '_em_message' + $lvl,
  27. $paramsErrors = '_em_paramsErrors' + $lvl,
  28. $propParam = '_em_propParam' + $lvl,
  29. $keywordPropParams = '_em_keywordPropParams' + $lvl,
  30. $templates = '_em_templates' + $lvl,
  31. $errSchemaPathString = it.util.toQuotedString(it.errSchemaPath);
  32. out += ' if (errors > 0) { var ' + ($dataPath) + ' = (dataPath || \'\') + ' + (it.errorPath) + '; var ' + ($i) + ', ' + ($err) + ', ' + ($errors) + '; ';
  33. if (typeof $schema == 'object') {
  34. var $keywordErrors = {},
  35. $keywordPropErrors = {},
  36. $childErrors = {
  37. properties: {},
  38. items: {}
  39. },
  40. $hasKeywordProps = false,
  41. $hasProperties = false,
  42. $hasItems = false;
  43. for (var $k in $schema) {
  44. switch ($k) {
  45. case 'properties':
  46. for (var $prop in $schema.properties) {
  47. $hasProperties = true;
  48. $childErrors.properties[$prop] = [];
  49. }
  50. break;
  51. case 'items':
  52. for (var $item = 0; $item < $schema.items.length; $item++) {
  53. $hasItems = true;
  54. $childErrors.items[$item] = [];
  55. }
  56. break;
  57. default:
  58. if (typeof $schema[$k] == 'object') {
  59. $hasKeywordProps = true;
  60. $keywordPropErrors[$k] = {};
  61. for (var $prop in $schema[$k]) {
  62. $keywordPropErrors[$k][$prop] = [];
  63. }
  64. } else {
  65. $keywordErrors[$k] = [];
  66. }
  67. }
  68. }
  69. var $keywordErrorsArr = Object.keys($keywordErrors);
  70. if ($keywordErrorsArr.length) {
  71. out += ' ' + ($i) + ' = 0; ' + ($errors) + ' = ' + (JSON.stringify($keywordErrors)) + '; var ' + ($templates) + ' = { ';
  72. var $comma = false;
  73. var arr1 = $keywordErrorsArr;
  74. if (arr1) {
  75. var $k, i1 = -1,
  76. l1 = arr1.length - 1;
  77. while (i1 < l1) {
  78. $k = arr1[i1 += 1];
  79. if (INTERPOLATION.test($schema[$k])) {
  80. if ($comma) {
  81. out += ',';
  82. }
  83. out += '' + (it.util.toQuotedString($k)) + ': ' + (templateFunc($schema[$k])) + ' ';
  84. $comma = true;
  85. }
  86. }
  87. }
  88. out += ' }; while (' + ($i) + ' < errors) { ' + ($err) + ' = vErrors[' + ($i) + ']; if ( ' + ($err) + '.keyword != \'' + ($keyword) + '\' ';
  89. if ($config.options.keepErrors) {
  90. out += ' && !' + ($err) + '.emUsed ';
  91. }
  92. out += ' && ' + ($err) + '.keyword in ' + ($errors) + ' && ' + ($err) + '.dataPath == ' + ($dataPath) + ' && ' + ($err) + '.schemaPath.indexOf(' + ($errSchemaPathString) + ') == 0 && /^\\/[^\\/]*$/.test(' + ($err) + '.schemaPath.slice(' + (it.errSchemaPath.length) + '))) { ' + ($errors) + '[' + ($err) + '.keyword].push(' + ($err) + '); ';
  93. if ($config.options.keepErrors) {
  94. out += ' ' + ($err) + '.emUsed = true; ';
  95. } else {
  96. out += ' vErrors.splice(' + ($i) + ', 1); errors--; ';
  97. }
  98. out += ' } else { ' + ($i) + '++; } } ';
  99. if ($config.options.singleError) {
  100. out += ' var ' + ($message) + ' = \'\'; var ' + ($paramsErrors) + ' = []; ';
  101. }
  102. out += ' for (var ' + ($key) + ' in ' + ($errors) + ') { if (' + ($errors) + '[' + ($key) + '].length) { ';
  103. if ($config.options.singleError) {
  104. out += ' if (' + ($message) + ') { ' + ($message) + ' += ';
  105. if (typeof $config.options.singleError == 'string') {
  106. out += ' ' + (it.util.toQuotedString($config.options.singleError)) + ' ';
  107. } else {
  108. out += ' \'; \' ';
  109. }
  110. out += '; } ' + ($message) + ' += ' + ($key) + ' in ' + ($templates) + ' ? ' + ($templates) + '[' + ($key) + '] () : validate.schema' + ($schemaPath) + '[' + ($key) + ']; ' + ($paramsErrors) + ' = ' + ($paramsErrors) + '.concat(' + ($errors) + '[' + ($key) + ']); } } ';
  111. } else {
  112. out += ' var ' + ($message) + ' = ' + ($key) + ' in ' + ($templates) + ' ? ' + ($templates) + '[' + ($key) + '] () : validate.schema' + ($schemaPath) + '[' + ($key) + ']; var ' + ($paramsErrors) + ' = ' + ($errors) + '[' + ($key) + ']; ';
  113. }
  114. out += ' var err = { keyword: \'' + ($keyword) + '\' , dataPath: ' + ($dataPath) + ' , schemaPath: ' + ($errSchemaPathString) + ' + \'/' + ($keyword) + '\' , params: { errors: ' + ($paramsErrors) + ' } , message: ' + ($message) + ' ';
  115. if (it.opts.verbose) {
  116. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  117. }
  118. out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  119. if (!$config.options.singleError) {
  120. out += ' } } ';
  121. }
  122. }
  123. if ($hasKeywordProps) {
  124. out += ' ' + ($i) + ' = 0; ' + ($errors) + ' = ' + (JSON.stringify($keywordPropErrors)) + '; var ' + ($paramsErrors) + ', ' + ($propParam) + '; var ' + ($keywordPropParams) + ' = ' + (JSON.stringify($config.KEYWORD_PROPERTY_PARAMS)) + '; var ' + ($templates) + ' = { ';
  125. var $comma = false;
  126. var arr2 = Object.keys($keywordPropErrors);
  127. if (arr2) {
  128. var $k, i2 = -1,
  129. l2 = arr2.length - 1;
  130. while (i2 < l2) {
  131. $k = arr2[i2 += 1];
  132. var $keywordMsgs = $schema[$k];
  133. if ($comma) {
  134. out += ',';
  135. }
  136. out += '' + (it.util.toQuotedString($k)) + ': { ';
  137. $comma = true;
  138. var $innerComma = false;
  139. var arr3 = Object.keys($keywordMsgs);
  140. if (arr3) {
  141. var $prop, i3 = -1,
  142. l3 = arr3.length - 1;
  143. while (i3 < l3) {
  144. $prop = arr3[i3 += 1];
  145. if (INTERPOLATION.test($keywordMsgs[$prop])) {
  146. if ($innerComma) {
  147. out += ',';
  148. }
  149. out += '' + (it.util.toQuotedString($prop)) + ': ' + (templateFunc($keywordMsgs[$prop])) + ' ';
  150. $innerComma = true;
  151. }
  152. }
  153. }
  154. out += ' } ';
  155. }
  156. }
  157. out += ' }; while (' + ($i) + ' < errors) { ' + ($err) + ' = vErrors[' + ($i) + ']; if ( ' + ($err) + '.keyword != \'' + ($keyword) + '\' ';
  158. if ($config.options.keepErrors) {
  159. out += ' && !' + ($err) + '.emUsed ';
  160. }
  161. out += ' && ' + ($err) + '.keyword in ' + ($errors) + ' && ' + ($err) + '.dataPath == ' + ($dataPath) + ' && ' + ($err) + '.schemaPath.indexOf(' + ($errSchemaPathString) + ') == 0 && /^\\/[^\\/]*$/.test(' + ($err) + '.schemaPath.slice(' + (it.errSchemaPath.length) + '))) { ' + ($propParam) + ' = ' + ($keywordPropParams) + '[' + ($err) + '.keyword]; ' + ($paramsErrors) + ' = ' + ($errors) + '[' + ($err) + '.keyword][' + ($err) + '.params[' + ($propParam) + ']]; if (' + ($paramsErrors) + ') { ' + ($paramsErrors) + '.push(' + ($err) + '); ';
  162. if ($config.options.keepErrors) {
  163. out += ' ' + ($err) + '.emUsed = true; ';
  164. } else {
  165. out += ' vErrors.splice(' + ($i) + ', 1); errors--; ';
  166. }
  167. out += ' } else { ' + ($i) + '++; } } else { ' + ($i) + '++; } } for (var ' + ($key) + ' in ' + ($errors) + ') { for (var ' + ($keyProp) + ' in ' + ($errors) + '[' + ($key) + ']) { ' + ($paramsErrors) + ' = ' + ($errors) + '[' + ($key) + '][' + ($keyProp) + ']; if (' + ($paramsErrors) + '.length) { var ' + ($message) + ' = ' + ($key) + ' in ' + ($templates) + ' && ' + ($keyProp) + ' in ' + ($templates) + '[' + ($key) + '] ? ' + ($templates) + '[' + ($key) + '][' + ($keyProp) + '] () : validate.schema' + ($schemaPath) + '[' + ($key) + '][' + ($keyProp) + ']; var err = { keyword: \'' + ($keyword) + '\' , dataPath: ' + ($dataPath) + ' , schemaPath: ' + ($errSchemaPathString) + ' + \'/' + ($keyword) + '\' , params: { errors: ' + ($paramsErrors) + ' } , message: ' + ($message) + ' ';
  168. if (it.opts.verbose) {
  169. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  170. }
  171. out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } } } ';
  172. }
  173. if ($hasProperties || $hasItems) {
  174. out += ' var ' + ($isArray) + ' = Array.isArray(' + ($data) + '); if ';
  175. if ($hasProperties && $hasItems) {
  176. out += ' (typeof ' + ($data) + ' == \'object\') { ';
  177. var $childProp = '[' + $childKeyword + ']';
  178. out += ' ' + ($i) + ' = 0; if (' + ($isArray) + ') { var ' + ($childKeyword) + ' = \'items\'; ' + ($errors) + ' = ' + (JSON.stringify($childErrors.items)) + '; ';
  179. var _keysArray = Object.keys($childErrors.items);
  180. out += ' var ' + ($templates) + ' = { ';
  181. var $comma = false;
  182. var arr4 = _keysArray;
  183. if (arr4) {
  184. var $k, i4 = -1,
  185. l4 = arr4.length - 1;
  186. while (i4 < l4) {
  187. $k = arr4[i4 += 1];
  188. if (INTERPOLATION.test($schema.items[$k])) {
  189. if ($comma) {
  190. out += ',';
  191. }
  192. out += '' + (it.util.toQuotedString($k)) + ': ' + (templateFunc($schema.items[$k])) + ' ';
  193. $comma = true;
  194. }
  195. }
  196. }
  197. out += ' }; } else { var ' + ($childKeyword) + ' = \'properties\'; ' + ($errors) + ' = ' + (JSON.stringify($childErrors.properties)) + '; ';
  198. var _keysArray = Object.keys($childErrors.properties);
  199. out += ' var ' + ($templates) + ' = { ';
  200. var $comma = false;
  201. var arr5 = _keysArray;
  202. if (arr5) {
  203. var $k, i5 = -1,
  204. l5 = arr5.length - 1;
  205. while (i5 < l5) {
  206. $k = arr5[i5 += 1];
  207. if (INTERPOLATION.test($schema.properties[$k])) {
  208. if ($comma) {
  209. out += ',';
  210. }
  211. out += '' + (it.util.toQuotedString($k)) + ': ' + (templateFunc($schema.properties[$k])) + ' ';
  212. $comma = true;
  213. }
  214. }
  215. }
  216. out += ' }; } ';
  217. } else if ($hasProperties) {
  218. out += ' (typeof ' + ($data) + ' == \'object\' && !' + ($isArray) + ') { ';
  219. var $childProp = '.properties';
  220. out += ' ' + ($i) + ' = 0; ' + ($errors) + ' = ' + (JSON.stringify($childErrors.properties)) + '; ';
  221. var _keysArray = Object.keys($childErrors.properties);
  222. out += ' var ' + ($templates) + ' = { ';
  223. var $comma = false;
  224. var arr6 = _keysArray;
  225. if (arr6) {
  226. var $k, i6 = -1,
  227. l6 = arr6.length - 1;
  228. while (i6 < l6) {
  229. $k = arr6[i6 += 1];
  230. if (INTERPOLATION.test($schema.properties[$k])) {
  231. if ($comma) {
  232. out += ',';
  233. }
  234. out += '' + (it.util.toQuotedString($k)) + ': ' + (templateFunc($schema.properties[$k])) + ' ';
  235. $comma = true;
  236. }
  237. }
  238. }
  239. out += ' }; ';
  240. } else {
  241. out += ' (' + ($isArray) + ') { ';
  242. var $childProp = '.items';
  243. out += ' ' + ($i) + ' = 0; ' + ($errors) + ' = ' + (JSON.stringify($childErrors.items)) + '; ';
  244. var _keysArray = Object.keys($childErrors.items);
  245. out += ' var ' + ($templates) + ' = { ';
  246. var $comma = false;
  247. var arr7 = _keysArray;
  248. if (arr7) {
  249. var $k, i7 = -1,
  250. l7 = arr7.length - 1;
  251. while (i7 < l7) {
  252. $k = arr7[i7 += 1];
  253. if (INTERPOLATION.test($schema.items[$k])) {
  254. if ($comma) {
  255. out += ',';
  256. }
  257. out += '' + (it.util.toQuotedString($k)) + ': ' + (templateFunc($schema.items[$k])) + ' ';
  258. $comma = true;
  259. }
  260. }
  261. }
  262. out += ' }; ';
  263. }
  264. out += ' var ' + ($child) + ', ' + ($matches) + '; while (' + ($i) + ' < errors) { ' + ($err) + ' = vErrors[' + ($i) + ']; if ( ' + ($err) + '.keyword != \'' + ($keyword) + '\' ';
  265. if ($config.options.keepErrors) {
  266. out += ' && !' + ($err) + '.emUsed ';
  267. }
  268. out += ' && ' + ($err) + '.dataPath.indexOf(' + ($dataPath) + ') == 0 && (' + ($matches) + ' = ' + ($err) + '.dataPath.slice(' + ($dataPath) + '.length).match(/^\\/([^\\/]*)(?:\\/|$)/), ' + ($child) + ' = ' + ($matches) + ' && ' + ($matches) + '[1].replace(/~1/g, \'/\').replace(/~0/g, \'~\') ) !== undefined && ' + ($child) + ' in ' + ($errors) + ') { ' + ($errors) + '[' + ($child) + '].push(' + ($err) + '); ';
  269. if ($config.options.keepErrors) {
  270. out += ' ' + ($err) + '.emUsed = true; ';
  271. } else {
  272. out += ' vErrors.splice(' + ($i) + ', 1); errors--; ';
  273. }
  274. out += ' } else { ' + ($i) + '++; } } for (var ' + ($key) + ' in ' + ($errors) + ') { if (' + ($errors) + '[' + ($key) + '].length) { var err = { keyword: \'' + ($keyword) + '\' , dataPath: ' + ($dataPath) + ' + \'/\' + ' + ($key) + '.replace(/~/g, \'~0\').replace(/\\//g, \'~1\') , schemaPath: ' + ($errSchemaPathString) + ' + \'/' + ($keyword) + '\' , params: { errors: ' + ($errors) + '[' + ($key) + '] } , message: ' + ($key) + ' in ' + ($templates) + ' ? ' + ($templates) + '[' + ($key) + '] () : validate.schema' + ($schemaPath) + ($childProp) + '[' + ($key) + '] ';
  275. if (it.opts.verbose) {
  276. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  277. }
  278. out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } } } ';
  279. }
  280. }
  281. var $schemaMessage = typeof $schema == 'string' ? $schema : $schema._;
  282. if ($schemaMessage) {
  283. out += ' ' + ($i) + ' = 0; ' + ($errors) + ' = []; while (' + ($i) + ' < errors) { ' + ($err) + ' = vErrors[' + ($i) + ']; if ( ' + ($err) + '.keyword != \'' + ($keyword) + '\' ';
  284. if ($config.options.keepErrors) {
  285. out += ' && !' + ($err) + '.emUsed ';
  286. }
  287. out += ' && (' + ($err) + '.dataPath == ' + ($dataPath) + ' || (' + ($err) + '.dataPath.indexOf(' + ($dataPath) + ') == 0 && ' + ($err) + '.dataPath[' + ($dataPath) + '.length] == \'/\')) && ' + ($err) + '.schemaPath.indexOf(' + ($errSchemaPathString) + ') == 0 && ' + ($err) + '.schemaPath[' + (it.errSchemaPath.length) + '] == \'/\') { ' + ($errors) + '.push(' + ($err) + '); ';
  288. if ($config.options.keepErrors) {
  289. out += ' ' + ($err) + '.emUsed = true; ';
  290. } else {
  291. out += ' vErrors.splice(' + ($i) + ', 1); errors--; ';
  292. }
  293. out += ' } else { ' + ($i) + '++; } } if (' + ($errors) + '.length) { var err = { keyword: \'' + ($keyword) + '\' , dataPath: ' + ($dataPath) + ' , schemaPath: ' + ($errSchemaPathString) + ' + \'/' + ($keyword) + '\' , params: { errors: ' + ($errors) + ' } , message: ' + (templateExpr($schemaMessage)) + ' ';
  294. if (it.opts.verbose) {
  295. out += ' , schema: ' + (it.util.toQuotedString($schemaMessage)) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  296. }
  297. out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } ';
  298. }
  299. out += ' }';
  300. }
  301. function templateExpr(str) {
  302. str = it.util.escapeQuotes(str);
  303. if (!INTERPOLATION.test(str)) return "'" + str + "'";
  304. var expr = "'" + str.replace(INTERPOLATION_REPLACE, function($0, $1) {
  305. return "' + JSON.stringify(" + it.util.getData($1, $dataLvl, it.dataPathArr) + ") + '";
  306. }) + "'";
  307. return expr.replace(EMPTY_STR, '');
  308. }
  309. function templateFunc(str) {
  310. return 'function() { return ' + templateExpr(str) + '; }';
  311. }
  312. return out;
  313. }