2 module.exports = function generate__formatLimit(it, $keyword) {
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;
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; ';
18 var $schemaFormat = it.schema.format,
19 $isDataFormat = it.opts.v5 && $schemaFormat.$data,
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;';
27 var $format = it.formats[$schemaFormat];
28 if (!($format && $format.compare)) {
29 out += ' ' + ($valid) + ' = true; ';
32 var $compare = 'formats' + it.util.getProperty($schemaFormat) + '.compare';
34 var $isMax = $keyword == 'formatMaximum',
35 $exclusiveKeyword = 'formatExclusive' + ($isMax ? 'Maximum' : 'Minimum'),
36 $schemaExcl = it.schema[$exclusiveKeyword],
37 $isDataExcl = it.opts.v5 && $schemaExcl && $schemaExcl.$data,
38 $op = $isMax ? '<' : '>',
39 $result = 'result' + $lvl;
40 var $isData = it.opts.v5 && $schema && $schema.$data,
43 out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
44 $schemaValue = 'schema' + $lvl;
46 $schemaValue = $schema;
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 || [];
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\' ';
65 if (it.opts.verbose) {
66 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
73 out = $$outStack.pop();
74 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
76 out += ' throw new ValidationError([' + (__err) + ']); ';
78 out += ' validate.errors = [' + (__err) + ']; return false; ';
81 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
85 $closingBraces += '}';
89 out += ' if (' + ($schemaValue) + ' === undefined) ' + ($valid) + ' = true; else if (typeof ' + ($schemaValue) + ' != \'string\') ' + ($valid) + ' = false; else { ';
90 $closingBraces += '}';
93 out += ' if (!' + ($compare) + ') ' + ($valid) + ' = true; else { ';
94 $closingBraces += '}';
96 out += ' var ' + ($result) + ' = ' + ($compare) + '(' + ($data) + ', ';
98 out += '' + ($schemaValue);
100 out += '' + (it.util.toQuotedString($schema));
102 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 var $exclusive = $schemaExcl === true,
106 if (!$exclusive) $opStr += '=';
107 var $opExpr = '\'' + $opStr + '\'';
109 out += ' if (' + ($schemaValue) + ' === undefined) ' + ($valid) + ' = true; else if (typeof ' + ($schemaValue) + ' != \'string\') ' + ($valid) + ' = false; else { ';
110 $closingBraces += '}';
113 out += ' if (!' + ($compare) + ') ' + ($valid) + ' = true; else { ';
114 $closingBraces += '}';
116 out += ' var ' + ($result) + ' = ' + ($compare) + '(' + ($data) + ', ';
118 out += '' + ($schemaValue);
120 out += '' + (it.util.toQuotedString($schema));
122 out += ' ); if (' + ($result) + ' === undefined) ' + ($valid) + ' = false; if (' + ($valid) + ' === undefined) ' + ($valid) + ' = ' + ($result) + ' ' + ($op);
128 out += '' + ($closingBraces) + 'if (!' + ($valid) + ') { ';
129 var $errorKeyword = $keyword;
130 var $$outStack = $$outStack || [];
131 $$outStack.push(out);
132 out = ''; /* istanbul ignore else */
133 if (it.createErrors !== false) {
134 out += ' { keyword: \'' + ($errorKeyword || '_formatLimit') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { comparison: ' + ($opExpr) + ', limit: ';
136 out += '' + ($schemaValue);
138 out += '' + (it.util.toQuotedString($schema));
140 out += ' , exclusive: ' + ($exclusive) + ' } ';
141 if (it.opts.messages !== false) {
142 out += ' , message: \'should be ' + ($opStr) + ' "';
144 out += '\' + ' + ($schemaValue) + ' + \'';
146 out += '' + (it.util.escapeQuotes($schema));
150 if (it.opts.verbose) {
151 out += ' , schema: ';
153 out += 'validate.schema' + ($schemaPath);
155 out += '' + (it.util.toQuotedString($schema));
157 out += ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
164 out = $$outStack.pop();
165 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
167 out += ' throw new ValidationError([' + (__err) + ']); ';
169 out += ' validate.errors = [' + (__err) + ']; return false; ';
172 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';