You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.4 KiB

  1. {{# def.definitions }}
  2. {{# def.errors }}
  3. {{# def.setupKeyword }}
  4. {{## def._validateRef:_v:
  5. {{? it.opts.passContext }}
  6. {{=_v}}.call(this,
  7. {{??}}
  8. {{=_v}}(
  9. {{?}}
  10. {{=$data}}, {{# def.dataPath }}{{# def.passParentData }}, rootData)
  11. #}}
  12. {{ var $async, $refCode; }}
  13. {{? $schema == '#' || $schema == '#/' }}
  14. {{
  15. if (it.isRoot) {
  16. $async = it.async;
  17. $refCode = 'validate';
  18. } else {
  19. $async = it.root.schema.$async === true;
  20. $refCode = 'root.refVal[0]';
  21. }
  22. }}
  23. {{??}}
  24. {{ var $refVal = it.resolveRef(it.baseId, $schema, it.isRoot); }}
  25. {{? $refVal === undefined }}
  26. {{ var $message = it.MissingRefError.message(it.baseId, $schema); }}
  27. {{? it.opts.missingRefs == 'fail' }}
  28. {{ it.logger.error($message); }}
  29. {{# def.error:'$ref' }}
  30. {{? $breakOnError }} if (false) { {{?}}
  31. {{?? it.opts.missingRefs == 'ignore' }}
  32. {{ it.logger.warn($message); }}
  33. {{? $breakOnError }} if (true) { {{?}}
  34. {{??}}
  35. {{ throw new it.MissingRefError(it.baseId, $schema, $message); }}
  36. {{?}}
  37. {{?? $refVal.inline }}
  38. {{# def.setupNextLevel }}
  39. {{
  40. $it.schema = $refVal.schema;
  41. $it.schemaPath = '';
  42. $it.errSchemaPath = $schema;
  43. }}
  44. {{ var $code = it.validate($it).replace(/validate\.schema/g, $refVal.code); }}
  45. {{= $code }}
  46. {{? $breakOnError}}
  47. if ({{=$nextValid}}) {
  48. {{?}}
  49. {{??}}
  50. {{
  51. $async = $refVal.$async === true || (it.async && $refVal.$async !== false);
  52. $refCode = $refVal.code;
  53. }}
  54. {{?}}
  55. {{?}}
  56. {{? $refCode }}
  57. {{# def.beginDefOut}}
  58. {{# def._validateRef:$refCode }}
  59. {{# def.storeDefOut:__callValidate }}
  60. {{? $async }}
  61. {{ if (!it.async) throw new Error('async schema referenced by sync schema'); }}
  62. {{? $breakOnError }} var {{=$valid}}; {{?}}
  63. try {
  64. await {{=__callValidate}};
  65. {{? $breakOnError }} {{=$valid}} = true; {{?}}
  66. } catch (e) {
  67. if (!(e instanceof ValidationError)) throw e;
  68. if (vErrors === null) vErrors = e.errors;
  69. else vErrors = vErrors.concat(e.errors);
  70. errors = vErrors.length;
  71. {{? $breakOnError }} {{=$valid}} = false; {{?}}
  72. }
  73. {{? $breakOnError }} if ({{=$valid}}) { {{?}}
  74. {{??}}
  75. if (!{{=__callValidate}}) {
  76. if (vErrors === null) vErrors = {{=$refCode}}.errors;
  77. else vErrors = vErrors.concat({{=$refCode}}.errors);
  78. errors = vErrors.length;
  79. } {{? $breakOnError }} else { {{?}}
  80. {{?}}
  81. {{?}}