Path: blob/master/node_modules/ajv/lib/dotjs/format.js
1126 views
'use strict';1module.exports = function generate_format(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 || '');10if (it.opts.format === false) {11if ($breakOnError) {12out += ' if (true) { ';13}14return out;15}16var $isData = it.opts.$data && $schema && $schema.$data,17$schemaValue;18if ($isData) {19out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';20$schemaValue = 'schema' + $lvl;21} else {22$schemaValue = $schema;23}24var $unknownFormats = it.opts.unknownFormats,25$allowUnknown = Array.isArray($unknownFormats);26if ($isData) {27var $format = 'format' + $lvl,28$isObject = 'isObject' + $lvl,29$formatType = 'formatType' + $lvl;30out += ' var ' + ($format) + ' = formats[' + ($schemaValue) + ']; var ' + ($isObject) + ' = typeof ' + ($format) + ' == \'object\' && !(' + ($format) + ' instanceof RegExp) && ' + ($format) + '.validate; var ' + ($formatType) + ' = ' + ($isObject) + ' && ' + ($format) + '.type || \'string\'; if (' + ($isObject) + ') { ';31if (it.async) {32out += ' var async' + ($lvl) + ' = ' + ($format) + '.async; ';33}34out += ' ' + ($format) + ' = ' + ($format) + '.validate; } if ( ';35if ($isData) {36out += ' (' + ($schemaValue) + ' !== undefined && typeof ' + ($schemaValue) + ' != \'string\') || ';37}38out += ' (';39if ($unknownFormats != 'ignore') {40out += ' (' + ($schemaValue) + ' && !' + ($format) + ' ';41if ($allowUnknown) {42out += ' && self._opts.unknownFormats.indexOf(' + ($schemaValue) + ') == -1 ';43}44out += ') || ';45}46out += ' (' + ($format) + ' && ' + ($formatType) + ' == \'' + ($ruleType) + '\' && !(typeof ' + ($format) + ' == \'function\' ? ';47if (it.async) {48out += ' (async' + ($lvl) + ' ? await ' + ($format) + '(' + ($data) + ') : ' + ($format) + '(' + ($data) + ')) ';49} else {50out += ' ' + ($format) + '(' + ($data) + ') ';51}52out += ' : ' + ($format) + '.test(' + ($data) + '))))) {';53} else {54var $format = it.formats[$schema];55if (!$format) {56if ($unknownFormats == 'ignore') {57it.logger.warn('unknown format "' + $schema + '" ignored in schema at path "' + it.errSchemaPath + '"');58if ($breakOnError) {59out += ' if (true) { ';60}61return out;62} else if ($allowUnknown && $unknownFormats.indexOf($schema) >= 0) {63if ($breakOnError) {64out += ' if (true) { ';65}66return out;67} else {68throw new Error('unknown format "' + $schema + '" is used in schema at path "' + it.errSchemaPath + '"');69}70}71var $isObject = typeof $format == 'object' && !($format instanceof RegExp) && $format.validate;72var $formatType = $isObject && $format.type || 'string';73if ($isObject) {74var $async = $format.async === true;75$format = $format.validate;76}77if ($formatType != $ruleType) {78if ($breakOnError) {79out += ' if (true) { ';80}81return out;82}83if ($async) {84if (!it.async) throw new Error('async format in sync schema');85var $formatRef = 'formats' + it.util.getProperty($schema) + '.validate';86out += ' if (!(await ' + ($formatRef) + '(' + ($data) + '))) { ';87} else {88out += ' if (! ';89var $formatRef = 'formats' + it.util.getProperty($schema);90if ($isObject) $formatRef += '.validate';91if (typeof $format == 'function') {92out += ' ' + ($formatRef) + '(' + ($data) + ') ';93} else {94out += ' ' + ($formatRef) + '.test(' + ($data) + ') ';95}96out += ') { ';97}98}99var $$outStack = $$outStack || [];100$$outStack.push(out);101out = ''; /* istanbul ignore else */102if (it.createErrors !== false) {103out += ' { keyword: \'' + ('format') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { format: ';104if ($isData) {105out += '' + ($schemaValue);106} else {107out += '' + (it.util.toQuotedString($schema));108}109out += ' } ';110if (it.opts.messages !== false) {111out += ' , message: \'should match format "';112if ($isData) {113out += '\' + ' + ($schemaValue) + ' + \'';114} else {115out += '' + (it.util.escapeQuotes($schema));116}117out += '"\' ';118}119if (it.opts.verbose) {120out += ' , schema: ';121if ($isData) {122out += 'validate.schema' + ($schemaPath);123} else {124out += '' + (it.util.toQuotedString($schema));125}126out += ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';127}128out += ' } ';129} else {130out += ' {} ';131}132var __err = out;133out = $$outStack.pop();134if (!it.compositeRule && $breakOnError) {135/* istanbul ignore if */136if (it.async) {137out += ' throw new ValidationError([' + (__err) + ']); ';138} else {139out += ' validate.errors = [' + (__err) + ']; return false; ';140}141} else {142out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';143}144out += ' } ';145if ($breakOnError) {146out += ' else { ';147}148return out;149}150151152