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