forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.validation.test.js
271 lines (224 loc) · 7.78 KB
/
errors.validation.test.js
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
* Module dependencies.
*/
'use strict';
const start = require('./common');
const ValidationError = require('../lib/error/validation');
const assert = require('assert');
const mongoose = start.mongoose;
const Schema = mongoose.Schema;
const SchemaType = mongoose.SchemaType;
const ValidatorError = SchemaType.ValidatorError;
describe('ValidationError', function() {
describe('#infiniteRecursion', function() {
it('does not cause RangeError (gh-1834)', function(done) {
const SubSchema = new Schema({
name: { type: String, required: true },
contents: [new Schema({
key: { type: String, required: true },
value: { type: String, required: true }
}, { _id: false })]
});
const M = mongoose.model('SubSchema', SubSchema);
const model = new M({
name: 'Model',
contents: [
{ key: 'foo' }
]
});
model.validate(function(err) {
assert.doesNotThrow(function() {
JSON.stringify(err);
});
done();
});
});
});
describe('#minDate', function() {
it('causes a validation error', function(done) {
const MinSchema = new Schema({
appointmentDate: { type: Date, min: Date.now }
});
const M = mongoose.model('MinSchema', MinSchema);
const model = new M({
appointmentDate: new Date(Date.now().valueOf() - 10000)
});
// should fail validation
model.validate(function(err) {
assert.notEqual(err, null, 'min Date validation failed.');
assert.ok(err.message.startsWith('MinSchema validation failed'));
model.appointmentDate = new Date(Date.now().valueOf() + 10000);
// should pass validation
model.validate(function(err) {
assert.equal(err, null);
done();
});
});
});
});
describe('#maxDate', function() {
it('causes a validation error', function(done) {
const MaxSchema = new Schema({
birthdate: { type: Date, max: Date.now }
});
const M = mongoose.model('MaxSchema', MaxSchema);
const model = new M({
birthdate: new Date(Date.now().valueOf() + 2000)
});
// should fail validation
model.validate(function(err) {
assert.notEqual(err, null, 'max Date validation failed');
assert.ok(err.message.startsWith('MaxSchema validation failed'));
model.birthdate = Date.now();
// should pass validation
model.validate(function(err) {
assert.equal(err, null, 'max Date validation failed');
done();
});
});
});
});
describe('#minLength', function() {
it('causes a validation error', function(done) {
const AddressSchema = new Schema({
postalCode: { type: String, minlength: 5 },
zipCode: { type: String, minLength: 5 }
});
const Address = mongoose.model('MinLengthAddress', AddressSchema);
const model = new Address({
postalCode: '9512',
zipCode: '9512'
});
// should fail validation
model.validate(function(err) {
assert.notEqual(err, null, 'String minLength validation failed.');
assert.ok(err.message.startsWith('MinLengthAddress validation failed'));
model.postalCode = '95125';
model.zipCode = '95125';
// should pass validation
model.validate(function(err) {
assert.equal(err, null);
done();
});
});
});
it('with correct error message (gh-4207)', function(done) {
const old = mongoose.Error.messages;
mongoose.Error.messages = {
String: {
minlength: 'woops!'
}
};
const AddressSchema = new Schema({
postalCode: { type: String, minlength: 5 },
zipCode: { type: String, minLength: 5 }
});
const Address = mongoose.model('gh4207', AddressSchema);
const model = new Address({
postalCode: '9512',
zipCode: '9512'
});
// should fail validation
model.validate(function(err) {
assert.equal(err.errors['postalCode'].message, 'woops!');
assert.ok(err.message.startsWith('gh4207 validation failed'));
mongoose.Error.messages = old;
done();
});
});
});
describe('#maxLength', function() {
it('causes a validation error', function(done) {
const AddressSchema = new Schema({
postalCode: { type: String, maxlength: 10 },
zipCode: { type: String, maxLength: 10 }
});
const Address = mongoose.model('MaxLengthAddress', AddressSchema);
const model = new Address({
postalCode: '95125012345',
zipCode: '95125012345'
});
// should fail validation
model.validate(function(err) {
assert.notEqual(err, null, 'String maxLength validation failed.');
assert.ok(err.message.startsWith('MaxLengthAddress validation failed'));
model.postalCode = '95125';
model.zipCode = '95125';
// should pass validation
model.validate(function(err) {
assert.equal(err, null);
done();
});
});
});
});
describe('#toString', function() {
it('does not cause RangeError (gh-1296)', function(done) {
const ASchema = new Schema({
key: { type: String, required: true },
value: { type: String, required: true }
});
const BSchema = new Schema({
contents: [ASchema]
});
const M = mongoose.model('A', BSchema);
const m = new M;
m.contents.push({ key: 'asdf' });
m.validate(function(err) {
assert.doesNotThrow(function() {
String(err);
});
done();
});
});
});
describe('formatMessage', function() {
it('replaces properties in a message', function() {
const props = { base: 'eggs', topping: 'bacon' };
const message = 'I had {BASE} and {TOPPING} for breakfast';
const result = ValidatorError.prototype.formatMessage(message, props);
assert.equal(result, 'I had eggs and bacon for breakfast');
});
});
it('JSON.stringify() with message (gh-5309) (gh-9296)', function() {
model.modelName = 'TestClass';
const err = new ValidationError(new model());
err.addError('test', new ValidatorError({ message: 'Fail' }));
const obj = JSON.parse(JSON.stringify(err));
assert.ok(obj.message.indexOf('TestClass validation failed') !== -1,
obj.message);
assert.ok(obj.message.indexOf('test: Fail') !== -1,
obj.message);
assert.ok(obj.errors['test'].message);
function model() {}
});
it('default error message', function() {
const err = new ValidationError();
assert.equal(err.message, 'Validation failed');
});
describe('when user code defines a r/o Error#toJSON', function() {
it('should not fail', function(done) {
const err = [];
const child = require('child_process')
.fork('./test/isolated/project-has-error.toJSON.js', ['--no-warnings'], { silent: true });
child.stderr.on('data', function(buf) { err.push(buf); });
child.on('exit', function(code) {
const stderr = err.filter(line => !line.includes('Warning: ')).join('');
assert.equal(stderr, '');
assert.equal(code, 0);
done();
});
});
});
it('should have error name in Cast error gh-10166', function(done) {
const zetaSchema = new Schema({ text: { type: String, required: [true, 'Text is required'] }, number: {
type: Number, required: [true, 'Number is required'] } });
const Zeta = mongoose.model('Zeta', zetaSchema);
const entry = new Zeta({ text: false, number: 'fsfsf' });
entry.validate(function(error) {
assert.ok(JSON.parse(JSON.stringify(error.errors.number.message)));
assert.ok(JSON.parse(JSON.stringify(error.errors.number.name)));
});
done();
});
});