Skip to content
Newer
Older
100644 294 lines (223 sloc) 8.35 KB
54ddb55 @1602 Validations
authored Oct 10, 2011
1 juggling = require('../index')
2 Schema = juggling.Schema
3 AbstractClass = juggling.AbstractClass
4 Validatable = juggling.Validatable
5
6 require('./spec_helper').init module.exports
7
8 schema = new Schema 'memory'
9 User = schema.define 'User',
10 email: String
11 name: String
12 password: String
13 state: String
14 age: Number
15 gender: String
16 domain: String
8a05e1f @1602 Conditional validations
authored Oct 11, 2011
17 pendingPeriod: Number
18 createdByAdmin: Boolean
53f39a0 @1602 Fix unnecessary _was attributes issue, closes #36, #31, #34, #35
authored Mar 3, 2012
19 updatedAt: Date
54ddb55 @1602 Validations
authored Oct 10, 2011
20
21 validAttributes =
22 name: 'Anatoliy'
23 email: 'email@example.com'
24 state: ''
25 age: 26
26 gender: 'male'
27 domain: '1602'
8a05e1f @1602 Conditional validations
authored Oct 11, 2011
28 createdByAdmin: false
29 createdByScript: true
54ddb55 @1602 Validations
authored Oct 10, 2011
30
3b2b57e @1602 Async validations hooks
authored Nov 19, 2011
31 getValidAttributes = ->
32 name: 'Anatoliy'
33 email: 'email@example.com'
34 state: ''
35 age: 26
36 gender: 'male'
37 domain: '1602'
38 createdByAdmin: false
39 createdByScript: true
40
54ddb55 @1602 Validations
authored Oct 10, 2011
41 it 'should validate presence', (test) ->
8a05e1f @1602 Conditional validations
authored Oct 11, 2011
42 User.validatesPresenceOf 'email', 'name'
43
54ddb55 @1602 Validations
authored Oct 10, 2011
44 user = new User
45 test.ok not user.isValid(), 'User is not valid'
46 test.ok user.errors.email, 'Attr email in errors'
47 test.ok user.errors.name, 'Attr name in errors'
48
49 user.name = 'Anatoliy'
50 test.ok not user.isValid(), 'User is still not valid'
51 test.ok user.errors.email, 'Attr email still in errors'
52 test.ok not user.errors.name, 'Attr name valid'
53
54 user.email = 'anatoliy@localhost'
55 test.ok user.isValid(), 'User is valid'
56 test.ok not user.errors, 'No errors'
57 test.ok not user.errors.email, 'Attr email valid'
58 test.ok not user.errors.name, 'Attr name valid'
59 test.done()
60
8a05e1f @1602 Conditional validations
authored Oct 11, 2011
61 it 'should allow to skip validations', (test) ->
62 User.validatesPresenceOf 'pendingPeriod', if: 'createdByAdmin'
63 User.validatesLengthOf 'domain', is: 2, unless: 'createdByScript'
64
65 user = new User validAttributes
66 test.ok user.isValid()
67
68 user.createdByAdmin = true
69 test.ok not user.isValid()
70 test.ok user.errors.pendingPeriod.length
71
72 user.pendingPeriod = 1
73 test.ok user.isValid()
74
75 user.createdByScript = false
76 test.ok not user.isValid()
77 test.ok user.errors.domain.length
78
79 user.domain = '12'
80 test.ok user.isValid()
81
82 User.validatesLengthOf 'domain', is: 3, unless: -> @domain != 'xyz'
83 test.ok user.isValid()
84
85 user.domain = 'xyz'
86 test.ok not user.isValid() # is: 3 passed, but is: 2 failed
87
88
89 test.done()
90
54ddb55 @1602 Validations
authored Oct 10, 2011
91 it 'should throw error on save if required', (test) ->
92 user = new User
93
94 test.throws () ->
95 user.save throws: true
96
97 test.done()
98
99
100 it 'should allow to skip validation on save', (test) ->
101 user = new User
102 test.ok user.isNewRecord(), 'User not saved yet'
103 test.ok not user.isValid(), 'User not valid'
104
105 user.save validate: false
106
107 test.ok not user.isNewRecord(), 'User saved'
108 test.ok not user.isValid(), 'User data still not valid'
109 test.done()
110
111 it 'should perform validation on updateAttributes', (test) ->
112 User.create email: 'anatoliy@localhost', name: 'anatoliy', (err, user) ->
113 user.updateAttributes name: null, (err, name) ->
114 test.ok(err)
115 test.ok user.errors
116 test.ok user.errors.name
117 test.done()
118
119 it 'should perform validation on create', (test) ->
120 User.create (err, user) ->
121 test.ok err, 'We have an error'
122 # we got an user,
123 test.ok user, 'We got an user'
124 # but it's not saved
125 test.ok user.isNewRecord(), 'User not saved'
126 # and we have errors
127 test.ok user.errors, 'User have errors'
128 # explaining what happens
129 test.ok user.errors.name, 'Errors contain name'
130 test.ok user.errors.email, 'Errors contain email'
131
132 test.done()
133
134 it 'should validate length', (test) ->
135 User.validatesLengthOf 'password', min: 3, max: 10, allowNull: true
136 User.validatesLengthOf 'state', is: 2, allowBlank: true
137 user = new User validAttributes
138
139 user.password = 'qw'
140 test.ok not user.isValid(), 'Invalid: too short'
141 test.equal user.errors.password[0], 'too short'
142
143 user.password = '12345678901'
144 test.ok not user.isValid(), 'Invalid: too long'
145 test.equal user.errors.password[0], 'too long'
146
147 user.password = 'hello'
148 test.ok user.isValid(), 'Valid with value'
149 test.ok not user.errors
150
151 user.password = null
152 test.ok user.isValid(), 'Valid without value'
153 test.ok not user.errors
154
155 user.state = 'Texas'
156 test.ok not user.isValid(), 'Invalid state'
157 test.equal user.errors.state[0], 'length is wrong'
158
159 user.state = 'TX'
160 test.ok user.isValid(), 'Valid with value of state'
161 test.ok not user.errors
162
163 test.done()
164
165 it 'should validate numericality', (test) ->
166 User.validatesNumericalityOf 'age', int: true
167 user = new User validAttributes
168
169 user.age = '26'
170 test.ok not user.isValid(), 'User is not valid: not a number'
171 test.equal user.errors.age[0], 'is not a number'
172
173 user.age = 26.1
174 test.ok not user.isValid(), 'User is not valid: not integer'
175 test.equal user.errors.age[0], 'is not an integer'
176
177 user.age = 26
178 test.ok user.isValid(), 'User valid: integer age'
179 test.ok not user.errors
180
181 test.done()
182
183 it 'should validate inclusion', (test) ->
184 User.validatesInclusionOf 'gender', in: ['male', 'female']
185 user = new User validAttributes
186
187 user.gender = 'any'
188 test.ok not user.isValid()
189 test.equal user.errors.gender[0], 'is not included in the list'
190
191 user.gender = 'female'
192 test.ok user.isValid()
193
194 user.gender = 'male'
195 test.ok user.isValid()
196
3eb3399 @1602 Validate format, nullChecks everywhere
authored Oct 10, 2011
197 user.gender = 'man'
54ddb55 @1602 Validations
authored Oct 10, 2011
198 test.ok not user.isValid()
199 test.equal user.errors.gender[0], 'is not included in the list'
200
201 test.done()
202
203 it 'should validate exclusion', (test) ->
204 User.validatesExclusionOf 'domain', in: ['www', 'admin']
205 user = new User validAttributes
206
207 user.domain = 'www'
208 test.ok not user.isValid()
209 test.equal user.errors.domain[0], 'is reserved'
210
211 user.domain = 'my'
212 test.ok user.isValid()
213
214 test.done()
215
3eb3399 @1602 Validate format, nullChecks everywhere
authored Oct 10, 2011
216 it 'should validate format', (test) ->
217 User.validatesFormatOf 'email', with: /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
218 user = new User validAttributes
219
220 user.email = 'invalid email'
221 test.ok not user.isValid()
222
223 user.email = 'valid@email.tld'
224 test.ok user.isValid()
225
226 test.done()
227
9dcd976 @1602 Rewrite custom validation
authored Nov 19, 2011
228 it 'should validate a field using a custom validator', (test) ->
dd1e54f @1602 Some coding style fixes
authored Nov 17, 2011
229
9dcd976 @1602 Rewrite custom validation
authored Nov 18, 2011
230 validator = (err) ->
231 err('crash') if @name == 'bad name'
232
233 User.validate 'name', validator, message: crash: 'custom message'
fa96767 @juggy Add the ability to create custom validation on fields
juggy authored Nov 16, 2011
234
235 user = new User validAttributes
236 test.ok user.isValid()
237
238 user = new User validAttributes
9dcd976 @1602 Rewrite custom validation
authored Nov 18, 2011
239 user.name = 'bad name'
240 test.ok not user.isValid(), 'invalid due custom name validator'
241 test.equal user.errors.name[0], 'custom message'
fa96767 @juggy Add the ability to create custom validation on fields
juggy authored Nov 17, 2011
242
243 test.done()
dd1e54f @1602 Some coding style fixes
authored Nov 17, 2011
244
fbf1186 @1602 Async validations
authored Nov 19, 2011
245 it 'should validate asynchronously', (test) ->
246
247 validator = (err, done) ->
248 setTimeout =>
249 err 'async' if @name == 'bad name'
250 done()
53f39a0 @1602 Fix unnecessary _was attributes issue, closes #36, #31, #34, #35
authored Mar 3, 2012
251 , 10
fbf1186 @1602 Async validations
authored Nov 19, 2011
252
253 User.validateAsync 'name', validator, message: async: 'hello'
254
255 user = new User validAttributes
256 test.ok not user.isValid(), 'not valid because async validation'
257 user.isValid (valid) ->
258 test.ok valid, 'valid name'
259
260 user.name = 'bad name'
261 user.isValid (valid) ->
262 test.ok not valid, 'not valid name'
263 test.done()
264
3b2b57e @1602 Async validations hooks
authored Nov 19, 2011
265 it 'should validate uniqueness', (test) ->
266 User.validatesUniquenessOf 'email'
d7fbf1a @1602 Tune async validation hooks
authored Nov 20, 2011
267 User.create getValidAttributes(), (err, user) ->
3b2b57e @1602 Async validations hooks
authored Nov 19, 2011
268 user = new User getValidAttributes()
269
270 # test.ok not user.isValid(), 'not valid because async validation'
271 user.isValid (valid) ->
272 test.ok not valid, 'email is not unique'
273 user.email = 'unique@email.tld'
274 user.isValid (valid) ->
275 test.ok valid, 'valid with unique email'
53f39a0 @1602 Fix unnecessary _was attributes issue, closes #36, #31, #34, #35
authored Mar 3, 2012
276 user.save (err) ->
277 test.ok not user.propertyChanged('email'), 'Email changed'
278 user.updateAttributes { updatedAt: new Date, createdByAdmin: false }, (err) ->
279 User.all where: email: 'unique@email.tld', (err, users) ->
280 test.ok users[0]
281 test.ok users[0].email == 'unique@email.tld'
282 test.ok !err, 'Updated'
283 test.done()
8b720c3 @1602 Keep dirty state for cached objects
authored Nov 23, 2011
284
285 it 'should save dirty state when validating uniqueness', (test) ->
53f39a0 @1602 Fix unnecessary _was attributes issue, closes #36, #31, #34, #35
authored Mar 3, 2012
286 User.all where: email: 'unique@email.tld', (err, users) ->
8b720c3 @1602 Keep dirty state for cached objects
authored Nov 23, 2011
287 u = users[0]
288 u.name = 'Hulk'
289 u.isValid (valid) ->
53f39a0 @1602 Fix unnecessary _was attributes issue, closes #36, #31, #34, #35
authored Mar 3, 2012
290 test.ok valid, 'Invalid user'
8b720c3 @1602 Keep dirty state for cached objects
authored Nov 23, 2011
291 test.equal u.name, 'Hulk'
292 test.done()
53f39a0 @1602 Fix unnecessary _was attributes issue, closes #36, #31, #34, #35
authored Mar 3, 2012
293
Something went wrong with that request. Please try again.