| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160 |
3×
3×
3×
3×
2×
8×
8×
8×
8×
8×
8×
8×
8×
10×
12×
2×
12×
8×
10×
10×
12×
12×
10×
3×
3×
3×
8×
10×
3×
3×
16×
3×
3×
16×
3×
8×
3×
3×
| /**
* Module dependencies
*/
var WLError = require('./WLError');
var WLUsageError = require('./WLUsageError');
var util = require('util');
var _ = require('lodash');
/**
* WLValidationError
*
* @extends WLError
*/
function WLValidationError(properties) {
// Call superclass
WLValidationError.super_.call(this, properties);
// Ensure valid usage
Iif (typeof this.invalidAttributes !== 'object') {
return new WLUsageError({
reason: 'An `invalidAttributes` object must be passed into the constructor for `WLValidationError`'
});
}
// if ( typeof this.model !== 'string' ) {
// return new WLUsageError({
// reason: 'A `model` string (the collection\'s `globalId`) must be passed into the constructor for `WLValidationError`'
// });
// }
// Customize the `reason` based on the # of invalid attributes
// (`reason` may not be overridden)
var isSingular = this.length === 1;
this.reason = util.format('%d attribute%s %s invalid',
this.length,
isSingular ? '' : 's',
isSingular ? 'is' : 'are');
// Always apply the 'E_VALIDATION' error code, even if it was overridden.
this.code = 'E_VALIDATION';
// Status may be overridden.
this.status = properties.status || 400;
// Model should always be set.
// (this should be the globalId of model, or "collection")
this.model = properties.model;
// Ensure messages exist for each invalidAttribute
this.invalidAttributes = _.mapValues(this.invalidAttributes, function(rules, attrName) {
return _.map(rules, function(rule) {
if (!rule.message) {
rule.message = util.format('A record with that `%s` already exists (`%s`).', attrName, rule.value);
}
return rule;
});
});
// Customize the `details`
this.details = util.format('Invalid attributes sent to %s:\n', this.model) +
_.reduce(this.messages, function(memo, messages, attrName) {
memo += ' • ' + attrName + '\n';
memo += _.reduce(messages, function(memo, message) {
memo += ' • ' + message + '\n';
return memo;
}, '');
return memo;
}, '');
}
util.inherits(WLValidationError, WLError);
/**
* `rules`
*
* @return {Object[Array[String]]} dictionary of validation rule ids, indexed by attribute
*/
WLValidationError.prototype.__defineGetter__('rules', function() {
return _.mapValues(this.invalidAttributes, function(rules, attrName) {
return _.pluck(rules, 'rule');
});
});
/**
* `messages` (aka `errors`)
*
* @return {Object[Array[String]]} dictionary of validation messages, indexed by attribute
*/
WLValidationError.prototype.__defineGetter__('messages', function() {
return _.mapValues(this.invalidAttributes, function(rules, attrName) {
return _.pluck(rules, 'message');
});
});
WLValidationError.prototype.__defineGetter__('errors', function() {
return this.messages;
});
/**
* `attributes` (aka `keys`)
*
* @return {Array[String]} of invalid attribute names
*/
WLValidationError.prototype.__defineGetter__('attributes', function() {
return _.keys(this.invalidAttributes);
});
WLValidationError.prototype.__defineGetter__('keys', function() {
return this.attributes;
});
/**
* `.length`
*
* @return {Integer} number of invalid attributes
*/
WLValidationError.prototype.__defineGetter__('length', function() {
return this.attributes.length;
});
/**
* `.ValidationError`
* (backwards-compatibility)
*
* @return {Object[Array[Object]]} number of invalid attributes
*/
WLValidationError.prototype.__defineGetter__('ValidationError', function() {
//
// TODO:
// Down the road- emit deprecation event here--
// (will log information about new error handling options)
//
return this.invalidAttributes;
});
/**
* [toJSON description]
* @type {[type]}
*/
WLValidationError.prototype.toJSON =
WLValidationError.prototype.toPOJO =
function() {
return {
error: this.code,
status: this.status,
summary: this.reason,
model: this.model,
invalidAttributes: this.invalidAttributes
};
};
module.exports = WLValidationError;
|