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