Path: blob/master/node_modules/ajv/lib/dotjs/contains.js
1126 views
'use strict';1module.exports = function generate_contains(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,20$nonEmptySchema = (it.opts.strictKeywords ? (typeof $schema == 'object' && Object.keys($schema).length > 0) || $schema === false : it.util.schemaHasRules($schema, it.RULES.all));21out += 'var ' + ($errs) + ' = errors;var ' + ($valid) + ';';22if ($nonEmptySchema) {23var $wasComposite = it.compositeRule;24it.compositeRule = $it.compositeRule = true;25$it.schema = $schema;26$it.schemaPath = $schemaPath;27$it.errSchemaPath = $errSchemaPath;28out += ' var ' + ($nextValid) + ' = false; for (var ' + ($idx) + ' = 0; ' + ($idx) + ' < ' + ($data) + '.length; ' + ($idx) + '++) { ';29$it.errorPath = it.util.getPathExpr(it.errorPath, $idx, it.opts.jsonPointers, true);30var $passData = $data + '[' + $idx + ']';31$it.dataPathArr[$dataNxt] = $idx;32var $code = it.validate($it);33$it.baseId = $currentBaseId;34if (it.util.varOccurences($code, $nextData) < 2) {35out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';36} else {37out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';38}39out += ' if (' + ($nextValid) + ') break; } ';40it.compositeRule = $it.compositeRule = $wasComposite;41out += ' ' + ($closingBraces) + ' if (!' + ($nextValid) + ') {';42} else {43out += ' if (' + ($data) + '.length == 0) {';44}45var $$outStack = $$outStack || [];46$$outStack.push(out);47out = ''; /* istanbul ignore else */48if (it.createErrors !== false) {49out += ' { keyword: \'' + ('contains') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: {} ';50if (it.opts.messages !== false) {51out += ' , message: \'should contain a valid item\' ';52}53if (it.opts.verbose) {54out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';55}56out += ' } ';57} else {58out += ' {} ';59}60var __err = out;61out = $$outStack.pop();62if (!it.compositeRule && $breakOnError) {63/* istanbul ignore if */64if (it.async) {65out += ' throw new ValidationError([' + (__err) + ']); ';66} else {67out += ' validate.errors = [' + (__err) + ']; return false; ';68}69} else {70out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';71}72out += ' } else { ';73if ($nonEmptySchema) {74out += ' errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; } ';75}76if (it.opts.allErrors) {77out += ' } ';78}79return out;80}818283