custom.jst 4.6 KB

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