-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathvalidation.js
More file actions
418 lines (333 loc) · 11 KB
/
validation.js
File metadata and controls
418 lines (333 loc) · 11 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
var _ = require('lodash');
var should = require('should');
var helper = require('../support/spec_helper');
var async = require('async');
var common = require('../common');
var protocol = common.protocol().toLowerCase();
var ORM = require('../../');
describe("Validations", function() {
var db = null;
var Person = null;
var Person2 = null;
var setup = function (returnAll, required) {
return function (done) {
db.settings.set('properties.required', required);
db.settings.set('instance.returnAllErrors', returnAll);
Person = db.define("person", {
name: { type: 'text' },
height: { type: 'number' },
}, {
validations: {
name: ORM.validators.rangeLength(3, 30),
height: ORM.validators.rangeNumber(0.1, 3.0)
}
});
return helper.dropSync(Person, done);
};
};
notNull = function(val, next, data) {
if (val != null) {
return next('notnull');
}
return next();
};
var setupAlwaysValidate = function () {
return function (done) {
Person2 = db.define("person2", {
name: { type: 'text' },
mustbenull: { type: 'text', required:false, alwaysValidate: true }
, canbenull: { type: 'text', required:false }
}, {
validations: {
name: ORM.validators.rangeLength(3, 30),
mustbenull: notNull,
canbenull: notNull
}
});
return helper.dropSync(Person2, done);
};
};
before(function (done) {
helper.connect(function (connection) {
db = connection;
done();
});
});
after(function () {
db.close();
});
describe("alwaysValidate", function () {
before(setupAlwaysValidate());
it("I want to see it fail first (the absence of evidence)", function(done) {
var rachel = new Person2({name: 'rachel', canbenull:null, mustbenull:null});
rachel.save(function (err) {
should.not.exist(err);
return done();
});
});
it("then it should work", function(done) {
var tom = new Person2({name: 'tom', canbenull:null, mustbenull:'notnull'});
tom.save(function (err) {
should.exist(err);
should.equal(typeof err, "object");
should.equal(err.property, "mustbenull");
should.equal(err.msg, "notnull");
should.equal(err.type, "validation");
should.equal(tom.id, null);
return done();
});
});
});
describe("predefined", function () {
before(setup(false, false));
it("should work", function(done) {
var john = new Person({name: 'fdhdjendfjkdfhshdfhakdfjajhfdjhbfgk'});
john.save(function (err) {
should.equal(typeof err, "object");
should.equal(err.property, "name");
should.equal(err.value, "fdhdjendfjkdfhshdfhakdfjajhfdjhbfgk");
should.equal(err.msg, "out-of-range-length");
should.equal(err.type, "validation");
should.equal(john.id, null);
return done();
});
});
describe("unique", function () {
if (protocol === "mongodb") return;
var Product = null;
var setupUnique = function (ignoreCase, scope, msg) {
return function (done) {
Product = db.define("product_unique", {
instock : { type: 'boolean', required: true, defaultValue: false },
name : String,
category : String
}, {
cache: false,
validations: {
name : ORM.validators.unique({ ignoreCase: ignoreCase, scope: scope }, msg),
instock : ORM.validators.required(),
productId : ORM.validators.unique() // this must be straight after a required & validated row.
}
});
return helper.dropSync(Product, done);
};
};
describe("simple", function () {
before(setupUnique(false, false));
it("should return validation error for duplicate name", function (done) {
Product.create({name: 'fork'}, function (err, product) {
should.not.exist(err);
Product.create({name: 'fork'}, function (err, product) {
should.exist(err);
return done();
});
});
});
it("should pass with different names", function (done) {
Product.create({name: 'spatula'}, function (err, product) {
should.not.exist(err);
Product.create({name: 'plate'}, function (err, product) {
should.not.exist(err);
return done();
});
});
});
// Technically this is covered by the tests above, but I'm putting it here for clarity's sake. 3 HOURS WASTED *sigh.
it("should not leak required state from previous validation for association properties [regression test]", function (done) {
Product.create({ name: 'pencil', productId: null}, function (err, product) {
should.not.exist(err);
Product.create({ name: 'pencilcase', productId: null }, function (err, product) {
should.not.exist(err);
return done();
});
});
});
});
describe("scope", function () {
describe("to other property", function () {
before(setupUnique(false, ['category']));
it("should return validation error if other property also matches", function(done) {
Product.create({name: 'red', category: 'chair'}, function (err, product) {
should.not.exist(err);
Product.create({name: 'red', category: 'chair'}, function (err, product) {
should.exist(err);
should.equal(err.msg, 'not-unique');
return done();
});
});
});
it("should pass if other peroperty is different", function (done) {
Product.create({name: 'blue', category: 'chair'}, function (err, product) {
should.not.exist(err);
Product.create({name: 'blue', category: 'pen'}, function (err, product) {
should.not.exist(err);
return done();
});
});
});
// In SQL unique index land, NULL values are not considered equal.
it("should pass if other peroperty is null", function (done) {
Product.create({name: 'blue', category: null}, function (err, product) {
should.not.exist(err);
Product.create({name: 'blue', category: null}, function (err, product) {
should.not.exist(err);
return done();
});
});
});
});
});
describe("ignoreCase", function () {
if (protocol != 'mysql') {
it("false should do a case sensitive comparison", function (done) {
setupUnique(false, false)(function (err) {
should.not.exist(err);
Product.create({name: 'spork'}, function (err, product) {
should.not.exist(err);
Product.create({name: 'spOrk'}, function (err, product) {
should.not.exist(err);
return done();
});
});
});
});
}
it("true should do a case insensitive comparison", function (done) {
setupUnique(true, false)(function (err) {
should.not.exist(err);
Product.create({name: 'stapler'}, function (err, product) {
should.not.exist(err);
Product.create({name: 'staplER'}, function (err, product) {
should.exist(err);
should.equal(err.msg, 'not-unique');
return done();
});
});
});
});
it("true should do a case insensitive comparison on scoped properties too", function (done) {
setupUnique(true, ['category'], "name already taken for this category")(function (err) {
should.not.exist(err);
Product.create({name: 'black', category: 'pen'}, function (err, product) {
should.not.exist(err);
Product.create({name: 'Black', category: 'Pen'}, function (err, product) {
should.exist(err);
should.equal(err.msg, "name already taken for this category");
return done();
});
});
});
});
});
});
});
describe("instance.returnAllErrors = false", function() {
describe("properties.required = false", function() {
before(setup(false, false));
it("should save when properties are undefined", function(done) {
var john = new Person();
john.save(function (err) {
should.equal(err, null);
should.exist(john[Person.id]);
return done();
});
});
it("shouldn't save when a property is invalid", function(done) {
var john = new Person({ height: 4 });
john.save(function (err) {
should.notEqual(err, null);
should.equal(err.property, 'height');
should.equal(err.value, 4);
should.equal(err.msg, 'out-of-range-number');
should.equal(err.type, 'validation');
should.equal(john.id, null);
return done();
});
});
});
describe("properties.required = true", function() {
before(setup(false, true));
it("should not save when properties are null", function(done) {
var john = new Person();
john.save(function (err) {
should.notEqual(err, null);
should.equal(john.id, null);
return done();
});
});
it("should return a required error when the first property is blank", function(done) {
var john = new Person({ height: 4 });
john.save(function (err) {
should.notEqual(err, null);
should.equal(err.property, 'name');
should.equal(err.value, null);
should.equal(err.msg, 'required');
should.equal(err.type, 'validation');
should.equal(john.id, null);
return done();
});
});
});
});
describe("instance.returnAllErrors = true", function() {
describe("properties.required = false", function() {
before(setup(true, false));
it("should return all errors when a property is invalid", function(done) {
var john = new Person({ name: 'n', height: 4 });
john.save(function (err) {
should.notEqual(err, null);
should(Array.isArray(err));
should.equal(err.length, 2);
should.deepEqual(err[0], _.extend(new Error(),{
property: 'name', value: 'n', msg: 'out-of-range-length'
}));
should.deepEqual(err[1], _.extend(new Error(),{
property: 'height', value: '4', msg: 'out-of-range-number'
}));
should.equal(john.id, null);
return done();
});
});
});
describe("properties.required = true", function() {
before(setup(true, true));
it("should return required and user specified validation errors", function(done) {
var john = new Person({ height: 4 });
john.save(function (err) {
should.notEqual(err, null);
should(Array.isArray(err));
should.equal(err.length, 3);
// `type` is a non enumerable undocumented property of `Error` in V8.
should.deepEqual(err[0], _.extend(new Error(),{
property: 'name', value: undefined, msg: 'required'
}));
should.deepEqual(err[1], _.extend(new Error(),{
property: 'name', value: undefined, msg: 'undefined'
}));
should.deepEqual(err[2], _.extend(new Error(),{
property: 'height', value: '4', msg: 'out-of-range-number'
}));
should.equal(john.id, null);
return done();
});
});
});
});
describe("mockable", function() {
before(setup());
it("validate should be writable", function(done) {
var John = new Person({
name: "John"
});
var validateCalled = false;
John.validate = function(cb) {
validateCalled = true;
cb(null);
};
John.validate(function(err) {
should.equal(validateCalled,true);
return done();
});
});
});
});