Ohm-Management - Projektarbeit B-ME
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.

ref.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use strict';
  2. module.exports = function generate_ref(it, $keyword, $ruleType) {
  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 = it.MissingRefError.message(it.baseId, $schema);
  24. if (it.opts.missingRefs == 'fail') {
  25. it.logger.error($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. it.logger.warn($message);
  57. if ($breakOnError) {
  58. out += ' if (true) { ';
  59. }
  60. } else {
  61. throw new it.MissingRefError(it.baseId, $schema, $message);
  62. }
  63. } else if ($refVal.inline) {
  64. var $it = it.util.copy(it);
  65. $it.level++;
  66. var $nextValid = 'valid' + $it.level;
  67. $it.schema = $refVal.schema;
  68. $it.schemaPath = '';
  69. $it.errSchemaPath = $schema;
  70. var $code = it.validate($it).replace(/validate\.schema/g, $refVal.code);
  71. out += ' ' + ($code) + ' ';
  72. if ($breakOnError) {
  73. out += ' if (' + ($nextValid) + ') { ';
  74. }
  75. } else {
  76. $async = $refVal.$async === true || (it.async && $refVal.$async !== false);
  77. $refCode = $refVal.code;
  78. }
  79. }
  80. if ($refCode) {
  81. var $$outStack = $$outStack || [];
  82. $$outStack.push(out);
  83. out = '';
  84. if (it.opts.passContext) {
  85. out += ' ' + ($refCode) + '.call(this, ';
  86. } else {
  87. out += ' ' + ($refCode) + '( ';
  88. }
  89. out += ' ' + ($data) + ', (dataPath || \'\')';
  90. if (it.errorPath != '""') {
  91. out += ' + ' + (it.errorPath);
  92. }
  93. var $parentData = $dataLvl ? 'data' + (($dataLvl - 1) || '') : 'parentData',
  94. $parentDataProperty = $dataLvl ? it.dataPathArr[$dataLvl] : 'parentDataProperty';
  95. out += ' , ' + ($parentData) + ' , ' + ($parentDataProperty) + ', rootData) ';
  96. var __callValidate = out;
  97. out = $$outStack.pop();
  98. if ($async) {
  99. if (!it.async) throw new Error('async schema referenced by sync schema');
  100. if ($breakOnError) {
  101. out += ' var ' + ($valid) + '; ';
  102. }
  103. out += ' try { await ' + (__callValidate) + '; ';
  104. if ($breakOnError) {
  105. out += ' ' + ($valid) + ' = true; ';
  106. }
  107. out += ' } 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 += ' ' + ($valid) + ' = false; ';
  110. }
  111. out += ' } ';
  112. if ($breakOnError) {
  113. out += ' if (' + ($valid) + ') { ';
  114. }
  115. } else {
  116. out += ' if (!' + (__callValidate) + ') { if (vErrors === null) vErrors = ' + ($refCode) + '.errors; else vErrors = vErrors.concat(' + ($refCode) + '.errors); errors = vErrors.length; } ';
  117. if ($breakOnError) {
  118. out += ' else { ';
  119. }
  120. }
  121. }
  122. return out;
  123. }