Path: blob/master/node_modules/ajv/lib/dotjs/items.js
2593 views
'use strict';1module.exports = function generate_items(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 $data = 'data' + ($dataLvl || '');10var $valid = 'valid' + $lvl;11var $errs = 'errs__' + $lvl;12var $it = it.util.copy(it);13var $closingBraces = '';14$it.level++;15var $nextValid = 'valid' + $it.level;16var $idx = 'i' + $lvl,17$dataNxt = $it.dataLevel = it.dataLevel + 1,18$nextData = 'data' + $dataNxt,19$currentBaseId = it.baseId;20out += 'var ' + ($errs) + ' = errors;var ' + ($valid) + ';';21if (Array.isArray($schema)) {22var $additionalItems = it.schema.additionalItems;23if ($additionalItems === false) {24out += ' ' + ($valid) + ' = ' + ($data) + '.length <= ' + ($schema.length) + '; ';25var $currErrSchemaPath = $errSchemaPath;26$errSchemaPath = it.errSchemaPath + '/additionalItems';27out += ' if (!' + ($valid) + ') { ';28var $$outStack = $$outStack || [];29$$outStack.push(out);30out = ''; /* istanbul ignore else */31if (it.createErrors !== false) {32out += ' { keyword: \'' + ('additionalItems') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { limit: ' + ($schema.length) + ' } ';33if (it.opts.messages !== false) {34out += ' , message: \'should NOT have more than ' + ($schema.length) + ' items\' ';35}36if (it.opts.verbose) {37out += ' , schema: false , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';38}39out += ' } ';40} else {41out += ' {} ';42}43var __err = out;44out = $$outStack.pop();45if (!it.compositeRule && $breakOnError) {46/* istanbul ignore if */47if (it.async) {48out += ' throw new ValidationError([' + (__err) + ']); ';49} else {50out += ' validate.errors = [' + (__err) + ']; return false; ';51}52} else {53out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';54}55out += ' } ';56$errSchemaPath = $currErrSchemaPath;57if ($breakOnError) {58$closingBraces += '}';59out += ' else { ';60}61}62var arr1 = $schema;63if (arr1) {64var $sch, $i = -1,65l1 = arr1.length - 1;66while ($i < l1) {67$sch = arr1[$i += 1];68if ((it.opts.strictKeywords ? (typeof $sch == 'object' && Object.keys($sch).length > 0) || $sch === false : it.util.schemaHasRules($sch, it.RULES.all))) {69out += ' ' + ($nextValid) + ' = true; if (' + ($data) + '.length > ' + ($i) + ') { ';70var $passData = $data + '[' + $i + ']';71$it.schema = $sch;72$it.schemaPath = $schemaPath + '[' + $i + ']';73$it.errSchemaPath = $errSchemaPath + '/' + $i;74$it.errorPath = it.util.getPathExpr(it.errorPath, $i, it.opts.jsonPointers, true);75$it.dataPathArr[$dataNxt] = $i;76var $code = it.validate($it);77$it.baseId = $currentBaseId;78if (it.util.varOccurences($code, $nextData) < 2) {79out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';80} else {81out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';82}83out += ' } ';84if ($breakOnError) {85out += ' if (' + ($nextValid) + ') { ';86$closingBraces += '}';87}88}89}90}91if (typeof $additionalItems == 'object' && (it.opts.strictKeywords ? (typeof $additionalItems == 'object' && Object.keys($additionalItems).length > 0) || $additionalItems === false : it.util.schemaHasRules($additionalItems, it.RULES.all))) {92$it.schema = $additionalItems;93$it.schemaPath = it.schemaPath + '.additionalItems';94$it.errSchemaPath = it.errSchemaPath + '/additionalItems';95out += ' ' + ($nextValid) + ' = true; if (' + ($data) + '.length > ' + ($schema.length) + ') { for (var ' + ($idx) + ' = ' + ($schema.length) + '; ' + ($idx) + ' < ' + ($data) + '.length; ' + ($idx) + '++) { ';96$it.errorPath = it.util.getPathExpr(it.errorPath, $idx, it.opts.jsonPointers, true);97var $passData = $data + '[' + $idx + ']';98$it.dataPathArr[$dataNxt] = $idx;99var $code = it.validate($it);100$it.baseId = $currentBaseId;101if (it.util.varOccurences($code, $nextData) < 2) {102out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';103} else {104out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';105}106if ($breakOnError) {107out += ' if (!' + ($nextValid) + ') break; ';108}109out += ' } } ';110if ($breakOnError) {111out += ' if (' + ($nextValid) + ') { ';112$closingBraces += '}';113}114}115} else if ((it.opts.strictKeywords ? (typeof $schema == 'object' && Object.keys($schema).length > 0) || $schema === false : it.util.schemaHasRules($schema, it.RULES.all))) {116$it.schema = $schema;117$it.schemaPath = $schemaPath;118$it.errSchemaPath = $errSchemaPath;119out += ' for (var ' + ($idx) + ' = ' + (0) + '; ' + ($idx) + ' < ' + ($data) + '.length; ' + ($idx) + '++) { ';120$it.errorPath = it.util.getPathExpr(it.errorPath, $idx, it.opts.jsonPointers, true);121var $passData = $data + '[' + $idx + ']';122$it.dataPathArr[$dataNxt] = $idx;123var $code = it.validate($it);124$it.baseId = $currentBaseId;125if (it.util.varOccurences($code, $nextData) < 2) {126out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';127} else {128out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';129}130if ($breakOnError) {131out += ' if (!' + ($nextValid) + ') break; ';132}133out += ' }';134}135if ($breakOnError) {136out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';137}138return out;139}140141142