[d565449] | 1 | 'use strict';
|
---|
| 2 | module.exports = function generate_oneOf(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 $currentBaseId = $it.baseId,
|
---|
| 18 | $prevValid = 'prevValid' + $lvl,
|
---|
| 19 | $passingSchemas = 'passingSchemas' + $lvl;
|
---|
| 20 | out += 'var ' + ($errs) + ' = errors , ' + ($prevValid) + ' = false , ' + ($valid) + ' = false , ' + ($passingSchemas) + ' = null; ';
|
---|
| 21 | var $wasComposite = it.compositeRule;
|
---|
| 22 | it.compositeRule = $it.compositeRule = true;
|
---|
| 23 | var arr1 = $schema;
|
---|
| 24 | if (arr1) {
|
---|
| 25 | var $sch, $i = -1,
|
---|
| 26 | l1 = arr1.length - 1;
|
---|
| 27 | while ($i < l1) {
|
---|
| 28 | $sch = arr1[$i += 1];
|
---|
| 29 | if ((it.opts.strictKeywords ? (typeof $sch == 'object' && Object.keys($sch).length > 0) || $sch === false : it.util.schemaHasRules($sch, it.RULES.all))) {
|
---|
| 30 | $it.schema = $sch;
|
---|
| 31 | $it.schemaPath = $schemaPath + '[' + $i + ']';
|
---|
| 32 | $it.errSchemaPath = $errSchemaPath + '/' + $i;
|
---|
| 33 | out += ' ' + (it.validate($it)) + ' ';
|
---|
| 34 | $it.baseId = $currentBaseId;
|
---|
| 35 | } else {
|
---|
| 36 | out += ' var ' + ($nextValid) + ' = true; ';
|
---|
| 37 | }
|
---|
| 38 | if ($i) {
|
---|
| 39 | out += ' if (' + ($nextValid) + ' && ' + ($prevValid) + ') { ' + ($valid) + ' = false; ' + ($passingSchemas) + ' = [' + ($passingSchemas) + ', ' + ($i) + ']; } else { ';
|
---|
| 40 | $closingBraces += '}';
|
---|
| 41 | }
|
---|
| 42 | out += ' if (' + ($nextValid) + ') { ' + ($valid) + ' = ' + ($prevValid) + ' = true; ' + ($passingSchemas) + ' = ' + ($i) + '; }';
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | it.compositeRule = $it.compositeRule = $wasComposite;
|
---|
| 46 | out += '' + ($closingBraces) + 'if (!' + ($valid) + ') { var err = '; /* istanbul ignore else */
|
---|
| 47 | if (it.createErrors !== false) {
|
---|
| 48 | out += ' { keyword: \'' + ('oneOf') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { passingSchemas: ' + ($passingSchemas) + ' } ';
|
---|
| 49 | if (it.opts.messages !== false) {
|
---|
| 50 | out += ' , message: \'should match exactly one schema in oneOf\' ';
|
---|
| 51 | }
|
---|
| 52 | if (it.opts.verbose) {
|
---|
| 53 | out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
|
---|
| 54 | }
|
---|
| 55 | out += ' } ';
|
---|
| 56 | } else {
|
---|
| 57 | out += ' {} ';
|
---|
| 58 | }
|
---|
| 59 | out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
|
---|
| 60 | if (!it.compositeRule && $breakOnError) {
|
---|
| 61 | /* istanbul ignore if */
|
---|
| 62 | if (it.async) {
|
---|
| 63 | out += ' throw new ValidationError(vErrors); ';
|
---|
| 64 | } else {
|
---|
| 65 | out += ' validate.errors = vErrors; return false; ';
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | out += '} else { errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; }';
|
---|
| 69 | if (it.opts.allErrors) {
|
---|
| 70 | out += ' } ';
|
---|
| 71 | }
|
---|
| 72 | return out;
|
---|
| 73 | }
|
---|