format.jst 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 != 'ignore' }}
  13. ({{=$schemaValue}} && !{{=$format}}
  14. {{? $allowUnknown }}
  15. && self._opts.unknownFormats.indexOf({{=$schemaValue}}) == -1
  16. {{?}}) ||
  17. {{?}}
  18. ({{=$format}} && {{=$formatType}} == '{{=$ruleType}}'
  19. && !(typeof {{=$format}} == 'function'
  20. ? {{? it.async}}
  21. (async{{=$lvl}} ? await {{=$format}}({{=$data}}) : {{=$format}}({{=$data}}))
  22. {{??}}
  23. {{=$format}}({{=$data}})
  24. {{?}}
  25. : {{=$format}}.test({{=$data}}))))
  26. #}}
  27. {{## def.checkFormat:
  28. {{
  29. var $formatRef = 'formats' + it.util.getProperty($schema);
  30. if ($isObject) $formatRef += '.validate';
  31. }}
  32. {{? typeof $format == 'function' }}
  33. {{=$formatRef}}({{=$data}})
  34. {{??}}
  35. {{=$formatRef}}.test({{=$data}})
  36. {{?}}
  37. #}}
  38. {{
  39. var $unknownFormats = it.opts.unknownFormats
  40. , $allowUnknown = Array.isArray($unknownFormats);
  41. }}
  42. {{? $isData }}
  43. {{
  44. var $format = 'format' + $lvl
  45. , $isObject = 'isObject' + $lvl
  46. , $formatType = 'formatType' + $lvl;
  47. }}
  48. var {{=$format}} = formats[{{=$schemaValue}}];
  49. var {{=$isObject}} = typeof {{=$format}} == 'object'
  50. && !({{=$format}} instanceof RegExp)
  51. && {{=$format}}.validate;
  52. var {{=$formatType}} = {{=$isObject}} && {{=$format}}.type || 'string';
  53. if ({{=$isObject}}) {
  54. {{? it.async}}
  55. var async{{=$lvl}} = {{=$format}}.async;
  56. {{?}}
  57. {{=$format}} = {{=$format}}.validate;
  58. }
  59. if ({{# def.$dataCheckFormat }}) {
  60. {{??}}
  61. {{ var $format = it.formats[$schema]; }}
  62. {{? !$format }}
  63. {{? $unknownFormats == 'ignore' }}
  64. {{ it.logger.warn('unknown format "' + $schema + '" ignored in schema at path "' + it.errSchemaPath + '"'); }}
  65. {{# def.skipFormat }}
  66. {{?? $allowUnknown && $unknownFormats.indexOf($schema) >= 0 }}
  67. {{# def.skipFormat }}
  68. {{??}}
  69. {{ throw new Error('unknown format "' + $schema + '" is used in schema at path "' + it.errSchemaPath + '"'); }}
  70. {{?}}
  71. {{?}}
  72. {{
  73. var $isObject = typeof $format == 'object'
  74. && !($format instanceof RegExp)
  75. && $format.validate;
  76. var $formatType = $isObject && $format.type || 'string';
  77. if ($isObject) {
  78. var $async = $format.async === true;
  79. $format = $format.validate;
  80. }
  81. }}
  82. {{? $formatType != $ruleType }}
  83. {{# def.skipFormat }}
  84. {{?}}
  85. {{? $async }}
  86. {{
  87. if (!it.async) throw new Error('async format in sync schema');
  88. var $formatRef = 'formats' + it.util.getProperty($schema) + '.validate';
  89. }}
  90. if (!(await {{=$formatRef}}({{=$data}}))) {
  91. {{??}}
  92. if (!{{# def.checkFormat }}) {
  93. {{?}}
  94. {{?}}
  95. {{# def.error:'format' }}
  96. } {{? $breakOnError }} else { {{?}}