forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.validation.test.js
245 lines (198 loc) · 6 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
/**
* Module dependencies.
*/
'use strict';
const assert = require('power-assert');
const start = require('./common');
const mongoose = start.mongoose;
const Schema = mongoose.Schema;
const SchemaType = mongoose.SchemaType;
const ValidatorError = SchemaType.ValidatorError;
const ValidationError = require('../lib/error/validation');
describe('ValidationError', function() {
describe('#infiniteRecursion', function() {
it('does not cause RangeError (gh-1834)', function(done) {
let SubSchema,
M,
model;
SubSchema = new Schema({
name: {type: String, required: true},
contents: [new Schema({
key: {type: String, required: true},
value: {type: String, required: true}
}, {_id: false})]
});
M = mongoose.model('SubSchema', SubSchema);
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) {
let MinSchema,
M,
model;
MinSchema = new Schema({
appointmentDate: {type: Date, min: Date.now}
});
M = mongoose.model('MinSchema', MinSchema);
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.');
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) {
let MaxSchema,
M,
model;
MaxSchema = new Schema({
birthdate: {type: Date, max: Date.now}
});
M = mongoose.model('MaxSchema', MaxSchema);
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');
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) {
let AddressSchema,
Address,
model;
AddressSchema = new Schema({
postalCode: {type: String, minlength: 5}
});
Address = mongoose.model('MinLengthAddress', AddressSchema);
model = new Address({
postalCode: '9512'
});
// should fail validation
model.validate(function(err) {
assert.notEqual(err, null, 'String minlegth validation failed.');
model.postalCode = '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 }
});
const Address = mongoose.model('gh4207', AddressSchema);
const model = new Address({
postalCode: '9512'
});
// should fail validation
model.validate(function(err) {
assert.equal(err.errors['postalCode'].message, 'woops!');
mongoose.Error.messages = old;
done();
});
});
});
describe('#maxlength', function() {
it('causes a validation error', function(done) {
let AddressSchema,
Address,
model;
AddressSchema = new Schema({
postalCode: {type: String, maxlength: 10}
});
Address = mongoose.model('MaxLengthAddress', AddressSchema);
model = new Address({
postalCode: '95125012345'
});
// should fail validation
model.validate(function(err) {
assert.notEqual(err, null, 'String maxlegth validation failed.');
model.postalCode = '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(done) {
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');
done();
});
});
it('JSON.stringify() with message (gh-5309)', function(done) {
model.modelName = 'TestClass';
const err = new ValidationError(new model());
err.addError('test', { 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);
done();
function model() {}
});
});