Skip to content
This repository
Newer
Older
100644 230 lines (169 sloc) 6.319 kb
54ddb55c »
2011-10-10 Validations
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
8a05e1ff »
2011-10-11 Conditional validations
17 pendingPeriod: Number
18 createdByAdmin: Boolean
54ddb55c »
2011-10-10 Validations
19
20 validAttributes =
21 name: 'Anatoliy'
22 email: 'email@example.com'
23 state: ''
24 age: 26
25 gender: 'male'
26 domain: '1602'
8a05e1ff »
2011-10-11 Conditional validations
27 createdByAdmin: false
28 createdByScript: true
54ddb55c »
2011-10-10 Validations
29
30 it 'should validate presence', (test) ->
8a05e1ff »
2011-10-11 Conditional validations
31 User.validatesPresenceOf 'email', 'name'
32
54ddb55c »
2011-10-10 Validations
33 user = new User
34 test.ok not user.isValid(), 'User is not valid'
35 test.ok user.errors.email, 'Attr email in errors'
36 test.ok user.errors.name, 'Attr name in errors'
37
38 user.name = 'Anatoliy'
39 test.ok not user.isValid(), 'User is still not valid'
40 test.ok user.errors.email, 'Attr email still in errors'
41 test.ok not user.errors.name, 'Attr name valid'
42
43 user.email = 'anatoliy@localhost'
44 test.ok user.isValid(), 'User is valid'
45 test.ok not user.errors, 'No errors'
46 test.ok not user.errors.email, 'Attr email valid'
47 test.ok not user.errors.name, 'Attr name valid'
48 test.done()
49
8a05e1ff »
2011-10-11 Conditional validations
50 it 'should allow to skip validations', (test) ->
51 User.validatesPresenceOf 'pendingPeriod', if: 'createdByAdmin'
52 User.validatesLengthOf 'domain', is: 2, unless: 'createdByScript'
53
54 user = new User validAttributes
55 test.ok user.isValid()
56
57 user.createdByAdmin = true
58 test.ok not user.isValid()
59 test.ok user.errors.pendingPeriod.length
60
61 user.pendingPeriod = 1
62 test.ok user.isValid()
63
64 user.createdByScript = false
65 test.ok not user.isValid()
66 test.ok user.errors.domain.length
67
68 user.domain = '12'
69 test.ok user.isValid()
70
71 User.validatesLengthOf 'domain', is: 3, unless: -> @domain != 'xyz'
72 test.ok user.isValid()
73
74 user.domain = 'xyz'
75 test.ok not user.isValid() # is: 3 passed, but is: 2 failed
76
77
78 test.done()
79
54ddb55c »
2011-10-10 Validations
80 it 'should throw error on save if required', (test) ->
81 user = new User
82
83 test.throws () ->
84 user.save throws: true
85
86 test.done()
87
88
89 it 'should allow to skip validation on save', (test) ->
90 user = new User
91 test.ok user.isNewRecord(), 'User not saved yet'
92 test.ok not user.isValid(), 'User not valid'
93
94 user.save validate: false
95
96 test.ok not user.isNewRecord(), 'User saved'
97 test.ok not user.isValid(), 'User data still not valid'
98 test.done()
99
100 it 'should perform validation on updateAttributes', (test) ->
101 User.create email: 'anatoliy@localhost', name: 'anatoliy', (err, user) ->
102 user.updateAttributes name: null, (err, name) ->
103 test.ok(err)
104 test.ok user.errors
105 test.ok user.errors.name
106 test.done()
107
108 it 'should perform validation on create', (test) ->
109 User.create (err, user) ->
110 test.ok err, 'We have an error'
111 # we got an user,
112 test.ok user, 'We got an user'
113 # but it's not saved
114 test.ok user.isNewRecord(), 'User not saved'
115 # and we have errors
116 test.ok user.errors, 'User have errors'
117 # explaining what happens
118 test.ok user.errors.name, 'Errors contain name'
119 test.ok user.errors.email, 'Errors contain email'
120
121 test.done()
122
123 it 'should validate length', (test) ->
124 User.validatesLengthOf 'password', min: 3, max: 10, allowNull: true
125 User.validatesLengthOf 'state', is: 2, allowBlank: true
126 user = new User validAttributes
127
128 user.password = 'qw'
129 test.ok not user.isValid(), 'Invalid: too short'
130 test.equal user.errors.password[0], 'too short'
131
132 user.password = '12345678901'
133 test.ok not user.isValid(), 'Invalid: too long'
134 test.equal user.errors.password[0], 'too long'
135
136 user.password = 'hello'
137 test.ok user.isValid(), 'Valid with value'
138 test.ok not user.errors
139
140 user.password = null
141 test.ok user.isValid(), 'Valid without value'
142 test.ok not user.errors
143
144 user.state = 'Texas'
145 test.ok not user.isValid(), 'Invalid state'
146 test.equal user.errors.state[0], 'length is wrong'
147
148 user.state = 'TX'
149 test.ok user.isValid(), 'Valid with value of state'
150 test.ok not user.errors
151
152 test.done()
153
154 it 'should validate numericality', (test) ->
155 User.validatesNumericalityOf 'age', int: true
156 user = new User validAttributes
157
158 user.age = '26'
159 test.ok not user.isValid(), 'User is not valid: not a number'
160 test.equal user.errors.age[0], 'is not a number'
161
162 user.age = 26.1
163 test.ok not user.isValid(), 'User is not valid: not integer'
164 test.equal user.errors.age[0], 'is not an integer'
165
166 user.age = 26
167 test.ok user.isValid(), 'User valid: integer age'
168 test.ok not user.errors
169
170 test.done()
171
172 it 'should validate inclusion', (test) ->
173 User.validatesInclusionOf 'gender', in: ['male', 'female']
174 user = new User validAttributes
175
176 user.gender = 'any'
177 test.ok not user.isValid()
178 test.equal user.errors.gender[0], 'is not included in the list'
179
180 user.gender = 'female'
181 test.ok user.isValid()
182
183 user.gender = 'male'
184 test.ok user.isValid()
185
3eb33998 »
2011-10-10 Validate format, nullChecks everywhere
186 user.gender = 'man'
54ddb55c »
2011-10-10 Validations
187 test.ok not user.isValid()
188 test.equal user.errors.gender[0], 'is not included in the list'
189
190 test.done()
191
192 it 'should validate exclusion', (test) ->
193 User.validatesExclusionOf 'domain', in: ['www', 'admin']
194 user = new User validAttributes
195
196 user.domain = 'www'
197 test.ok not user.isValid()
198 test.equal user.errors.domain[0], 'is reserved'
199
200 user.domain = 'my'
201 test.ok user.isValid()
202
203 test.done()
204
3eb33998 »
2011-10-10 Validate format, nullChecks everywhere
205 it 'should validate format', (test) ->
206 User.validatesFormatOf 'email', with: /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
207 user = new User validAttributes
208
209 user.email = 'invalid email'
210 test.ok not user.isValid()
211
212 user.email = 'valid@email.tld'
213 test.ok user.isValid()
214
215 test.done()
216
fa967677 »
2011-11-16 Add the ability to create custom validation on fields
217 it 'should validate a field using a custom validator', (test)->
dd1e54ff »
2011-11-17 Some coding style fixes
218
fa967677 »
2011-11-16 Add the ability to create custom validation on fields
219 User.validate 'email', (err)-> err("crash") if @email.length is 0
220
221 user = new User validAttributes
222 test.ok user.isValid()
223
224 user = new User validAttributes
225 user.email = ""
226 test.ok not user.isValid()
227
228 test.done()
dd1e54ff »
2011-11-17 Some coding style fixes
229
Something went wrong with that request. Please try again.