format.js 5.4 KB

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