validation.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*!
  2. * Module requirements
  3. */
  4. var MongooseError = require('../error.js');
  5. /**
  6. * Document Validation Error
  7. *
  8. * @api private
  9. * @param {Document} instance
  10. * @inherits MongooseError
  11. */
  12. function ValidationError (instance) {
  13. if (instance && instance.constructor.name === 'model') {
  14. MongooseError.call(this, instance.constructor.modelName + " validation failed");
  15. } else {
  16. MongooseError.call(this, "Validation failed");
  17. }
  18. this.stack = new Error().stack;
  19. this.name = 'ValidationError';
  20. this.errors = {};
  21. if (instance) {
  22. instance.errors = this.errors;
  23. }
  24. }
  25. /*!
  26. * Inherits from MongooseError.
  27. */
  28. ValidationError.prototype = Object.create(MongooseError.prototype);
  29. ValidationError.prototype.constructor = MongooseError;
  30. /**
  31. * Console.log helper
  32. */
  33. ValidationError.prototype.toString = function () {
  34. var ret = this.name + ': ';
  35. var msgs = [];
  36. Object.keys(this.errors).forEach(function (key) {
  37. if (this == this.errors[key]) return;
  38. msgs.push(String(this.errors[key]));
  39. }, this);
  40. return ret + msgs.join(', ');
  41. };
  42. /*!
  43. * Module exports
  44. */
  45. module.exports = exports = ValidationError;