Initial commit
[yaffs-website] / node_modules / ajv / lib / dotjs / ref.js
1 'use strict';
2 module.exports = function generate_ref(it, $keyword) {
3   var out = ' ';
4   var $lvl = it.level;
5   var $dataLvl = it.dataLevel;
6   var $schema = it.schema[$keyword];
7   var $errSchemaPath = it.errSchemaPath + '/' + $keyword;
8   var $breakOnError = !it.opts.allErrors;
9   var $errorKeyword;
10   var $data = 'data' + ($dataLvl || '');
11   var $valid = 'valid' + $lvl;
12   var $async, $refCode;
13   if ($schema == '#' || $schema == '#/') {
14     if (it.isRoot) {
15       $async = it.async;
16       $refCode = 'validate';
17     } else {
18       $async = it.root.schema.$async === true;
19       $refCode = 'root.refVal[0]';
20     }
21   } else {
22     var $refVal = it.resolveRef(it.baseId, $schema, it.isRoot);
23     if ($refVal === undefined) {
24       var $message = 'can\'t resolve reference ' + $schema + ' from id ' + it.baseId;
25       if (it.opts.missingRefs == 'fail') {
26         console.log($message);
27         var $$outStack = $$outStack || [];
28         $$outStack.push(out);
29         out = ''; /* istanbul ignore else */
30         if (it.createErrors !== false) {
31           out += ' { keyword: \'' + ($errorKeyword || '$ref') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { ref: \'' + (it.util.escapeQuotes($schema)) + '\' } ';
32           if (it.opts.messages !== false) {
33             out += ' , message: \'can\\\'t resolve reference ' + (it.util.escapeQuotes($schema)) + '\' ';
34           }
35           if (it.opts.verbose) {
36             out += ' , schema: ' + (it.util.toQuotedString($schema)) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
37           }
38           out += ' } ';
39         } else {
40           out += ' {} ';
41         }
42         var __err = out;
43         out = $$outStack.pop();
44         if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
45           if (it.async) {
46             out += ' throw new ValidationError([' + (__err) + ']); ';
47           } else {
48             out += ' validate.errors = [' + (__err) + ']; return false; ';
49           }
50         } else {
51           out += ' var err = ' + (__err) + ';  if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
52         }
53         if ($breakOnError) {
54           out += ' if (false) { ';
55         }
56       } else if (it.opts.missingRefs == 'ignore') {
57         console.log($message);
58         if ($breakOnError) {
59           out += ' if (true) { ';
60         }
61       } else {
62         var $error = new Error($message);
63         $error.missingRef = it.resolve.url(it.baseId, $schema);
64         $error.missingSchema = it.resolve.normalizeId(it.resolve.fullPath($error.missingRef));
65         throw $error;
66       }
67     } else if ($refVal.inline) {
68       var $it = it.util.copy(it);
69       $it.level++;
70       var $nextValid = 'valid' + $it.level;
71       $it.schema = $refVal.schema;
72       $it.schemaPath = '';
73       $it.errSchemaPath = $schema;
74       var $code = it.validate($it).replace(/validate\.schema/g, $refVal.code);
75       out += ' ' + ($code) + ' ';
76       if ($breakOnError) {
77         out += ' if (' + ($nextValid) + ') { ';
78       }
79     } else {
80       $async = $refVal.$async === true;
81       $refCode = $refVal.code;
82     }
83   }
84   if ($refCode) {
85     var $$outStack = $$outStack || [];
86     $$outStack.push(out);
87     out = '';
88     if (it.opts.passContext) {
89       out += ' ' + ($refCode) + '.call(this, ';
90     } else {
91       out += ' ' + ($refCode) + '( ';
92     }
93     out += ' ' + ($data) + ', (dataPath || \'\')';
94     if (it.errorPath != '""') {
95       out += ' + ' + (it.errorPath);
96     }
97     var $parentData = $dataLvl ? 'data' + (($dataLvl - 1) || '') : 'parentData',
98       $parentDataProperty = $dataLvl ? it.dataPathArr[$dataLvl] : 'parentDataProperty';
99     out += ' , ' + ($parentData) + ' , ' + ($parentDataProperty) + ', rootData)  ';
100     var __callValidate = out;
101     out = $$outStack.pop();
102     if ($async) {
103       if (!it.async) throw new Error('async schema referenced by sync schema');
104       out += ' try { ';
105       if ($breakOnError) {
106         out += 'var ' + ($valid) + ' =';
107       }
108       out += ' ' + (it.yieldAwait) + ' ' + (__callValidate) + '; } catch (e) { if (!(e instanceof ValidationError)) throw e; if (vErrors === null) vErrors = e.errors; else vErrors = vErrors.concat(e.errors); errors = vErrors.length; } ';
109       if ($breakOnError) {
110         out += ' if (' + ($valid) + ') { ';
111       }
112     } else {
113       out += ' if (!' + (__callValidate) + ') { if (vErrors === null) vErrors = ' + ($refCode) + '.errors; else vErrors = vErrors.concat(' + ($refCode) + '.errors); errors = vErrors.length; } ';
114       if ($breakOnError) {
115         out += ' else { ';
116       }
117     }
118   }
119   return out;
120 }