ref.jst 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 = 'can\'t resolve reference ' + $schema + ' from id ' + it.baseId; }}
  27. {{? it.opts.missingRefs == 'fail' }}
  28. {{ console.log($message); }}
  29. {{# def.error:'$ref' }}
  30. {{? $breakOnError }} if (false) { {{?}}
  31. {{?? it.opts.missingRefs == 'ignore' }}
  32. {{ console.log($message); }}
  33. {{? $breakOnError }} if (true) { {{?}}
  34. {{??}}
  35. {{
  36. var $error = new Error($message);
  37. $error.missingRef = it.resolve.url(it.baseId, $schema);
  38. $error.missingSchema = it.resolve.normalizeId(it.resolve.fullPath($error.missingRef));
  39. throw $error;
  40. }}
  41. {{?}}
  42. {{?? $refVal.inline }}
  43. {{# def.setupNextLevel }}
  44. {{
  45. $it.schema = $refVal.schema;
  46. $it.schemaPath = '';
  47. $it.errSchemaPath = $schema;
  48. }}
  49. {{ var $code = it.validate($it).replace(/validate\.schema/g, $refVal.code); }}
  50. {{= $code }}
  51. {{? $breakOnError}}
  52. if ({{=$nextValid}}) {
  53. {{?}}
  54. {{??}}
  55. {{
  56. $async = $refVal.$async === true;
  57. $refCode = $refVal.code;
  58. }}
  59. {{?}}
  60. {{?}}
  61. {{? $refCode }}
  62. {{# def.beginDefOut}}
  63. {{# def._validateRef:$refCode }}
  64. {{# def.storeDefOut:__callValidate }}
  65. {{? $async }}
  66. {{ if (!it.async) throw new Error('async schema referenced by sync schema'); }}
  67. try { {{? $breakOnError }}var {{=$valid}} ={{?}} {{=it.yieldAwait}} {{=__callValidate}}; }
  68. catch (e) {
  69. if (!(e instanceof ValidationError)) throw e;
  70. if (vErrors === null) vErrors = e.errors;
  71. else vErrors = vErrors.concat(e.errors);
  72. errors = vErrors.length;
  73. }
  74. {{? $breakOnError }} if ({{=$valid}}) { {{?}}
  75. {{??}}
  76. if (!{{=__callValidate}}) {
  77. if (vErrors === null) vErrors = {{=$refCode}}.errors;
  78. else vErrors = vErrors.concat({{=$refCode}}.errors);
  79. errors = vErrors.length;
  80. } {{? $breakOnError }} else { {{?}}
  81. {{?}}
  82. {{?}}