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