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.

372 lines
12 KiB

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