/
validations.test.js
201 lines (169 loc) · 6.02 KB
/
validations.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
// This test written in mocha+should.js
var should = require('./init.js');
var j = require('../'), db, User;
var ValidationError = require('../lib/validations.js').ValidationError;
function getValidAttributes() {
return {
name: 'Anatoliy',
email: 'email@example.com',
state: '',
age: 26,
gender: 'male',
createdByAdmin: false,
createdByScript: true
};
}
describe('validations', function() {
before(function(done) {
db = getSchema();
User = db.define('User', {
email: String,
name: String,
password: String,
state: String,
age: Number,
gender: String,
domain: String,
pendingPeriod: Number,
createdByAdmin: Boolean,
createdByScript: Boolean,
updatedAt: Date
});
db.automigrate(done);
});
beforeEach(function(done) {
User.destroyAll(function() {
delete User._validations;
done();
});
});
after(function() {
db.disconnect();
});
describe('commons', function() {
describe('skipping', function() {
it('should allow to skip using if: attribute', function() {
User.validatesPresenceOf('pendingPeriod', {if: 'createdByAdmin'});
var user = new User;
user.createdByAdmin = true;
user.isValid().should.be.false;
user.errors.pendingPeriod.should.eql(['can\'t be blank']);
user.pendingPeriod = 1
user.isValid().should.be.true;
});
});
describe('lifecycle', function() {
it('should work on create', function(done) {
delete User._validations;
User.validatesPresenceOf('name');
User.create(function(e, u) {
should.exist(e);
User.create({name: 'Valid'}, function(e, d) {
should.not.exist(e);
done();
});
});
});
it('should work on update', function(done) {
delete User._validations;
User.validatesPresenceOf('name');
User.create({name: 'Valid'}, function(e, d) {
d.updateAttribute('name', null, function(e) {
should.exist(e);
e.should.be.instanceOf(Error);
e.should.be.instanceOf(ValidationError);
d.updateAttribute('name', 'Vasiliy', function(e) {
should.not.exist(e);
done();
});
})
});
});
it('should return error code', function(done) {
delete User._validations;
User.validatesPresenceOf('name');
User.create(function(e, u) {
should.exist(e);
e.codes.name.should.eql(['presence']);
done();
});
});
it('should allow to modify error after validation', function(done) {
User.afterValidate = function(next) {
next();
};
});
});
});
describe('presence', function() {
it('should validate presence', function() {
User.validatesPresenceOf('name', 'email');
var u = new User;
u.isValid().should.not.be.true;
u.name = 1;
u.email = 2;
u.isValid().should.be.true;
});
it('should skip validation by property (if/unless)', function() {
User.validatesPresenceOf('domain', {unless: 'createdByScript'});
var user = new User(getValidAttributes())
user.isValid().should.be.true;
user.createdByScript = false;
user.isValid().should.be.false;
user.errors.domain.should.eql(['can\'t be blank']);
user.domain = 'domain';
user.isValid().should.be.true;
});
});
describe('uniqueness', function() {
it('should validate uniqueness', function(done) {
User.validatesUniquenessOf('email');
var u = new User({email: 'hey'});
Boolean(u.isValid(function(valid) {
valid.should.be.true;
u.save(function() {
var u2 = new User({email: 'hey'});
u2.isValid(function(valid) {
valid.should.be.false;
done();
});
});
})).should.be.false;
});
it('should handle same object modification', function(done) {
User.validatesUniquenessOf('email');
var u = new User({email: 'hey'});
Boolean(u.isValid(function(valid) {
valid.should.be.true;
u.save(function() {
u.name = 'Goghi';
u.isValid(function(valid) {
valid.should.be.true;
u.save(done);
});
});
// async validations always falsy when called as sync
})).should.not.be.ok;
});
});
describe('format', function() {
it('should validate format');
it('should overwrite default blank message with custom format message');
});
describe('numericality', function() {
it('should validate numericality');
});
describe('inclusion', function() {
it('should validate inclusion');
});
describe('exclusion', function() {
it('should validate exclusion');
});
describe('length', function() {
it('should validate length');
});
describe('custom', function() {
it('should validate using custom sync validation');
it('should validate using custom async validation');
});
});