Path: blob/master/node_modules/ajv/lib/dotjs/_limit.js
1126 views
'use strict';1module.exports = function generate__limit(it, $keyword, $ruleType) {2var out = ' ';3var $lvl = it.level;4var $dataLvl = it.dataLevel;5var $schema = it.schema[$keyword];6var $schemaPath = it.schemaPath + it.util.getProperty($keyword);7var $errSchemaPath = it.errSchemaPath + '/' + $keyword;8var $breakOnError = !it.opts.allErrors;9var $errorKeyword;10var $data = 'data' + ($dataLvl || '');11var $isData = it.opts.$data && $schema && $schema.$data,12$schemaValue;13if ($isData) {14out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';15$schemaValue = 'schema' + $lvl;16} else {17$schemaValue = $schema;18}19var $isMax = $keyword == 'maximum',20$exclusiveKeyword = $isMax ? 'exclusiveMaximum' : 'exclusiveMinimum',21$schemaExcl = it.schema[$exclusiveKeyword],22$isDataExcl = it.opts.$data && $schemaExcl && $schemaExcl.$data,23$op = $isMax ? '<' : '>',24$notOp = $isMax ? '>' : '<',25$errorKeyword = undefined;26if (!($isData || typeof $schema == 'number' || $schema === undefined)) {27throw new Error($keyword + ' must be number');28}29if (!($isDataExcl || $schemaExcl === undefined || typeof $schemaExcl == 'number' || typeof $schemaExcl == 'boolean')) {30throw new Error($exclusiveKeyword + ' must be number or boolean');31}32if ($isDataExcl) {33var $schemaValueExcl = it.util.getData($schemaExcl.$data, $dataLvl, it.dataPathArr),34$exclusive = 'exclusive' + $lvl,35$exclType = 'exclType' + $lvl,36$exclIsNumber = 'exclIsNumber' + $lvl,37$opExpr = 'op' + $lvl,38$opStr = '\' + ' + $opExpr + ' + \'';39out += ' var schemaExcl' + ($lvl) + ' = ' + ($schemaValueExcl) + '; ';40$schemaValueExcl = 'schemaExcl' + $lvl;41out += ' var ' + ($exclusive) + '; var ' + ($exclType) + ' = typeof ' + ($schemaValueExcl) + '; if (' + ($exclType) + ' != \'boolean\' && ' + ($exclType) + ' != \'undefined\' && ' + ($exclType) + ' != \'number\') { ';42var $errorKeyword = $exclusiveKeyword;43var $$outStack = $$outStack || [];44$$outStack.push(out);45out = ''; /* istanbul ignore else */46if (it.createErrors !== false) {47out += ' { keyword: \'' + ($errorKeyword || '_exclusiveLimit') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: {} ';48if (it.opts.messages !== false) {49out += ' , message: \'' + ($exclusiveKeyword) + ' should be boolean\' ';50}51if (it.opts.verbose) {52out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';53}54out += ' } ';55} else {56out += ' {} ';57}58var __err = out;59out = $$outStack.pop();60if (!it.compositeRule && $breakOnError) {61/* istanbul ignore if */62if (it.async) {63out += ' throw new ValidationError([' + (__err) + ']); ';64} else {65out += ' validate.errors = [' + (__err) + ']; return false; ';66}67} else {68out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';69}70out += ' } else if ( ';71if ($isData) {72out += ' (' + ($schemaValue) + ' !== undefined && typeof ' + ($schemaValue) + ' != \'number\') || ';73}74out += ' ' + ($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) + '=\'; ';75if ($schema === undefined) {76$errorKeyword = $exclusiveKeyword;77$errSchemaPath = it.errSchemaPath + '/' + $exclusiveKeyword;78$schemaValue = $schemaValueExcl;79$isData = $isDataExcl;80}81} else {82var $exclIsNumber = typeof $schemaExcl == 'number',83$opStr = $op;84if ($exclIsNumber && $isData) {85var $opExpr = '\'' + $opStr + '\'';86out += ' if ( ';87if ($isData) {88out += ' (' + ($schemaValue) + ' !== undefined && typeof ' + ($schemaValue) + ' != \'number\') || ';89}90out += ' ( ' + ($schemaValue) + ' === undefined || ' + ($schemaExcl) + ' ' + ($op) + '= ' + ($schemaValue) + ' ? ' + ($data) + ' ' + ($notOp) + '= ' + ($schemaExcl) + ' : ' + ($data) + ' ' + ($notOp) + ' ' + ($schemaValue) + ' ) || ' + ($data) + ' !== ' + ($data) + ') { ';91} else {92if ($exclIsNumber && $schema === undefined) {93$exclusive = true;94$errorKeyword = $exclusiveKeyword;95$errSchemaPath = it.errSchemaPath + '/' + $exclusiveKeyword;96$schemaValue = $schemaExcl;97$notOp += '=';98} else {99if ($exclIsNumber) $schemaValue = Math[$isMax ? 'min' : 'max']($schemaExcl, $schema);100if ($schemaExcl === ($exclIsNumber ? $schemaValue : true)) {101$exclusive = true;102$errorKeyword = $exclusiveKeyword;103$errSchemaPath = it.errSchemaPath + '/' + $exclusiveKeyword;104$notOp += '=';105} else {106$exclusive = false;107$opStr += '=';108}109}110var $opExpr = '\'' + $opStr + '\'';111out += ' if ( ';112if ($isData) {113out += ' (' + ($schemaValue) + ' !== undefined && typeof ' + ($schemaValue) + ' != \'number\') || ';114}115out += ' ' + ($data) + ' ' + ($notOp) + ' ' + ($schemaValue) + ' || ' + ($data) + ' !== ' + ($data) + ') { ';116}117}118$errorKeyword = $errorKeyword || $keyword;119var $$outStack = $$outStack || [];120$$outStack.push(out);121out = ''; /* istanbul ignore else */122if (it.createErrors !== false) {123out += ' { keyword: \'' + ($errorKeyword || '_limit') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { comparison: ' + ($opExpr) + ', limit: ' + ($schemaValue) + ', exclusive: ' + ($exclusive) + ' } ';124if (it.opts.messages !== false) {125out += ' , message: \'should be ' + ($opStr) + ' ';126if ($isData) {127out += '\' + ' + ($schemaValue);128} else {129out += '' + ($schemaValue) + '\'';130}131}132if (it.opts.verbose) {133out += ' , schema: ';134if ($isData) {135out += 'validate.schema' + ($schemaPath);136} else {137out += '' + ($schema);138}139out += ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';140}141out += ' } ';142} else {143out += ' {} ';144}145var __err = out;146out = $$outStack.pop();147if (!it.compositeRule && $breakOnError) {148/* istanbul ignore if */149if (it.async) {150out += ' throw new ValidationError([' + (__err) + ']); ';151} else {152out += ' validate.errors = [' + (__err) + ']; return false; ';153}154} else {155out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';156}157out += ' } ';158if ($breakOnError) {159out += ' else { ';160}161return out;162}163164165