Path: blob/master/node_modules/ajv/lib/dotjs/enum.js
2593 views
'use strict';1module.exports = function generate_enum(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 $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}19var $i = 'i' + $lvl,20$vSchema = 'schema' + $lvl;21if (!$isData) {22out += ' var ' + ($vSchema) + ' = validate.schema' + ($schemaPath) + ';';23}24out += 'var ' + ($valid) + ';';25if ($isData) {26out += ' if (schema' + ($lvl) + ' === undefined) ' + ($valid) + ' = true; else if (!Array.isArray(schema' + ($lvl) + ')) ' + ($valid) + ' = false; else {';27}28out += '' + ($valid) + ' = false;for (var ' + ($i) + '=0; ' + ($i) + '<' + ($vSchema) + '.length; ' + ($i) + '++) if (equal(' + ($data) + ', ' + ($vSchema) + '[' + ($i) + '])) { ' + ($valid) + ' = true; break; }';29if ($isData) {30out += ' } ';31}32out += ' if (!' + ($valid) + ') { ';33var $$outStack = $$outStack || [];34$$outStack.push(out);35out = ''; /* istanbul ignore else */36if (it.createErrors !== false) {37out += ' { keyword: \'' + ('enum') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { allowedValues: schema' + ($lvl) + ' } ';38if (it.opts.messages !== false) {39out += ' , message: \'should be equal to one of the allowed values\' ';40}41if (it.opts.verbose) {42out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';43}44out += ' } ';45} else {46out += ' {} ';47}48var __err = out;49out = $$outStack.pop();50if (!it.compositeRule && $breakOnError) {51/* istanbul ignore if */52if (it.async) {53out += ' throw new ValidationError([' + (__err) + ']); ';54} else {55out += ' validate.errors = [' + (__err) + ']; return false; ';56}57} else {58out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';59}60out += ' }';61if ($breakOnError) {62out += ' else { ';63}64return out;65}666768