format.jst 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. {{# def.definitions }}
  2. {{# def.errors }}
  3. {{# def.setupKeyword }}
  4. {{## def.skipFormat:
  5. {{? $breakOnError }} if (true) { {{?}}
  6. {{ return out; }}
  7. #}}
  8. {{? it.opts.format === false }}{{# def.skipFormat }}{{?}}
  9. {{# def.$data }}
  10. {{## def.$dataCheckFormat:
  11. {{# def.$dataNotType:'string' }}
  12. ({{? $unknownFormats === true || $allowUnknown }}
  13. ({{=$schemaValue}} && !{{=$format}}
  14. {{? $allowUnknown }}
  15. && self._opts.unknownFormats.indexOf({{=$schemaValue}}) == -1
  16. {{?}}) ||
  17. {{?}}
  18. ({{=$format}} && !(typeof {{=$format}} == 'function'
  19. ? {{? it.async}}
  20. (async{{=$lvl}} ? {{=it.yieldAwait}} {{=$format}}({{=$data}}) : {{=$format}}({{=$data}}))
  21. {{??}}
  22. {{=$format}}({{=$data}})
  23. {{?}}
  24. : {{=$format}}.test({{=$data}}))))
  25. #}}
  26. {{## def.checkFormat:
  27. {{
  28. var $formatRef = 'formats' + it.util.getProperty($schema);
  29. if ($isObject) $formatRef += '.validate';
  30. }}
  31. {{? typeof $format == 'function' }}
  32. {{=$formatRef}}({{=$data}})
  33. {{??}}
  34. {{=$formatRef}}.test({{=$data}})
  35. {{?}}
  36. #}}
  37. {{
  38. var $unknownFormats = it.opts.unknownFormats
  39. , $allowUnknown = Array.isArray($unknownFormats);
  40. }}
  41. {{? $isData }}
  42. {{ var $format = 'format' + $lvl; }}
  43. var {{=$format}} = formats[{{=$schemaValue}}];
  44. var isObject{{=$lvl}} = typeof {{=$format}} == 'object'
  45. && !({{=$format}} instanceof RegExp)
  46. && {{=$format}}.validate;
  47. if (isObject{{=$lvl}}) {
  48. {{? it.async}}
  49. var async{{=$lvl}} = {{=$format}}.async;
  50. {{?}}
  51. {{=$format}} = {{=$format}}.validate;
  52. }
  53. if ({{# def.$dataCheckFormat }}) {
  54. {{??}}
  55. {{ var $format = it.formats[$schema]; }}
  56. {{? !$format }}
  57. {{? $unknownFormats === true || ($allowUnknown && $unknownFormats.indexOf($schema) == -1) }}
  58. {{ throw new Error('unknown format "' + $schema + '" is used in schema at path "' + it.errSchemaPath + '"'); }}
  59. {{??}}
  60. {{
  61. if (!$allowUnknown) {
  62. console.warn('unknown format "' + $schema + '" ignored in schema at path "' + it.errSchemaPath + '"');
  63. if ($unknownFormats !== 'ignore')
  64. console.warn('In the next major version it will throw exception. See option unknownFormats for more information');
  65. }
  66. }}
  67. {{# def.skipFormat }}
  68. {{?}}
  69. {{?}}
  70. {{
  71. var $isObject = typeof $format == 'object'
  72. && !($format instanceof RegExp)
  73. && $format.validate;
  74. if ($isObject) {
  75. var $async = $format.async === true;
  76. $format = $format.validate;
  77. }
  78. }}
  79. {{? $async }}
  80. {{
  81. if (!it.async) throw new Error('async format in sync schema');
  82. var $formatRef = 'formats' + it.util.getProperty($schema) + '.validate';
  83. }}
  84. if (!({{=it.yieldAwait}} {{=$formatRef}}({{=$data}}))) {
  85. {{??}}
  86. if (!{{# def.checkFormat }}) {
  87. {{?}}
  88. {{?}}
  89. {{# def.error:'format' }}
  90. } {{? $breakOnError }} else { {{?}}