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