ref.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict';
  2. module.exports = function generate_ref(it, $keyword) {
  3. var out = ' ';
  4. var $lvl = it.level;
  5. var $dataLvl = it.dataLevel;
  6. var $schema = it.schema[$keyword];
  7. var $errSchemaPath = it.errSchemaPath + '/' + $keyword;
  8. var $breakOnError = !it.opts.allErrors;
  9. var $data = 'data' + ($dataLvl || '');
  10. var $valid = 'valid' + $lvl;
  11. var $async, $refCode;
  12. if ($schema == '#' || $schema == '#/') {
  13. if (it.isRoot) {
  14. $async = it.async;
  15. $refCode = 'validate';
  16. } else {
  17. $async = it.root.schema.$async === true;
  18. $refCode = 'root.refVal[0]';
  19. }
  20. } else {
  21. var $refVal = it.resolveRef(it.baseId, $schema, it.isRoot);
  22. if ($refVal === undefined) {
  23. var $message = 'can\'t resolve reference ' + $schema + ' from id ' + it.baseId;
  24. if (it.opts.missingRefs == 'fail') {
  25. console.log($message);
  26. var $$outStack = $$outStack || [];
  27. $$outStack.push(out);
  28. out = ''; /* istanbul ignore else */
  29. if (it.createErrors !== false) {
  30. out += ' { keyword: \'' + ('$ref') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { ref: \'' + (it.util.escapeQuotes($schema)) + '\' } ';
  31. if (it.opts.messages !== false) {
  32. out += ' , message: \'can\\\'t resolve reference ' + (it.util.escapeQuotes($schema)) + '\' ';
  33. }
  34. if (it.opts.verbose) {
  35. out += ' , schema: ' + (it.util.toQuotedString($schema)) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  36. }
  37. out += ' } ';
  38. } else {
  39. out += ' {} ';
  40. }
  41. var __err = out;
  42. out = $$outStack.pop();
  43. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  44. if (it.async) {
  45. out += ' throw new ValidationError([' + (__err) + ']); ';
  46. } else {
  47. out += ' validate.errors = [' + (__err) + ']; return false; ';
  48. }
  49. } else {
  50. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  51. }
  52. if ($breakOnError) {
  53. out += ' if (false) { ';
  54. }
  55. } else if (it.opts.missingRefs == 'ignore') {
  56. console.log($message);
  57. if ($breakOnError) {
  58. out += ' if (true) { ';
  59. }
  60. } else {
  61. var $error = new Error($message);
  62. $error.missingRef = it.resolve.url(it.baseId, $schema);
  63. $error.missingSchema = it.resolve.normalizeId(it.resolve.fullPath($error.missingRef));
  64. throw $error;
  65. }
  66. } else if ($refVal.inline) {
  67. var $it = it.util.copy(it);
  68. $it.level++;
  69. var $nextValid = 'valid' + $it.level;
  70. $it.schema = $refVal.schema;
  71. $it.schemaPath = '';
  72. $it.errSchemaPath = $schema;
  73. var $code = it.validate($it).replace(/validate\.schema/g, $refVal.code);
  74. out += ' ' + ($code) + ' ';
  75. if ($breakOnError) {
  76. out += ' if (' + ($nextValid) + ') { ';
  77. }
  78. } else {
  79. $async = $refVal.$async === true;
  80. $refCode = $refVal.code;
  81. }
  82. }
  83. if ($refCode) {
  84. var $$outStack = $$outStack || [];
  85. $$outStack.push(out);
  86. out = '';
  87. if (it.opts.passContext) {
  88. out += ' ' + ($refCode) + '.call(this, ';
  89. } else {
  90. out += ' ' + ($refCode) + '( ';
  91. }
  92. out += ' ' + ($data) + ', (dataPath || \'\')';
  93. if (it.errorPath != '""') {
  94. out += ' + ' + (it.errorPath);
  95. }
  96. var $parentData = $dataLvl ? 'data' + (($dataLvl - 1) || '') : 'parentData',
  97. $parentDataProperty = $dataLvl ? it.dataPathArr[$dataLvl] : 'parentDataProperty';
  98. out += ' , ' + ($parentData) + ' , ' + ($parentDataProperty) + ', rootData) ';
  99. var __callValidate = out;
  100. out = $$outStack.pop();
  101. if ($async) {
  102. if (!it.async) throw new Error('async schema referenced by sync schema');
  103. out += ' try { ';
  104. if ($breakOnError) {
  105. out += 'var ' + ($valid) + ' =';
  106. }
  107. out += ' ' + (it.yieldAwait) + ' ' + (__callValidate) + '; } catch (e) { if (!(e instanceof ValidationError)) throw e; if (vErrors === null) vErrors = e.errors; else vErrors = vErrors.concat(e.errors); errors = vErrors.length; } ';
  108. if ($breakOnError) {
  109. out += ' if (' + ($valid) + ') { ';
  110. }
  111. } else {
  112. out += ' if (!' + (__callValidate) + ') { if (vErrors === null) vErrors = ' + ($refCode) + '.errors; else vErrors = vErrors.concat(' + ($refCode) + '.errors); errors = vErrors.length; } ';
  113. if ($breakOnError) {
  114. out += ' else { ';
  115. }
  116. }
  117. }
  118. return out;
  119. }