validation-error.js
6.46 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use strict';
const BaseError = require('./base-error');
/**
* Validation Error. Thrown when the sequelize validation has failed. The error contains an `errors` property,
* which is an array with 1 or more ValidationErrorItems, one for each validation that failed.
*
* @param {string} message Error message
* @param {Array} [errors] Array of ValidationErrorItem objects describing the validation errors
*
* @property errors {ValidationErrorItems[]}
*/
class ValidationError extends BaseError {
constructor(message, errors) {
super(message);
this.name = 'SequelizeValidationError';
this.message = 'Validation Error';
/**
*
* @type {ValidationErrorItem[]}
*/
this.errors = errors || [];
// Use provided error message if available...
if (message) {
this.message = message;
// ... otherwise create a concatenated message out of existing errors.
} else if (this.errors.length > 0 && this.errors[0].message) {
this.message = this.errors.map(err => `${err.type || err.origin}: ${err.message}`).join(',\n');
}
}
/**
* Gets all validation error items for the path / field specified.
*
* @param {string} path The path to be checked for error items
*
* @returns {Array<ValidationErrorItem>} Validation error items for the specified path
*/
get(path) {
return this.errors.reduce((reduced, error) => {
if (error.path === path) {
reduced.push(error);
}
return reduced;
}, []);
}
}
/**
* Validation Error Item
* Instances of this class are included in the `ValidationError.errors` property.
*/
class ValidationErrorItem {
/**
* Creates new validation error item
*
* @param {string} message An error message
* @param {string} type The type/origin of the validation error
* @param {string} path The field that triggered the validation error
* @param {string} value The value that generated the error
* @param {object} [inst] the DAO instance that caused the validation error
* @param {object} [validatorKey] a validation "key", used for identification
* @param {string} [fnName] property name of the BUILT-IN validator function that caused the validation error (e.g. "in" or "len"), if applicable
* @param {string} [fnArgs] parameters used with the BUILT-IN validator function, if applicable
*/
constructor(message, type, path, value, inst, validatorKey, fnName, fnArgs) {
/**
* An error message
*
* @type {string} message
*/
this.message = message || '';
/**
* The type/origin of the validation error
*
* @type {string}
*/
this.type = null;
/**
* The field that triggered the validation error
*
* @type {string}
*/
this.path = path || null;
/**
* The value that generated the error
*
* @type {string}
*/
this.value = value !== undefined ? value : null;
this.origin = null;
/**
* The DAO instance that caused the validation error
*
* @type {Model}
*/
this.instance = inst || null;
/**
* A validation "key", used for identification
*
* @type {string}
*/
this.validatorKey = validatorKey || null;
/**
* Property name of the BUILT-IN validator function that caused the validation error (e.g. "in" or "len"), if applicable
*
* @type {string}
*/
this.validatorName = fnName || null;
/**
* Parameters used with the BUILT-IN validator function, if applicable
*
* @type {string}
*/
this.validatorArgs = fnArgs || [];
if (type) {
if (ValidationErrorItem.Origins[ type ]) {
this.origin = type;
} else {
const lowercaseType = `${type}`.toLowerCase().trim();
const realType = ValidationErrorItem.TypeStringMap[ lowercaseType ];
if (realType && ValidationErrorItem.Origins[ realType ]) {
this.origin = realType;
this.type = type;
}
}
}
// This doesn't need captureStackTrace because it's not a subclass of Error
}
/**
* return a lowercase, trimmed string "key" that identifies the validator.
*
* Note: the string will be empty if the instance has neither a valid `validatorKey` property nor a valid `validatorName` property
*
* @param {boolean} [useTypeAsNS=true] controls whether the returned value is "namespace",
* this parameter is ignored if the validator's `type` is not one of ValidationErrorItem.Origins
* @param {string} [NSSeparator='.'] a separator string for concatenating the namespace, must be not be empty,
* defaults to "." (fullstop). only used and validated if useTypeAsNS is TRUE.
* @throws {Error} thrown if NSSeparator is found to be invalid.
* @returns {string}
*
* @private
*/
getValidatorKey(useTypeAsNS, NSSeparator) {
const useTANS = useTypeAsNS === undefined || !!useTypeAsNS;
const NSSep = NSSeparator === undefined ? '.' : NSSeparator;
const type = this.origin;
const key = this.validatorKey || this.validatorName;
const useNS = useTANS && type && ValidationErrorItem.Origins[ type ];
if (useNS && (typeof NSSep !== 'string' || !NSSep.length)) {
throw new Error('Invalid namespace separator given, must be a non-empty string');
}
if (!(typeof key === 'string' && key.length)) {
return '';
}
return (useNS ? [type, key].join(NSSep) : key).toLowerCase().trim();
}
}
/**
* An enum that defines valid ValidationErrorItem `origin` values
*
* @type {object}
* @property CORE {string} specifies errors that originate from the sequelize "core"
* @property DB {string} specifies validation errors that originate from the storage engine
* @property FUNCTION {string} specifies validation errors that originate from validator functions (both built-in and custom) defined for a given attribute
*/
ValidationErrorItem.Origins = {
CORE: 'CORE',
DB: 'DB',
FUNCTION: 'FUNCTION'
};
/**
* An object that is used internally by the `ValidationErrorItem` class
* that maps current `type` strings (as given to ValidationErrorItem.constructor()) to
* our new `origin` values.
*
* @type {object}
*/
ValidationErrorItem.TypeStringMap = {
'notnull violation': 'CORE',
'string violation': 'CORE',
'unique violation': 'DB',
'validation error': 'FUNCTION'
};
module.exports = ValidationError;
module.exports.ValidationErrorItem = ValidationErrorItem;