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.

81 lines
1.6 KiB

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