Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 231 lines (169 sloc) 6.328 kb
54ddb55 @1602 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 @1602 Conditional validations
authored
17 pendingPeriod: Number
18 createdByAdmin: Boolean
54ddb55 @1602 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 @1602 Conditional validations
authored
27 createdByAdmin: false
28 createdByScript: true
54ddb55 @1602 Validations
authored
29
30 it 'should validate presence', (test) ->
8a05e1f @1602 Conditional validations
authored
31 User.validatesPresenceOf 'email', 'name'
32
54ddb55 @1602 Validations
authored
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
8a05e1f @1602 Conditional validations
authored
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
54ddb55 @1602 Validations
authored
80
81 it 'should throw error on save if required', (test) ->
82 user = new User
83
84 test.throws () ->
85 user.save throws: true
86
87 test.done()
88
89
90 it 'should allow to skip validation on save', (test) ->
91 user = new User
92 test.ok user.isNewRecord(), 'User not saved yet'
93 test.ok not user.isValid(), 'User not valid'
94
95 user.save validate: false
96
97 test.ok not user.isNewRecord(), 'User saved'
98 test.ok not user.isValid(), 'User data still not valid'
99 test.done()
100
101 it 'should perform validation on updateAttributes', (test) ->
102 User.create email: 'anatoliy@localhost', name: 'anatoliy', (err, user) ->
103 user.updateAttributes name: null, (err, name) ->
104 test.ok(err)
105 test.ok user.errors
106 test.ok user.errors.name
107 test.done()
108
109 it 'should perform validation on create', (test) ->
110 User.create (err, user) ->
111 test.ok err, 'We have an error'
112 # we got an user,
113 test.ok user, 'We got an user'
114 # but it's not saved
115 test.ok user.isNewRecord(), 'User not saved'
116 # and we have errors
117 test.ok user.errors, 'User have errors'
118 # explaining what happens
119 test.ok user.errors.name, 'Errors contain name'
120 test.ok user.errors.email, 'Errors contain email'
121
122 test.done()
123
124 it 'should validate length', (test) ->
125 User.validatesLengthOf 'password', min: 3, max: 10, allowNull: true
126 User.validatesLengthOf 'state', is: 2, allowBlank: true
127 user = new User validAttributes
128
129 user.password = 'qw'
130 test.ok not user.isValid(), 'Invalid: too short'
131 test.equal user.errors.password[0], 'too short'
132
133 user.password = '12345678901'
134 test.ok not user.isValid(), 'Invalid: too long'
135 test.equal user.errors.password[0], 'too long'
136
137 user.password = 'hello'
138 test.ok user.isValid(), 'Valid with value'
139 test.ok not user.errors
140
141 user.password = null
142 test.ok user.isValid(), 'Valid without value'
143 test.ok not user.errors
144
145 user.state = 'Texas'
146 test.ok not user.isValid(), 'Invalid state'
147 test.equal user.errors.state[0], 'length is wrong'
148
149 user.state = 'TX'
150 test.ok user.isValid(), 'Valid with value of state'
151 test.ok not user.errors
152
153 test.done()
154
155 it 'should validate numericality', (test) ->
156 User.validatesNumericalityOf 'age', int: true
157 user = new User validAttributes
158
159 user.age = '26'
160 test.ok not user.isValid(), 'User is not valid: not a number'
161 test.equal user.errors.age[0], 'is not a number'
162
163 user.age = 26.1
164 test.ok not user.isValid(), 'User is not valid: not integer'
165 test.equal user.errors.age[0], 'is not an integer'
166
167 user.age = 26
168 test.ok user.isValid(), 'User valid: integer age'
169 test.ok not user.errors
170
171 test.done()
172
173 it 'should validate inclusion', (test) ->
174 User.validatesInclusionOf 'gender', in: ['male', 'female']
175 user = new User validAttributes
176
177 user.gender = 'any'
178 test.ok not user.isValid()
179 test.equal user.errors.gender[0], 'is not included in the list'
180
181 user.gender = 'female'
182 test.ok user.isValid()
183
184 user.gender = 'male'
185 test.ok user.isValid()
186
3eb3399 @1602 Validate format, nullChecks everywhere
authored
187 user.gender = 'man'
54ddb55 @1602 Validations
authored
188 test.ok not user.isValid()
189 test.equal user.errors.gender[0], 'is not included in the list'
190
191 test.done()
192
193 it 'should validate exclusion', (test) ->
194 User.validatesExclusionOf 'domain', in: ['www', 'admin']
195 user = new User validAttributes
196
197 user.domain = 'www'
198 test.ok not user.isValid()
199 test.equal user.errors.domain[0], 'is reserved'
200
201 user.domain = 'my'
202 test.ok user.isValid()
203
204 test.done()
205
3eb3399 @1602 Validate format, nullChecks everywhere
authored
206 it 'should validate format', (test) ->
207 User.validatesFormatOf 'email', with: /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
208 user = new User validAttributes
209
210 user.email = 'invalid email'
211 test.ok not user.isValid()
212
213 user.email = 'valid@email.tld'
214 test.ok user.isValid()
215
216 test.done()
217
fa96767 @juggy Add the ability to create custom validation on fields
juggy authored
218 it 'should validate a field using a custom validator', (test)->
219
220 User.validate 'email', (err)-> err("crash") if @email.length is 0
221
222 user = new User validAttributes
223 test.ok user.isValid()
224
225 user = new User validAttributes
226 user.email = ""
227 test.ok not user.isValid()
228
229 test.done()
230
Something went wrong with that request. Please try again.