2 module.exports = function generate_format(it, $keyword) {
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;
11 var $data = 'data' + ($dataLvl || '');
12 if (it.opts.format === false) {
14 out += ' if (true) { ';
18 var $isData = it.opts.v5 && $schema && $schema.$data,
21 out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
22 $schemaValue = 'schema' + $lvl;
24 $schemaValue = $schema;
26 var $unknownFormats = it.opts.unknownFormats,
27 $allowUnknown = Array.isArray($unknownFormats);
29 var $format = 'format' + $lvl;
30 out += ' var ' + ($format) + ' = formats[' + ($schemaValue) + ']; var isObject' + ($lvl) + ' = typeof ' + ($format) + ' == \'object\' && !(' + ($format) + ' instanceof RegExp) && ' + ($format) + '.validate; if (isObject' + ($lvl) + ') { ';
32 out += ' var async' + ($lvl) + ' = ' + ($format) + '.async; ';
34 out += ' ' + ($format) + ' = ' + ($format) + '.validate; } if ( ';
36 out += ' (' + ($schemaValue) + ' !== undefined && typeof ' + ($schemaValue) + ' != \'string\') || ';
39 if ($unknownFormats === true || $allowUnknown) {
40 out += ' (' + ($schemaValue) + ' && !' + ($format) + ' ';
42 out += ' && self._opts.unknownFormats.indexOf(' + ($schemaValue) + ') == -1 ';
46 out += ' (' + ($format) + ' && !(typeof ' + ($format) + ' == \'function\' ? ';
48 out += ' (async' + ($lvl) + ' ? ' + (it.yieldAwait) + ' ' + ($format) + '(' + ($data) + ') : ' + ($format) + '(' + ($data) + ')) ';
50 out += ' ' + ($format) + '(' + ($data) + ') ';
52 out += ' : ' + ($format) + '.test(' + ($data) + '))))) {';
54 var $format = it.formats[$schema];
56 if ($unknownFormats === true || ($allowUnknown && $unknownFormats.indexOf($schema) == -1)) {
57 throw new Error('unknown format "' + $schema + '" is used in schema at path "' + it.errSchemaPath + '"');
60 console.warn('unknown format "' + $schema + '" ignored in schema at path "' + it.errSchemaPath + '"');
61 if ($unknownFormats !== 'ignore') console.warn('In the next major version it will throw exception. See option unknownFormats for more information');
64 out += ' if (true) { ';
69 var $isObject = typeof $format == 'object' && !($format instanceof RegExp) && $format.validate;
71 var $async = $format.async === true;
72 $format = $format.validate;
75 if (!it.async) throw new Error('async format in sync schema');
76 var $formatRef = 'formats' + it.util.getProperty($schema) + '.validate';
77 out += ' if (!(' + (it.yieldAwait) + ' ' + ($formatRef) + '(' + ($data) + '))) { ';
80 var $formatRef = 'formats' + it.util.getProperty($schema);
81 if ($isObject) $formatRef += '.validate';
82 if (typeof $format == 'function') {
83 out += ' ' + ($formatRef) + '(' + ($data) + ') ';
85 out += ' ' + ($formatRef) + '.test(' + ($data) + ') ';
90 var $$outStack = $$outStack || [];
92 out = ''; /* istanbul ignore else */
93 if (it.createErrors !== false) {
94 out += ' { keyword: \'' + ($errorKeyword || 'format') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { format: ';
96 out += '' + ($schemaValue);
98 out += '' + (it.util.toQuotedString($schema));
101 if (it.opts.messages !== false) {
102 out += ' , message: \'should match format "';
104 out += '\' + ' + ($schemaValue) + ' + \'';
106 out += '' + (it.util.escapeQuotes($schema));
110 if (it.opts.verbose) {
111 out += ' , schema: ';
113 out += 'validate.schema' + ($schemaPath);
115 out += '' + (it.util.toQuotedString($schema));
117 out += ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
124 out = $$outStack.pop();
125 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
127 out += ' throw new ValidationError([' + (__err) + ']); ';
129 out += ' validate.errors = [' + (__err) + ']; return false; ';
132 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';