2 module.exports = function generate__limit(it, $keyword, $ruleType) {
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 $isData = it.opts.$data && $schema && $schema.$data,
15 out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
16 $schemaValue = 'schema' + $lvl;
18 $schemaValue = $schema;
20 var $isMax = $keyword == 'maximum',
21 $exclusiveKeyword = $isMax ? 'exclusiveMaximum' : 'exclusiveMinimum',
22 $schemaExcl = it.schema[$exclusiveKeyword],
23 $isDataExcl = it.opts.$data && $schemaExcl && $schemaExcl.$data,
24 $op = $isMax ? '<' : '>',
25 $notOp = $isMax ? '>' : '<',
26 $errorKeyword = undefined;
27 if (!($isData || typeof $schema == 'number' || $schema === undefined)) {
28 throw new Error($keyword + ' must be number');
30 if (!($isDataExcl || $schemaExcl === undefined || typeof $schemaExcl == 'number' || typeof $schemaExcl == 'boolean')) {
31 throw new Error($exclusiveKeyword + ' must be number or boolean');
34 var $schemaValueExcl = it.util.getData($schemaExcl.$data, $dataLvl, it.dataPathArr),
35 $exclusive = 'exclusive' + $lvl,
36 $exclType = 'exclType' + $lvl,
37 $exclIsNumber = 'exclIsNumber' + $lvl,
38 $opExpr = 'op' + $lvl,
39 $opStr = '\' + ' + $opExpr + ' + \'';
40 out += ' var schemaExcl' + ($lvl) + ' = ' + ($schemaValueExcl) + '; ';
41 $schemaValueExcl = 'schemaExcl' + $lvl;
42 out += ' var ' + ($exclusive) + '; var ' + ($exclType) + ' = typeof ' + ($schemaValueExcl) + '; if (' + ($exclType) + ' != \'boolean\' && ' + ($exclType) + ' != \'undefined\' && ' + ($exclType) + ' != \'number\') { ';
43 var $errorKeyword = $exclusiveKeyword;
44 var $$outStack = $$outStack || [];
46 out = ''; /* istanbul ignore else */
47 if (it.createErrors !== false) {
48 out += ' { keyword: \'' + ($errorKeyword || '_exclusiveLimit') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: {} ';
49 if (it.opts.messages !== false) {
50 out += ' , message: \'' + ($exclusiveKeyword) + ' should be boolean\' ';
52 if (it.opts.verbose) {
53 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
60 out = $$outStack.pop();
61 if (!it.compositeRule && $breakOnError) {
62 /* istanbul ignore if */
64 out += ' throw new ValidationError([' + (__err) + ']); ';
66 out += ' validate.errors = [' + (__err) + ']; return false; ';
69 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
71 out += ' } else if ( ';
73 out += ' (' + ($schemaValue) + ' !== undefined && typeof ' + ($schemaValue) + ' != \'number\') || ';
75 out += ' ' + ($exclType) + ' == \'number\' ? ( (' + ($exclusive) + ' = ' + ($schemaValue) + ' === undefined || ' + ($schemaValueExcl) + ' ' + ($op) + '= ' + ($schemaValue) + ') ? ' + ($data) + ' ' + ($notOp) + '= ' + ($schemaValueExcl) + ' : ' + ($data) + ' ' + ($notOp) + ' ' + ($schemaValue) + ' ) : ( (' + ($exclusive) + ' = ' + ($schemaValueExcl) + ' === true) ? ' + ($data) + ' ' + ($notOp) + '= ' + ($schemaValue) + ' : ' + ($data) + ' ' + ($notOp) + ' ' + ($schemaValue) + ' ) || ' + ($data) + ' !== ' + ($data) + ') { var op' + ($lvl) + ' = ' + ($exclusive) + ' ? \'' + ($op) + '\' : \'' + ($op) + '=\'; ';
76 if ($schema === undefined) {
77 $errorKeyword = $exclusiveKeyword;
78 $errSchemaPath = it.errSchemaPath + '/' + $exclusiveKeyword;
79 $schemaValue = $schemaValueExcl;
80 $isData = $isDataExcl;
83 var $exclIsNumber = typeof $schemaExcl == 'number',
85 if ($exclIsNumber && $isData) {
86 var $opExpr = '\'' + $opStr + '\'';
89 out += ' (' + ($schemaValue) + ' !== undefined && typeof ' + ($schemaValue) + ' != \'number\') || ';
91 out += ' ( ' + ($schemaValue) + ' === undefined || ' + ($schemaExcl) + ' ' + ($op) + '= ' + ($schemaValue) + ' ? ' + ($data) + ' ' + ($notOp) + '= ' + ($schemaExcl) + ' : ' + ($data) + ' ' + ($notOp) + ' ' + ($schemaValue) + ' ) || ' + ($data) + ' !== ' + ($data) + ') { ';
93 if ($exclIsNumber && $schema === undefined) {
95 $errorKeyword = $exclusiveKeyword;
96 $errSchemaPath = it.errSchemaPath + '/' + $exclusiveKeyword;
97 $schemaValue = $schemaExcl;
100 if ($exclIsNumber) $schemaValue = Math[$isMax ? 'min' : 'max']($schemaExcl, $schema);
101 if ($schemaExcl === ($exclIsNumber ? $schemaValue : true)) {
103 $errorKeyword = $exclusiveKeyword;
104 $errSchemaPath = it.errSchemaPath + '/' + $exclusiveKeyword;
111 var $opExpr = '\'' + $opStr + '\'';
114 out += ' (' + ($schemaValue) + ' !== undefined && typeof ' + ($schemaValue) + ' != \'number\') || ';
116 out += ' ' + ($data) + ' ' + ($notOp) + ' ' + ($schemaValue) + ' || ' + ($data) + ' !== ' + ($data) + ') { ';
119 $errorKeyword = $errorKeyword || $keyword;
120 var $$outStack = $$outStack || [];
121 $$outStack.push(out);
122 out = ''; /* istanbul ignore else */
123 if (it.createErrors !== false) {
124 out += ' { keyword: \'' + ($errorKeyword || '_limit') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { comparison: ' + ($opExpr) + ', limit: ' + ($schemaValue) + ', exclusive: ' + ($exclusive) + ' } ';
125 if (it.opts.messages !== false) {
126 out += ' , message: \'should be ' + ($opStr) + ' ';
128 out += '\' + ' + ($schemaValue);
130 out += '' + ($schemaValue) + '\'';
133 if (it.opts.verbose) {
134 out += ' , schema: ';
136 out += 'validate.schema' + ($schemaPath);
138 out += '' + ($schema);
140 out += ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
147 out = $$outStack.pop();
148 if (!it.compositeRule && $breakOnError) {
149 /* istanbul ignore if */
151 out += ' throw new ValidationError([' + (__err) + ']); ';
153 out += ' validate.errors = [' + (__err) + ']; return false; ';
156 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';