Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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