Path: blob/master/node_modules/ajv/lib/dotjs/_limitLength.js
1126 views
'use strict';1module.exports = function generate__limitLength(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}19if (!($isData || typeof $schema == 'number')) {20throw new Error($keyword + ' must be number');21}22var $op = $keyword == 'maxLength' ? '>' : '<';23out += 'if ( ';24if ($isData) {25out += ' (' + ($schemaValue) + ' !== undefined && typeof ' + ($schemaValue) + ' != \'number\') || ';26}27if (it.opts.unicode === false) {28out += ' ' + ($data) + '.length ';29} else {30out += ' ucs2length(' + ($data) + ') ';31}32out += ' ' + ($op) + ' ' + ($schemaValue) + ') { ';33var $errorKeyword = $keyword;34var $$outStack = $$outStack || [];35$$outStack.push(out);36out = ''; /* istanbul ignore else */37if (it.createErrors !== false) {38out += ' { keyword: \'' + ($errorKeyword || '_limitLength') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { limit: ' + ($schemaValue) + ' } ';39if (it.opts.messages !== false) {40out += ' , message: \'should NOT be ';41if ($keyword == 'maxLength') {42out += 'longer';43} else {44out += 'shorter';45}46out += ' than ';47if ($isData) {48out += '\' + ' + ($schemaValue) + ' + \'';49} else {50out += '' + ($schema);51}52out += ' characters\' ';53}54if (it.opts.verbose) {55out += ' , schema: ';56if ($isData) {57out += 'validate.schema' + ($schemaPath);58} else {59out += '' + ($schema);60}61out += ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';62}63out += ' } ';64} else {65out += ' {} ';66}67var __err = out;68out = $$outStack.pop();69if (!it.compositeRule && $breakOnError) {70/* istanbul ignore if */71if (it.async) {72out += ' throw new ValidationError([' + (__err) + ']); ';73} else {74out += ' validate.errors = [' + (__err) + ']; return false; ';75}76} else {77out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';78}79out += '} ';80if ($breakOnError) {81out += ' else { ';82}83return out;84}858687