Initial commit
[yaffs-website] / node_modules / ajv / lib / dotjs / dependencies.js
1 'use strict';
2 module.exports = function generate_dependencies(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 $errs = 'errs__' + $lvl;
13   var $it = it.util.copy(it);
14   var $closingBraces = '';
15   $it.level++;
16   var $nextValid = 'valid' + $it.level;
17   var $schemaDeps = {},
18     $propertyDeps = {};
19   for ($property in $schema) {
20     var $sch = $schema[$property];
21     var $deps = Array.isArray($sch) ? $propertyDeps : $schemaDeps;
22     $deps[$property] = $sch;
23   }
24   out += 'var ' + ($errs) + ' = errors;';
25   var $currentErrorPath = it.errorPath;
26   out += 'var missing' + ($lvl) + ';';
27   for (var $property in $propertyDeps) {
28     $deps = $propertyDeps[$property];
29     out += ' if (' + ($data) + (it.util.getProperty($property)) + ' !== undefined ';
30     if ($breakOnError) {
31       out += ' && ( ';
32       var arr1 = $deps;
33       if (arr1) {
34         var _$property, $i = -1,
35           l1 = arr1.length - 1;
36         while ($i < l1) {
37           _$property = arr1[$i += 1];
38           if ($i) {
39             out += ' || ';
40           }
41           var $prop = it.util.getProperty(_$property);
42           out += ' ( ' + ($data) + ($prop) + ' === undefined && (missing' + ($lvl) + ' = ' + (it.util.toQuotedString(it.opts.jsonPointers ? _$property : $prop)) + ') ) ';
43         }
44       }
45       out += ')) {  ';
46       var $propertyPath = 'missing' + $lvl,
47         $missingProperty = '\' + ' + $propertyPath + ' + \'';
48       if (it.opts._errorDataPathProperty) {
49         it.errorPath = it.opts.jsonPointers ? it.util.getPathExpr($currentErrorPath, $propertyPath, true) : $currentErrorPath + ' + ' + $propertyPath;
50       }
51       var $$outStack = $$outStack || [];
52       $$outStack.push(out);
53       out = ''; /* istanbul ignore else */
54       if (it.createErrors !== false) {
55         out += ' { keyword: \'' + ($errorKeyword || 'dependencies') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { property: \'' + (it.util.escapeQuotes($property)) + '\', missingProperty: \'' + ($missingProperty) + '\', depsCount: ' + ($deps.length) + ', deps: \'' + (it.util.escapeQuotes($deps.length == 1 ? $deps[0] : $deps.join(", "))) + '\' } ';
56         if (it.opts.messages !== false) {
57           out += ' , message: \'should have ';
58           if ($deps.length == 1) {
59             out += 'property ' + (it.util.escapeQuotes($deps[0]));
60           } else {
61             out += 'properties ' + (it.util.escapeQuotes($deps.join(", ")));
62           }
63           out += ' when property ' + (it.util.escapeQuotes($property)) + ' is present\' ';
64         }
65         if (it.opts.verbose) {
66           out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
67         }
68         out += ' } ';
69       } else {
70         out += ' {} ';
71       }
72       var __err = out;
73       out = $$outStack.pop();
74       if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
75         if (it.async) {
76           out += ' throw new ValidationError([' + (__err) + ']); ';
77         } else {
78           out += ' validate.errors = [' + (__err) + ']; return false; ';
79         }
80       } else {
81         out += ' var err = ' + (__err) + ';  if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
82       }
83     } else {
84       out += ' ) { ';
85       var arr2 = $deps;
86       if (arr2) {
87         var $reqProperty, i2 = -1,
88           l2 = arr2.length - 1;
89         while (i2 < l2) {
90           $reqProperty = arr2[i2 += 1];
91           var $prop = it.util.getProperty($reqProperty),
92             $missingProperty = it.util.escapeQuotes($reqProperty);
93           if (it.opts._errorDataPathProperty) {
94             it.errorPath = it.util.getPath($currentErrorPath, $reqProperty, it.opts.jsonPointers);
95           }
96           out += ' if (' + ($data) + ($prop) + ' === undefined) {  var err =   '; /* istanbul ignore else */
97           if (it.createErrors !== false) {
98             out += ' { keyword: \'' + ($errorKeyword || 'dependencies') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { property: \'' + (it.util.escapeQuotes($property)) + '\', missingProperty: \'' + ($missingProperty) + '\', depsCount: ' + ($deps.length) + ', deps: \'' + (it.util.escapeQuotes($deps.length == 1 ? $deps[0] : $deps.join(", "))) + '\' } ';
99             if (it.opts.messages !== false) {
100               out += ' , message: \'should have ';
101               if ($deps.length == 1) {
102                 out += 'property ' + (it.util.escapeQuotes($deps[0]));
103               } else {
104                 out += 'properties ' + (it.util.escapeQuotes($deps.join(", ")));
105               }
106               out += ' when property ' + (it.util.escapeQuotes($property)) + ' is present\' ';
107             }
108             if (it.opts.verbose) {
109               out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
110             }
111             out += ' } ';
112           } else {
113             out += ' {} ';
114           }
115           out += ';  if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } ';
116         }
117       }
118     }
119     out += ' }   ';
120     if ($breakOnError) {
121       $closingBraces += '}';
122       out += ' else { ';
123     }
124   }
125   it.errorPath = $currentErrorPath;
126   var $currentBaseId = $it.baseId;
127   for (var $property in $schemaDeps) {
128     var $sch = $schemaDeps[$property];
129     if (it.util.schemaHasRules($sch, it.RULES.all)) {
130       out += ' ' + ($nextValid) + ' = true; if (' + ($data) + (it.util.getProperty($property)) + ' !== undefined) { ';
131       $it.schema = $sch;
132       $it.schemaPath = $schemaPath + it.util.getProperty($property);
133       $it.errSchemaPath = $errSchemaPath + '/' + it.util.escapeFragment($property);
134       out += '  ' + (it.validate($it)) + ' ';
135       $it.baseId = $currentBaseId;
136       out += ' }  ';
137       if ($breakOnError) {
138         out += ' if (' + ($nextValid) + ') { ';
139         $closingBraces += '}';
140       }
141     }
142   }
143   if ($breakOnError) {
144     out += '   ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';
145   }
146   out = it.util.cleanUpCode(out);
147   return out;
148 }