custom.jst 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. {{# def.definitions }}
  2. {{# def.errors }}
  3. {{# def.setupKeyword }}
  4. {{# def.$data }}
  5. {{
  6. var $rule = this
  7. , $definition = 'definition' + $lvl
  8. , $rDef = $rule.definition
  9. , $closingBraces = '';
  10. var $validate = $rDef.validate;
  11. var $compile, $inline, $macro, $ruleValidate, $validateCode;
  12. }}
  13. {{? $isData && $rDef.$data }}
  14. {{
  15. $validateCode = 'keywordValidate' + $lvl;
  16. var $validateSchema = $rDef.validateSchema;
  17. }}
  18. var {{=$definition}} = RULES.custom['{{=$keyword}}'].definition;
  19. var {{=$validateCode}} = {{=$definition}}.validate;
  20. {{??}}
  21. {{
  22. $ruleValidate = it.useCustomRule($rule, $schema, it.schema, it);
  23. if (!$ruleValidate) return;
  24. $schemaValue = 'validate.schema' + $schemaPath;
  25. $validateCode = $ruleValidate.code;
  26. $compile = $rDef.compile;
  27. $inline = $rDef.inline;
  28. $macro = $rDef.macro;
  29. }}
  30. {{?}}
  31. {{
  32. var $ruleErrs = $validateCode + '.errors'
  33. , $i = 'i' + $lvl
  34. , $ruleErr = 'ruleErr' + $lvl
  35. , $asyncKeyword = $rDef.async;
  36. if ($asyncKeyword && !it.async)
  37. throw new Error('async keyword in sync schema');
  38. }}
  39. {{? !($inline || $macro) }}{{=$ruleErrs}} = null;{{?}}
  40. var {{=$errs}} = errors;
  41. var {{=$valid}};
  42. {{## def.callRuleValidate:
  43. {{=$validateCode}}.call(
  44. {{? it.opts.passContext }}this{{??}}self{{?}}
  45. {{? $compile || $rDef.schema === false }}
  46. , {{=$data}}
  47. {{??}}
  48. , {{=$schemaValue}}
  49. , {{=$data}}
  50. , validate.schema{{=it.schemaPath}}
  51. {{?}}
  52. , {{# def.dataPath }}
  53. {{# def.passParentData }}
  54. , rootData
  55. )
  56. #}}
  57. {{## def.extendErrors:_inline:
  58. for (var {{=$i}}={{=$errs}}; {{=$i}}<errors; {{=$i}}++) {
  59. var {{=$ruleErr}} = vErrors[{{=$i}}];
  60. if ({{=$ruleErr}}.dataPath === undefined)
  61. {{=$ruleErr}}.dataPath = (dataPath || '') + {{= it.errorPath }};
  62. {{# _inline ? 'if (\{\{=$ruleErr\}\}.schemaPath === undefined) {' : '' }}
  63. {{=$ruleErr}}.schemaPath = "{{=$errSchemaPath}}";
  64. {{# _inline ? '}' : '' }}
  65. {{? it.opts.verbose }}
  66. {{=$ruleErr}}.schema = {{=$schemaValue}};
  67. {{=$ruleErr}}.data = {{=$data}};
  68. {{?}}
  69. }
  70. #}}
  71. {{? $isData && $rDef.$data }}
  72. {{ $closingBraces += '}'; }}
  73. if ({{=$schemaValue}} === undefined) {
  74. {{=$valid}} = true;
  75. } else {
  76. {{? $validateSchema }}
  77. {{ $closingBraces += '}'; }}
  78. {{=$valid}} = {{=$definition}}.validateSchema({{=$schemaValue}});
  79. if ({{=$valid}}) {
  80. {{?}}
  81. {{?}}
  82. {{? $inline }}
  83. {{? $rDef.statements }}
  84. {{= $ruleValidate.validate }}
  85. {{??}}
  86. {{=$valid}} = {{= $ruleValidate.validate }};
  87. {{?}}
  88. {{?? $macro }}
  89. {{# def.setupNextLevel }}
  90. {{
  91. $it.schema = $ruleValidate.validate;
  92. $it.schemaPath = '';
  93. }}
  94. {{# def.setCompositeRule }}
  95. {{ var $code = it.validate($it).replace(/validate\.schema/g, $validateCode); }}
  96. {{# def.resetCompositeRule }}
  97. {{= $code }}
  98. {{??}}
  99. {{# def.beginDefOut}}
  100. {{# def.callRuleValidate }}
  101. {{# def.storeDefOut:def_callRuleValidate }}
  102. {{? $rDef.errors === false }}
  103. {{=$valid}} = {{? $asyncKeyword }}await {{?}}{{= def_callRuleValidate }};
  104. {{??}}
  105. {{? $asyncKeyword }}
  106. {{ $ruleErrs = 'customErrors' + $lvl; }}
  107. var {{=$ruleErrs}} = null;
  108. try {
  109. {{=$valid}} = await {{= def_callRuleValidate }};
  110. } catch (e) {
  111. {{=$valid}} = false;
  112. if (e instanceof ValidationError) {{=$ruleErrs}} = e.errors;
  113. else throw e;
  114. }
  115. {{??}}
  116. {{=$ruleErrs}} = null;
  117. {{=$valid}} = {{= def_callRuleValidate }};
  118. {{?}}
  119. {{?}}
  120. {{?}}
  121. {{? $rDef.modifying }}
  122. if ({{=$parentData}}) {{=$data}} = {{=$parentData}}[{{=$parentDataProperty}}];
  123. {{?}}
  124. {{= $closingBraces }}
  125. {{## def.notValidationResult:
  126. {{? $rDef.valid === undefined }}
  127. !{{? $macro }}{{=$nextValid}}{{??}}{{=$valid}}{{?}}
  128. {{??}}
  129. {{= !$rDef.valid }}
  130. {{?}}
  131. #}}
  132. {{? $rDef.valid }}
  133. {{? $breakOnError }} if (true) { {{?}}
  134. {{??}}
  135. if ({{# def.notValidationResult }}) {
  136. {{ $errorKeyword = $rule.keyword; }}
  137. {{# def.beginDefOut}}
  138. {{# def.error:'custom' }}
  139. {{# def.storeDefOut:def_customError }}
  140. {{? $inline }}
  141. {{? $rDef.errors }}
  142. {{? $rDef.errors != 'full' }}
  143. {{# def.extendErrors:true }}
  144. {{?}}
  145. {{??}}
  146. {{? $rDef.errors === false}}
  147. {{= def_customError }}
  148. {{??}}
  149. if ({{=$errs}} == errors) {
  150. {{= def_customError }}
  151. } else {
  152. {{# def.extendErrors:true }}
  153. }
  154. {{?}}
  155. {{?}}
  156. {{?? $macro }}
  157. {{# def.extraError:'custom' }}
  158. {{??}}
  159. {{? $rDef.errors === false}}
  160. {{= def_customError }}
  161. {{??}}
  162. if (Array.isArray({{=$ruleErrs}})) {
  163. if (vErrors === null) vErrors = {{=$ruleErrs}};
  164. else vErrors = vErrors.concat({{=$ruleErrs}});
  165. errors = vErrors.length;
  166. {{# def.extendErrors:false }}
  167. } else {
  168. {{= def_customError }}
  169. }
  170. {{?}}
  171. {{?}}
  172. } {{? $breakOnError }} else { {{?}}
  173. {{?}}