UNPKG

7.35 kBJavaScriptView Raw
1'use strict';
2module.exports = function generate__formatLimit(it, $keyword, $ruleType) {
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 out += 'var ' + ($valid) + ' = undefined;';
14 if (it.opts.format === false) {
15 out += ' ' + ($valid) + ' = true; ';
16 return out;
17 }
18 var $schemaFormat = it.schema.format,
19 $isDataFormat = it.opts.$data && $schemaFormat.$data,
20 $closingBraces = '';
21 if ($isDataFormat) {
22 var $schemaValueFormat = it.util.getData($schemaFormat.$data, $dataLvl, it.dataPathArr),
23 $format = 'format' + $lvl,
24 $compare = 'compare' + $lvl;
25 out += ' var ' + ($format) + ' = formats[' + ($schemaValueFormat) + '] , ' + ($compare) + ' = ' + ($format) + ' && ' + ($format) + '.compare;';
26 } else {
27 var $format = it.formats[$schemaFormat];
28 if (!($format && $format.compare)) {
29 out += ' ' + ($valid) + ' = true; ';
30 return out;
31 }
32 var $compare = 'formats' + it.util.getProperty($schemaFormat) + '.compare';
33 }
34 var $isMax = $keyword == 'formatMaximum',
35 $exclusiveKeyword = 'formatExclusive' + ($isMax ? 'Maximum' : 'Minimum'),
36 $schemaExcl = it.schema[$exclusiveKeyword],
37 $isDataExcl = it.opts.$data && $schemaExcl && $schemaExcl.$data,
38 $op = $isMax ? '<' : '>',
39 $result = 'result' + $lvl;
40 var $isData = it.opts.$data && $schema && $schema.$data,
41 $schemaValue;
42 if ($isData) {
43 out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
44 $schemaValue = 'schema' + $lvl;
45 } else {
46 $schemaValue = $schema;
47 }
48 if ($isDataExcl) {
49 var $schemaValueExcl = it.util.getData($schemaExcl.$data, $dataLvl, it.dataPathArr),
50 $exclusive = 'exclusive' + $lvl,
51 $opExpr = 'op' + $lvl,
52 $opStr = '\' + ' + $opExpr + ' + \'';
53 out += ' var schemaExcl' + ($lvl) + ' = ' + ($schemaValueExcl) + '; ';
54 $schemaValueExcl = 'schemaExcl' + $lvl;
55 out += ' if (typeof ' + ($schemaValueExcl) + ' != \'boolean\' && ' + ($schemaValueExcl) + ' !== undefined) { ' + ($valid) + ' = false; ';
56 var $errorKeyword = $exclusiveKeyword;
57 var $$outStack = $$outStack || [];
58 $$outStack.push(out);
59 out = ''; /* istanbul ignore else */
60 if (it.createErrors !== false) {
61 out += ' { keyword: \'' + ($errorKeyword || '_formatExclusiveLimit') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: {} ';
62 if (it.opts.messages !== false) {
63 out += ' , message: \'' + ($exclusiveKeyword) + ' should be boolean\' ';
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) {
75 /* istanbul ignore if */
76 if (it.async) {
77 out += ' throw new ValidationError([' + (__err) + ']); ';
78 } else {
79 out += ' validate.errors = [' + (__err) + ']; return false; ';
80 }
81 } else {
82 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
83 }
84 out += ' } ';
85 if ($breakOnError) {
86 $closingBraces += '}';
87 out += ' else { ';
88 }
89 if ($isData) {
90 out += ' if (' + ($schemaValue) + ' === undefined) ' + ($valid) + ' = true; else if (typeof ' + ($schemaValue) + ' != \'string\') ' + ($valid) + ' = false; else { ';
91 $closingBraces += '}';
92 }
93 if ($isDataFormat) {
94 out += ' if (!' + ($compare) + ') ' + ($valid) + ' = true; else { ';
95 $closingBraces += '}';
96 }
97 out += ' var ' + ($result) + ' = ' + ($compare) + '(' + ($data) + ', ';
98 if ($isData) {
99 out += '' + ($schemaValue);
100 } else {
101 out += '' + (it.util.toQuotedString($schema));
102 }
103 out += ' ); if (' + ($result) + ' === undefined) ' + ($valid) + ' = false; var ' + ($exclusive) + ' = ' + ($schemaValueExcl) + ' === true; if (' + ($valid) + ' === undefined) { ' + ($valid) + ' = ' + ($exclusive) + ' ? ' + ($result) + ' ' + ($op) + ' 0 : ' + ($result) + ' ' + ($op) + '= 0; } if (!' + ($valid) + ') var op' + ($lvl) + ' = ' + ($exclusive) + ' ? \'' + ($op) + '\' : \'' + ($op) + '=\';';
104 } else {
105 var $exclusive = $schemaExcl === true,
106 $opStr = $op;
107 if (!$exclusive) $opStr += '=';
108 var $opExpr = '\'' + $opStr + '\'';
109 if ($isData) {
110 out += ' if (' + ($schemaValue) + ' === undefined) ' + ($valid) + ' = true; else if (typeof ' + ($schemaValue) + ' != \'string\') ' + ($valid) + ' = false; else { ';
111 $closingBraces += '}';
112 }
113 if ($isDataFormat) {
114 out += ' if (!' + ($compare) + ') ' + ($valid) + ' = true; else { ';
115 $closingBraces += '}';
116 }
117 out += ' var ' + ($result) + ' = ' + ($compare) + '(' + ($data) + ', ';
118 if ($isData) {
119 out += '' + ($schemaValue);
120 } else {
121 out += '' + (it.util.toQuotedString($schema));
122 }
123 out += ' ); if (' + ($result) + ' === undefined) ' + ($valid) + ' = false; if (' + ($valid) + ' === undefined) ' + ($valid) + ' = ' + ($result) + ' ' + ($op);
124 if (!$exclusive) {
125 out += '=';
126 }
127 out += ' 0;';
128 }
129 out += '' + ($closingBraces) + 'if (!' + ($valid) + ') { ';
130 var $errorKeyword = $keyword;
131 var $$outStack = $$outStack || [];
132 $$outStack.push(out);
133 out = ''; /* istanbul ignore else */
134 if (it.createErrors !== false) {
135 out += ' { keyword: \'' + ($errorKeyword || '_formatLimit') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { comparison: ' + ($opExpr) + ', limit: ';
136 if ($isData) {
137 out += '' + ($schemaValue);
138 } else {
139 out += '' + (it.util.toQuotedString($schema));
140 }
141 out += ' , exclusive: ' + ($exclusive) + ' } ';
142 if (it.opts.messages !== false) {
143 out += ' , message: \'should be ' + ($opStr) + ' "';
144 if ($isData) {
145 out += '\' + ' + ($schemaValue) + ' + \'';
146 } else {
147 out += '' + (it.util.escapeQuotes($schema));
148 }
149 out += '"\' ';
150 }
151 if (it.opts.verbose) {
152 out += ' , schema: ';
153 if ($isData) {
154 out += 'validate.schema' + ($schemaPath);
155 } else {
156 out += '' + (it.util.toQuotedString($schema));
157 }
158 out += ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
159 }
160 out += ' } ';
161 } else {
162 out += ' {} ';
163 }
164 var __err = out;
165 out = $$outStack.pop();
166 if (!it.compositeRule && $breakOnError) {
167 /* istanbul ignore if */
168 if (it.async) {
169 out += ' throw new ValidationError([' + (__err) + ']); ';
170 } else {
171 out += ' validate.errors = [' + (__err) + ']; return false; ';
172 }
173 } else {
174 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
175 }
176 out += '}';
177 return out;
178}