errors.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. 'use strict';
  2. const Annotate = require('./annotate');
  3. const Common = require('./common');
  4. const Template = require('./template');
  5. const internals = {};
  6. exports.Report = class {
  7. constructor(code, value, local, flags, messages, state, prefs) {
  8. this.code = code;
  9. this.flags = flags;
  10. this.messages = messages;
  11. this.path = state.path;
  12. this.prefs = prefs;
  13. this.state = state;
  14. this.value = value;
  15. this.message = null;
  16. this.template = null;
  17. this.local = local || {};
  18. this.local.label = exports.label(this.flags, this.state, this.prefs, this.messages);
  19. if (this.value !== undefined &&
  20. !this.local.hasOwnProperty('value')) {
  21. this.local.value = this.value;
  22. }
  23. if (this.path.length) {
  24. const key = this.path[this.path.length - 1];
  25. if (typeof key !== 'object') {
  26. this.local.key = key;
  27. }
  28. }
  29. }
  30. _setTemplate(template) {
  31. this.template = template;
  32. if (!this.flags.label &&
  33. this.path.length === 0) {
  34. const localized = this._template(this.template, 'root');
  35. if (localized) {
  36. this.local.label = localized;
  37. }
  38. }
  39. }
  40. toString() {
  41. if (this.message) {
  42. return this.message;
  43. }
  44. const code = this.code;
  45. if (!this.prefs.errors.render) {
  46. return this.code;
  47. }
  48. const template = this._template(this.template) ||
  49. this._template(this.prefs.messages) ||
  50. this._template(this.messages);
  51. if (template === undefined) {
  52. return `Error code "${code}" is not defined, your custom type is missing the correct messages definition`;
  53. }
  54. // Render and cache result
  55. this.message = template.render(this.value, this.state, this.prefs, this.local, { errors: this.prefs.errors, messages: [this.prefs.messages, this.messages] });
  56. if (!this.prefs.errors.label) {
  57. this.message = this.message.replace(/^"" /, '').trim();
  58. }
  59. return this.message;
  60. }
  61. _template(messages, code) {
  62. return exports.template(this.value, messages, code || this.code, this.state, this.prefs);
  63. }
  64. };
  65. exports.path = function (path) {
  66. let label = '';
  67. for (const segment of path) {
  68. if (typeof segment === 'object') { // Exclude array single path segment
  69. continue;
  70. }
  71. if (typeof segment === 'string') {
  72. if (label) {
  73. label += '.';
  74. }
  75. label += segment;
  76. }
  77. else {
  78. label += `[${segment}]`;
  79. }
  80. }
  81. return label;
  82. };
  83. exports.template = function (value, messages, code, state, prefs) {
  84. if (!messages) {
  85. return;
  86. }
  87. if (Template.isTemplate(messages)) {
  88. return code !== 'root' ? messages : null;
  89. }
  90. let lang = prefs.errors.language;
  91. if (Common.isResolvable(lang)) {
  92. lang = lang.resolve(value, state, prefs);
  93. }
  94. if (lang &&
  95. messages[lang]) {
  96. if (messages[lang][code] !== undefined) {
  97. return messages[lang][code];
  98. }
  99. if (messages[lang]['*'] !== undefined) {
  100. return messages[lang]['*'];
  101. }
  102. }
  103. if (!messages[code]) {
  104. return messages['*'];
  105. }
  106. return messages[code];
  107. };
  108. exports.label = function (flags, state, prefs, messages) {
  109. if (flags.label) {
  110. return flags.label;
  111. }
  112. if (!prefs.errors.label) {
  113. return '';
  114. }
  115. let path = state.path;
  116. if (prefs.errors.label === 'key' &&
  117. state.path.length > 1) {
  118. path = state.path.slice(-1);
  119. }
  120. const normalized = exports.path(path);
  121. if (normalized) {
  122. return normalized;
  123. }
  124. return exports.template(null, prefs.messages, 'root', state, prefs) ||
  125. messages && exports.template(null, messages, 'root', state, prefs) ||
  126. 'value';
  127. };
  128. exports.process = function (errors, original, prefs) {
  129. if (!errors) {
  130. return null;
  131. }
  132. const { override, message, details } = exports.details(errors);
  133. if (override) {
  134. return override;
  135. }
  136. if (prefs.errors.stack) {
  137. return new exports.ValidationError(message, details, original);
  138. }
  139. const limit = Error.stackTraceLimit;
  140. Error.stackTraceLimit = 0;
  141. const validationError = new exports.ValidationError(message, details, original);
  142. Error.stackTraceLimit = limit;
  143. return validationError;
  144. };
  145. exports.details = function (errors, options = {}) {
  146. let messages = [];
  147. const details = [];
  148. for (const item of errors) {
  149. // Override
  150. if (item instanceof Error) {
  151. if (options.override !== false) {
  152. return { override: item };
  153. }
  154. const message = item.toString();
  155. messages.push(message);
  156. details.push({
  157. message,
  158. type: 'override',
  159. context: { error: item }
  160. });
  161. continue;
  162. }
  163. // Report
  164. const message = item.toString();
  165. messages.push(message);
  166. details.push({
  167. message,
  168. path: item.path.filter((v) => typeof v !== 'object'),
  169. type: item.code,
  170. context: item.local
  171. });
  172. }
  173. if (messages.length > 1) {
  174. messages = [...new Set(messages)];
  175. }
  176. return { message: messages.join('. '), details };
  177. };
  178. exports.ValidationError = class extends Error {
  179. constructor(message, details, original) {
  180. super(message);
  181. this._original = original;
  182. this.details = details;
  183. }
  184. static isError(err) {
  185. return err instanceof exports.ValidationError;
  186. }
  187. };
  188. exports.ValidationError.prototype.isJoi = true;
  189. exports.ValidationError.prototype.name = 'ValidationError';
  190. exports.ValidationError.prototype.annotate = Annotate.error;