Initial commit
[yaffs-website] / node_modules / ajv / lib / dotjs / custom.js
1 'use strict';
2 module.exports = function generate_custom(it, $keyword) {
3   var out = ' ';
4   var $lvl = it.level;
5   var $dataLvl = it.dataLevel;
6   var $schema = it.schema[$keyword];
7   var $schemaPath = it.schemaPath + it.util.getProperty($keyword);
8   var $errSchemaPath = it.errSchemaPath + '/' + $keyword;
9   var $breakOnError = !it.opts.allErrors;
10   var $errorKeyword;
11   var $data = 'data' + ($dataLvl || '');
12   var $valid = 'valid' + $lvl;
13   var $errs = 'errs__' + $lvl;
14   var $isData = it.opts.v5 && $schema && $schema.$data,
15     $schemaValue;
16   if ($isData) {
17     out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
18     $schemaValue = 'schema' + $lvl;
19   } else {
20     $schemaValue = $schema;
21   }
22   var $rule = this,
23     $definition = 'definition' + $lvl,
24     $rDef = $rule.definition,
25     $validate = $rDef.validate,
26     $compile, $inline, $macro, $ruleValidate, $validateCode;
27   if ($isData && $rDef.$data) {
28     $validateCode = 'keywordValidate' + $lvl;
29     var $validateSchema = $rDef.validateSchema;
30     out += ' var ' + ($definition) + ' = RULES.custom[\'' + ($keyword) + '\'].definition; var ' + ($validateCode) + ' = ' + ($definition) + '.validate;';
31   } else {
32     $ruleValidate = it.useCustomRule($rule, $schema, it.schema, it);
33     $schemaValue = 'validate.schema' + $schemaPath;
34     $validateCode = $ruleValidate.code;
35     $compile = $rDef.compile;
36     $inline = $rDef.inline;
37     $macro = $rDef.macro;
38   }
39   var $ruleErrs = $validateCode + '.errors',
40     $i = 'i' + $lvl,
41     $ruleErr = 'ruleErr' + $lvl,
42     $asyncKeyword = $rDef.async;
43   if ($asyncKeyword && !it.async) throw new Error('async keyword in sync schema');
44   if (!($inline || $macro)) {
45     out += '' + ($ruleErrs) + ' = null;';
46   }
47   out += 'var ' + ($errs) + ' = errors;var ' + ($valid) + ';';
48   if ($validateSchema) {
49     out += ' ' + ($valid) + ' = ' + ($definition) + '.validateSchema(' + ($schemaValue) + '); if (' + ($valid) + ') {';
50   }
51   if ($inline) {
52     if ($rDef.statements) {
53       out += ' ' + ($ruleValidate.validate) + ' ';
54     } else {
55       out += ' ' + ($valid) + ' = ' + ($ruleValidate.validate) + '; ';
56     }
57   } else if ($macro) {
58     var $it = it.util.copy(it);
59     $it.level++;
60     var $nextValid = 'valid' + $it.level;
61     $it.schema = $ruleValidate.validate;
62     $it.schemaPath = '';
63     var $wasComposite = it.compositeRule;
64     it.compositeRule = $it.compositeRule = true;
65     var $code = it.validate($it).replace(/validate\.schema/g, $validateCode);
66     it.compositeRule = $it.compositeRule = $wasComposite;
67     out += ' ' + ($code);
68   } else {
69     var $$outStack = $$outStack || [];
70     $$outStack.push(out);
71     out = '';
72     out += '  ' + ($validateCode) + '.call( ';
73     if (it.opts.passContext) {
74       out += 'this';
75     } else {
76       out += 'self';
77     }
78     if ($compile || $rDef.schema === false) {
79       out += ' , ' + ($data) + ' ';
80     } else {
81       out += ' , ' + ($schemaValue) + ' , ' + ($data) + ' , validate.schema' + (it.schemaPath) + ' ';
82     }
83     out += ' , (dataPath || \'\')';
84     if (it.errorPath != '""') {
85       out += ' + ' + (it.errorPath);
86     }
87     var $parentData = $dataLvl ? 'data' + (($dataLvl - 1) || '') : 'parentData',
88       $parentDataProperty = $dataLvl ? it.dataPathArr[$dataLvl] : 'parentDataProperty';
89     out += ' , ' + ($parentData) + ' , ' + ($parentDataProperty) + ' , rootData )  ';
90     var def_callRuleValidate = out;
91     out = $$outStack.pop();
92     if ($rDef.errors === false) {
93       out += ' ' + ($valid) + ' = ';
94       if ($asyncKeyword) {
95         out += '' + (it.yieldAwait);
96       }
97       out += '' + (def_callRuleValidate) + '; ';
98     } else {
99       if ($asyncKeyword) {
100         $ruleErrs = 'customErrors' + $lvl;
101         out += ' var ' + ($ruleErrs) + ' = null; try { ' + ($valid) + ' = ' + (it.yieldAwait) + (def_callRuleValidate) + '; } catch (e) { ' + ($valid) + ' = false; if (e instanceof ValidationError) ' + ($ruleErrs) + ' = e.errors; else throw e; } ';
102       } else {
103         out += ' ' + ($ruleErrs) + ' = null; ' + ($valid) + ' = ' + (def_callRuleValidate) + '; ';
104       }
105     }
106   }
107   if ($rDef.modifying) {
108     out += ' ' + ($data) + ' = ' + ($parentData) + '[' + ($parentDataProperty) + '];';
109   }
110   if ($validateSchema) {
111     out += ' }';
112   }
113   if ($rDef.valid) {
114     if ($breakOnError) {
115       out += ' if (true) { ';
116     }
117   } else {
118     out += ' if ( ';
119     if ($rDef.valid === undefined) {
120       out += ' !';
121       if ($macro) {
122         out += '' + ($nextValid);
123       } else {
124         out += '' + ($valid);
125       }
126     } else {
127       out += ' ' + (!$rDef.valid) + ' ';
128     }
129     out += ') { ';
130     $errorKeyword = $rule.keyword;
131     var $$outStack = $$outStack || [];
132     $$outStack.push(out);
133     out = '';
134     var $$outStack = $$outStack || [];
135     $$outStack.push(out);
136     out = ''; /* istanbul ignore else */
137     if (it.createErrors !== false) {
138       out += ' { keyword: \'' + ($errorKeyword || 'custom') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { keyword: \'' + ($rule.keyword) + '\' } ';
139       if (it.opts.messages !== false) {
140         out += ' , message: \'should pass "' + ($rule.keyword) + '" keyword validation\' ';
141       }
142       if (it.opts.verbose) {
143         out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
144       }
145       out += ' } ';
146     } else {
147       out += ' {} ';
148     }
149     var __err = out;
150     out = $$outStack.pop();
151     if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
152       if (it.async) {
153         out += ' throw new ValidationError([' + (__err) + ']); ';
154       } else {
155         out += ' validate.errors = [' + (__err) + ']; return false; ';
156       }
157     } else {
158       out += ' var err = ' + (__err) + ';  if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
159     }
160     var def_customError = out;
161     out = $$outStack.pop();
162     if ($inline) {
163       if ($rDef.errors) {
164         if ($rDef.errors != 'full') {
165           out += '  for (var ' + ($i) + '=' + ($errs) + '; ' + ($i) + '<errors; ' + ($i) + '++) { var ' + ($ruleErr) + ' = vErrors[' + ($i) + ']; if (' + ($ruleErr) + '.dataPath === undefined) ' + ($ruleErr) + '.dataPath = (dataPath || \'\') + ' + (it.errorPath) + '; if (' + ($ruleErr) + '.schemaPath === undefined) { ' + ($ruleErr) + '.schemaPath = "' + ($errSchemaPath) + '"; } ';
166           if (it.opts.verbose) {
167             out += ' ' + ($ruleErr) + '.schema = ' + ($schemaValue) + '; ' + ($ruleErr) + '.data = ' + ($data) + '; ';
168           }
169           out += ' } ';
170         }
171       } else {
172         if ($rDef.errors === false) {
173           out += ' ' + (def_customError) + ' ';
174         } else {
175           out += ' if (' + ($errs) + ' == errors) { ' + (def_customError) + ' } else {  for (var ' + ($i) + '=' + ($errs) + '; ' + ($i) + '<errors; ' + ($i) + '++) { var ' + ($ruleErr) + ' = vErrors[' + ($i) + ']; if (' + ($ruleErr) + '.dataPath === undefined) ' + ($ruleErr) + '.dataPath = (dataPath || \'\') + ' + (it.errorPath) + '; if (' + ($ruleErr) + '.schemaPath === undefined) { ' + ($ruleErr) + '.schemaPath = "' + ($errSchemaPath) + '"; } ';
176           if (it.opts.verbose) {
177             out += ' ' + ($ruleErr) + '.schema = ' + ($schemaValue) + '; ' + ($ruleErr) + '.data = ' + ($data) + '; ';
178           }
179           out += ' } } ';
180         }
181       }
182     } else if ($macro) {
183       out += '   var err =   '; /* istanbul ignore else */
184       if (it.createErrors !== false) {
185         out += ' { keyword: \'' + ($errorKeyword || 'custom') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { keyword: \'' + ($rule.keyword) + '\' } ';
186         if (it.opts.messages !== false) {
187           out += ' , message: \'should pass "' + ($rule.keyword) + '" keyword validation\' ';
188         }
189         if (it.opts.verbose) {
190           out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
191         }
192         out += ' } ';
193       } else {
194         out += ' {} ';
195       }
196       out += ';  if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
197       if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
198         if (it.async) {
199           out += ' throw new ValidationError(vErrors); ';
200         } else {
201           out += ' validate.errors = vErrors; return false; ';
202         }
203       }
204     } else {
205       if ($rDef.errors === false) {
206         out += ' ' + (def_customError) + ' ';
207       } else {
208         out += ' if (Array.isArray(' + ($ruleErrs) + ')) { if (vErrors === null) vErrors = ' + ($ruleErrs) + '; else vErrors = vErrors.concat(' + ($ruleErrs) + '); errors = vErrors.length;  for (var ' + ($i) + '=' + ($errs) + '; ' + ($i) + '<errors; ' + ($i) + '++) { var ' + ($ruleErr) + ' = vErrors[' + ($i) + ']; if (' + ($ruleErr) + '.dataPath === undefined) ' + ($ruleErr) + '.dataPath = (dataPath || \'\') + ' + (it.errorPath) + ';  ' + ($ruleErr) + '.schemaPath = "' + ($errSchemaPath) + '";  ';
209         if (it.opts.verbose) {
210           out += ' ' + ($ruleErr) + '.schema = ' + ($schemaValue) + '; ' + ($ruleErr) + '.data = ' + ($data) + '; ';
211         }
212         out += ' } } else { ' + (def_customError) + ' } ';
213       }
214     }
215     out += ' } ';
216     if ($breakOnError) {
217       out += ' else { ';
218     }
219   }
220   return out;
221 }