Path: blob/master/node_modules/ajv/lib/dotjs/propertyNames.js
1126 views
'use strict';1module.exports = function generate_propertyNames(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 $errs = 'errs__' + $lvl;11var $it = it.util.copy(it);12var $closingBraces = '';13$it.level++;14var $nextValid = 'valid' + $it.level;15out += 'var ' + ($errs) + ' = errors;';16if ((it.opts.strictKeywords ? (typeof $schema == 'object' && Object.keys($schema).length > 0) || $schema === false : it.util.schemaHasRules($schema, it.RULES.all))) {17$it.schema = $schema;18$it.schemaPath = $schemaPath;19$it.errSchemaPath = $errSchemaPath;20var $key = 'key' + $lvl,21$idx = 'idx' + $lvl,22$i = 'i' + $lvl,23$invalidName = '\' + ' + $key + ' + \'',24$dataNxt = $it.dataLevel = it.dataLevel + 1,25$nextData = 'data' + $dataNxt,26$dataProperties = 'dataProperties' + $lvl,27$ownProperties = it.opts.ownProperties,28$currentBaseId = it.baseId;29if ($ownProperties) {30out += ' var ' + ($dataProperties) + ' = undefined; ';31}32if ($ownProperties) {33out += ' ' + ($dataProperties) + ' = ' + ($dataProperties) + ' || Object.keys(' + ($data) + '); for (var ' + ($idx) + '=0; ' + ($idx) + '<' + ($dataProperties) + '.length; ' + ($idx) + '++) { var ' + ($key) + ' = ' + ($dataProperties) + '[' + ($idx) + ']; ';34} else {35out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';36}37out += ' var startErrs' + ($lvl) + ' = errors; ';38var $passData = $key;39var $wasComposite = it.compositeRule;40it.compositeRule = $it.compositeRule = true;41var $code = it.validate($it);42$it.baseId = $currentBaseId;43if (it.util.varOccurences($code, $nextData) < 2) {44out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';45} else {46out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';47}48it.compositeRule = $it.compositeRule = $wasComposite;49out += ' if (!' + ($nextValid) + ') { for (var ' + ($i) + '=startErrs' + ($lvl) + '; ' + ($i) + '<errors; ' + ($i) + '++) { vErrors[' + ($i) + '].propertyName = ' + ($key) + '; } var err = '; /* istanbul ignore else */50if (it.createErrors !== false) {51out += ' { keyword: \'' + ('propertyNames') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { propertyName: \'' + ($invalidName) + '\' } ';52if (it.opts.messages !== false) {53out += ' , message: \'property name \\\'' + ($invalidName) + '\\\' is invalid\' ';54}55if (it.opts.verbose) {56out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';57}58out += ' } ';59} else {60out += ' {} ';61}62out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';63if (!it.compositeRule && $breakOnError) {64/* istanbul ignore if */65if (it.async) {66out += ' throw new ValidationError(vErrors); ';67} else {68out += ' validate.errors = vErrors; return false; ';69}70}71if ($breakOnError) {72out += ' break; ';73}74out += ' } }';75}76if ($breakOnError) {77out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';78}79return out;80}818283