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.

validator.js 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*!
  2. * Module dependencies.
  3. */
  4. 'use strict';
  5. const MongooseError = require('./');
  6. /**
  7. * Schema validator error
  8. *
  9. * @param {Object} properties
  10. * @inherits MongooseError
  11. * @api private
  12. */
  13. function ValidatorError(properties) {
  14. let msg = properties.message;
  15. if (!msg) {
  16. msg = MongooseError.messages.general.default;
  17. }
  18. const message = this.formatMessage(msg, properties);
  19. MongooseError.call(this, message);
  20. properties = Object.assign({}, properties, { message: message });
  21. this.name = 'ValidatorError';
  22. if (Error.captureStackTrace) {
  23. Error.captureStackTrace(this);
  24. } else {
  25. this.stack = new Error().stack;
  26. }
  27. this.properties = properties;
  28. this.kind = properties.type;
  29. this.path = properties.path;
  30. this.value = properties.value;
  31. this.reason = properties.reason;
  32. }
  33. /*!
  34. * Inherits from MongooseError
  35. */
  36. ValidatorError.prototype = Object.create(MongooseError.prototype);
  37. ValidatorError.prototype.constructor = MongooseError;
  38. /*!
  39. * The object used to define this validator. Not enumerable to hide
  40. * it from `require('util').inspect()` output re: gh-3925
  41. */
  42. Object.defineProperty(ValidatorError.prototype, 'properties', {
  43. enumerable: false,
  44. writable: true,
  45. value: null
  46. });
  47. /*!
  48. * Formats error messages
  49. */
  50. ValidatorError.prototype.formatMessage = function(msg, properties) {
  51. if (typeof msg === 'function') {
  52. return msg(properties);
  53. }
  54. const propertyNames = Object.keys(properties);
  55. for (let i = 0; i < propertyNames.length; ++i) {
  56. const propertyName = propertyNames[i];
  57. if (propertyName === 'message') {
  58. continue;
  59. }
  60. msg = msg.replace('{' + propertyName.toUpperCase() + '}', properties[propertyName]);
  61. }
  62. return msg;
  63. };
  64. /*!
  65. * toString helper
  66. */
  67. ValidatorError.prototype.toString = function() {
  68. return this.message;
  69. };
  70. /*!
  71. * exports
  72. */
  73. module.exports = ValidatorError;