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