@@ -4,8 +4,10 @@ import v from "src/utils/validators";
4
4
5
5
function check ( validator , value , field , errorCount ) {
6
6
let res = validator ( value , field ) ;
7
- expect ( res ) . to . be . instanceof ( Array ) ;
8
- expect ( res ) . to . be . length ( errorCount ) ;
7
+ if ( errorCount > 0 || res != undefined ) {
8
+ expect ( res ) . to . be . instanceof ( Array ) ;
9
+ expect ( res ) . to . be . length ( errorCount ) ;
10
+ }
9
11
return res ;
10
12
}
11
13
@@ -107,6 +109,12 @@ describe("Validators", () => {
107
109
it ( "should give error if value is greater than max" , ( ) => {
108
110
check ( v . string , "abcdefghijklmnop" , field , 1 ) ;
109
111
} ) ;
112
+
113
+ it ( "should give error if value is not string" , ( ) => {
114
+ check ( v . string , 123 , field , 1 ) ;
115
+ check ( v . string , true , field , 1 ) ;
116
+ check ( v . string , [ ] , field , 1 ) ;
117
+ } ) ;
110
118
111
119
it ( "should not give error" , ( ) => {
112
120
check ( v . string , "Foo" , field , 0 ) ;
@@ -148,6 +156,190 @@ describe("Validators", () => {
148
156
check ( v . date , "2100-04-05" , field , 1 ) ;
149
157
} ) ;
150
158
159
+ it ( "should give error if value is not a date" , ( ) => {
160
+ check ( v . date , "Foo" , field , 1 ) ;
161
+ check ( v . date , true , field , 1 ) ;
162
+ } ) ;
163
+
164
+ it ( "should not give error if value is null and field is not required" , ( ) => {
165
+ field . required = false ;
166
+ check ( v . date , null , field , 0 ) ;
167
+ } ) ;
168
+
169
+ } ) ;
170
+
171
+ describe ( "test Validators.regexp" , ( ) => {
172
+
173
+ let field = {
174
+ required : true ,
175
+ pattern : / ^ [ a - z 0 - 9 - ] + $ / g
176
+ }
177
+
178
+ it ( "should give error if value is null, but field is required" , ( ) => {
179
+ check ( v . regexp , null , field , 1 ) ;
180
+ } ) ;
181
+
182
+ it ( "should give error if value is not matched the pattern" , ( ) => {
183
+ check ( v . regexp , "ABCD" , field , 1 ) ;
184
+ check ( v . regexp , "12 34" , field , 1 ) ;
185
+ check ( v . regexp , "555+666" , field , 1 ) ;
186
+ } ) ;
187
+
188
+ it ( "should not give error" , ( ) => {
189
+ check ( v . regexp , "foo-bar" , field , 0 ) ;
190
+ check ( v . regexp , "john-doe-123" , field , 0 ) ;
191
+ } ) ;
192
+
193
+ it ( "should not give error if value is null and field is not required" , ( ) => {
194
+ field . required = false ;
195
+ check ( v . regexp , null , field , 0 ) ;
196
+ } ) ;
197
+ } ) ;
198
+
199
+ describe ( "test Validators.email" , ( ) => {
200
+
201
+ let field = { required : true } ;
202
+
203
+ it ( "should give error if value is null, but field is required" , ( ) => {
204
+ check ( v . email , null , field , 1 ) ;
205
+ } ) ;
206
+
207
+ it ( "should give error if value is not matched the pattern" , ( ) => {
208
+ check ( v . email , "abcdefg" , field , 1 ) ;
209
+ check ( v . email , "1234" , field , 1 ) ;
210
+ check ( v . email , "abc@gmail" , field , 1 ) ;
211
+ check ( v . email , "@gmail.com" , field , 1 ) ;
212
+ } ) ;
213
+
214
+ it ( "should not give error" , ( ) => {
215
+ check ( v . email , "[email protected] " , field , 0 ) ;
216
+ check ( v . email , "[email protected] " , field , 0 ) ;
217
+ } ) ;
218
+
219
+ it ( "should not give error if value is null and field is not required" , ( ) => {
220
+ field . required = false ;
221
+ check ( v . email , null , field , 0 ) ;
222
+ } ) ;
223
+ } ) ;
224
+
225
+
226
+ describe ( "test Validators.url" , ( ) => {
227
+
228
+ let field = { required : true } ;
229
+
230
+ it ( "should give error if value is null, but field is required" , ( ) => {
231
+ check ( v . url , null , field , 1 ) ;
232
+ } ) ;
233
+
234
+ it ( "should give error if value is not matched the pattern" , ( ) => {
235
+ check ( v . url , "abcdefg" , field , 1 ) ;
236
+ check ( v . url , "1234.c" , field , 1 ) ;
237
+ check ( v . url , "gmail.company1234" , field , 1 ) ;
238
+ check ( v . url , "@gmail.com" , field , 1 ) ;
239
+ } ) ;
240
+
241
+ it ( "should not give error" , ( ) => {
242
+ check ( v . url , "http://www.google.com" , field , 0 ) ;
243
+ check ( v . url , "http://nasa.gov" , field , 0 ) ;
244
+ check ( v . url , "http://github.com" , field , 0 ) ;
245
+ check ( v . url , "http://github.com/icebob/vue-form-generator" , field , 0 ) ;
246
+ } ) ;
247
+
248
+ it ( "should not give error if value is null and field is not required" , ( ) => {
249
+ field . required = false ;
250
+ check ( v . url , null , field , 0 ) ;
251
+ } ) ;
252
+ } ) ;
253
+
254
+ describe ( "test Validators.creditCard" , ( ) => {
255
+
256
+ let field = { required : true } ;
257
+
258
+ it ( "should give error if value is null, but field is required" , ( ) => {
259
+ check ( v . creditCard , null , field , 1 ) ;
260
+ } ) ;
261
+
262
+ it ( "should give error if value is not matched the pattern" , ( ) => {
263
+ check ( v . creditCard , "12345679" , field , 1 ) ;
264
+ check ( v . creditCard , "4556778266680579000" , field , 1 ) ;
265
+ } ) ;
266
+
267
+ it ( "should not give error" , ( ) => {
268
+ check ( v . creditCard , "4556778266680579" , field , 0 ) ; // Visa
269
+ check ( v . creditCard , "5491345312191350" , field , 0 ) ; // Mastercard
270
+ check ( v . creditCard , "6011319767119926" , field , 0 ) ; // Discover
271
+ check ( v . creditCard , "343811242956601" , field , 0 ) ; // American Express
272
+ } ) ;
273
+
274
+ it ( "should not give error if value is null and field is not required" , ( ) => {
275
+ field . required = false ;
276
+ check ( v . creditCard , null , field , 0 ) ;
277
+ } ) ;
278
+ } ) ;
279
+
280
+ describe ( "test Validators.alpha" , ( ) => {
281
+
282
+ let field = {
283
+ required : true
284
+ }
285
+
286
+ it ( "should give error if value is null, but field is required" , ( ) => {
287
+ check ( v . alpha , null , field , 1 ) ;
288
+ } ) ;
289
+
290
+ it ( "should give error if value is not alpha" , ( ) => {
291
+ check ( v . alpha , "Abc5" , field , 1 ) ;
292
+ check ( v . alpha , "$Abc" , field , 1 ) ;
293
+ check ( v . alpha , "john.doe" , field , 1 ) ;
294
+ check ( v . alpha , "john_doe" , field , 1 ) ;
295
+ check ( v . alpha , 512 , field , 1 ) ;
296
+ } ) ;
297
+
298
+ it ( "should not give error" , ( ) => {
299
+ check ( v . alpha , "F" , field , 0 ) ;
300
+ check ( v . alpha , "Foo" , field , 0 ) ;
301
+ check ( v . alpha , "Foobar" , field , 0 ) ;
302
+ check ( v . alpha , "JohnDoe" , field , 0 ) ;
303
+ check ( v . alpha , "FoobarWithoutNumber" , field , 0 ) ;
304
+ } ) ;
305
+
306
+ it ( "should not give error if value is null and field is not required" , ( ) => {
307
+ field . required = false ;
308
+ check ( v . alpha , null , field , 0 ) ;
309
+ } ) ;
151
310
} ) ;
152
311
312
+ describe ( "test Validators.alphaNumeric" , ( ) => {
313
+
314
+ let field = {
315
+ required : true
316
+ }
317
+
318
+ it ( "should give error if value is null, but field is required" , ( ) => {
319
+ check ( v . alphaNumeric , null , field , 1 ) ;
320
+ } ) ;
321
+
322
+ it ( "should give error if value is not alphaNumeric" , ( ) => {
323
+ check ( v . alphaNumeric , "Abc-5" , field , 1 ) ;
324
+ check ( v . alphaNumeric , "$Abc" , field , 1 ) ;
325
+ check ( v . alphaNumeric , "john.doe" , field , 1 ) ;
326
+ check ( v . alphaNumeric , "john_doe" , field , 1 ) ;
327
+ } ) ;
328
+
329
+ it ( "should not give error" , ( ) => {
330
+ check ( v . alphaNumeric , "F" , field , 0 ) ;
331
+ check ( v . alphaNumeric , "Foo" , field , 0 ) ;
332
+ check ( v . alphaNumeric , "Foobar" , field , 0 ) ;
333
+ check ( v . alphaNumeric , "Foobar555" , field , 0 ) ;
334
+ check ( v . alphaNumeric , "JohnDoe" , field , 0 ) ;
335
+ check ( v . alphaNumeric , "FoobarWithoutNumber" , field , 0 ) ;
336
+ check ( v . alphaNumeric , "100" , field , 0 ) ;
337
+ check ( v . alphaNumeric , 512 , field , 0 ) ;
338
+ } ) ;
339
+
340
+ it ( "should not give error if value is null and field is not required" , ( ) => {
341
+ field . required = false ;
342
+ check ( v . alphaNumeric , null , field , 0 ) ;
343
+ } ) ;
344
+ } ) ;
153
345
} ) ;
0 commit comments