format.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. 'use strict';
  2. module.exports = function generate_format(it, $keyword) {
  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.v5 && $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. out += ' var ' + ($format) + ' = formats[' + ($schemaValue) + ']; var isObject' + ($lvl) + ' = typeof ' + ($format) + ' == \'object\' && !(' + ($format) + ' instanceof RegExp) && ' + ($format) + '.validate; if (isObject' + ($lvl) + ') { ';
  30. if (it.async) {
  31. out += ' var async' + ($lvl) + ' = ' + ($format) + '.async; ';
  32. }
  33. out += ' ' + ($format) + ' = ' + ($format) + '.validate; } if ( ';
  34. if ($isData) {
  35. out += ' (' + ($schemaValue) + ' !== undefined && typeof ' + ($schemaValue) + ' != \'string\') || ';
  36. }
  37. out += ' (';
  38. if ($unknownFormats === true || $allowUnknown) {
  39. out += ' (' + ($schemaValue) + ' && !' + ($format) + ' ';
  40. if ($allowUnknown) {
  41. out += ' && self._opts.unknownFormats.indexOf(' + ($schemaValue) + ') == -1 ';
  42. }
  43. out += ') || ';
  44. }
  45. out += ' (' + ($format) + ' && !(typeof ' + ($format) + ' == \'function\' ? ';
  46. if (it.async) {
  47. out += ' (async' + ($lvl) + ' ? ' + (it.yieldAwait) + ' ' + ($format) + '(' + ($data) + ') : ' + ($format) + '(' + ($data) + ')) ';
  48. } else {
  49. out += ' ' + ($format) + '(' + ($data) + ') ';
  50. }
  51. out += ' : ' + ($format) + '.test(' + ($data) + '))))) {';
  52. } else {
  53. var $format = it.formats[$schema];
  54. if (!$format) {
  55. if ($unknownFormats === true || ($allowUnknown && $unknownFormats.indexOf($schema) == -1)) {
  56. throw new Error('unknown format "' + $schema + '" is used in schema at path "' + it.errSchemaPath + '"');
  57. } else {
  58. if (!$allowUnknown) {
  59. console.warn('unknown format "' + $schema + '" ignored in schema at path "' + it.errSchemaPath + '"');
  60. if ($unknownFormats !== 'ignore') console.warn('In the next major version it will throw exception. See option unknownFormats for more information');
  61. }
  62. if ($breakOnError) {
  63. out += ' if (true) { ';
  64. }
  65. return out;
  66. }
  67. }
  68. var $isObject = typeof $format == 'object' && !($format instanceof RegExp) && $format.validate;
  69. if ($isObject) {
  70. var $async = $format.async === true;
  71. $format = $format.validate;
  72. }
  73. if ($async) {
  74. if (!it.async) throw new Error('async format in sync schema');
  75. var $formatRef = 'formats' + it.util.getProperty($schema) + '.validate';
  76. out += ' if (!(' + (it.yieldAwait) + ' ' + ($formatRef) + '(' + ($data) + '))) { ';
  77. } else {
  78. out += ' if (! ';
  79. var $formatRef = 'formats' + it.util.getProperty($schema);
  80. if ($isObject) $formatRef += '.validate';
  81. if (typeof $format == 'function') {
  82. out += ' ' + ($formatRef) + '(' + ($data) + ') ';
  83. } else {
  84. out += ' ' + ($formatRef) + '.test(' + ($data) + ') ';
  85. }
  86. out += ') { ';
  87. }
  88. }
  89. var $$outStack = $$outStack || [];
  90. $$outStack.push(out);
  91. out = ''; /* istanbul ignore else */
  92. if (it.createErrors !== false) {
  93. out += ' { keyword: \'' + ('format') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { format: ';
  94. if ($isData) {
  95. out += '' + ($schemaValue);
  96. } else {
  97. out += '' + (it.util.toQuotedString($schema));
  98. }
  99. out += ' } ';
  100. if (it.opts.messages !== false) {
  101. out += ' , message: \'should match format "';
  102. if ($isData) {
  103. out += '\' + ' + ($schemaValue) + ' + \'';
  104. } else {
  105. out += '' + (it.util.escapeQuotes($schema));
  106. }
  107. out += '"\' ';
  108. }
  109. if (it.opts.verbose) {
  110. out += ' , schema: ';
  111. if ($isData) {
  112. out += 'validate.schema' + ($schemaPath);
  113. } else {
  114. out += '' + (it.util.toQuotedString($schema));
  115. }
  116. out += ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  117. }
  118. out += ' } ';
  119. } else {
  120. out += ' {} ';
  121. }
  122. var __err = out;
  123. out = $$outStack.pop();
  124. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  125. if (it.async) {
  126. out += ' throw new ValidationError([' + (__err) + ']); ';
  127. } else {
  128. out += ' validate.errors = [' + (__err) + ']; return false; ';
  129. }
  130. } else {
  131. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  132. }
  133. out += ' } ';
  134. if ($breakOnError) {
  135. out += ' else { ';
  136. }
  137. return out;
  138. }