@@ -15,6 +15,7 @@ import {Table} from "../../lib/annotations/Table";
15
15
import { Column } from "../../lib/annotations/Column" ;
16
16
import { Length } from "../../lib/annotations/validation/Length" ;
17
17
import { NotEmpty } from "../../lib/annotations/validation/NotEmpty" ;
18
+ import { Validator } from '../../lib/annotations/validation/Validator' ;
18
19
19
20
use ( chaiAsPromised ) ;
20
21
@@ -209,6 +210,7 @@ describe('validation', () => {
209
210
class User extends Model < User > {
210
211
@Length ( { min : 0 , max : 5 } ) @Column name : string ;
211
212
}
213
+
212
214
const sequelizeValidationOnly = createSequelizeValidationOnly ( false ) ;
213
215
sequelizeValidationOnly . addModels ( [ User ] ) ;
214
216
const user = new User ( { name : 'elisa' } ) ;
@@ -225,6 +227,7 @@ describe('validation', () => {
225
227
class User extends Model < User > {
226
228
@Length ( { min : 0 , max : 5 } ) @Column name : string ;
227
229
}
230
+
228
231
const sequelizeValidationOnly = createSequelizeValidationOnly ( false ) ;
229
232
sequelizeValidationOnly . addModels ( [ User ] ) ;
230
233
const user = new User ( { name : 'elisa tree' } ) ;
@@ -241,6 +244,7 @@ describe('validation', () => {
241
244
class User extends Model < User > {
242
245
@Length ( { min : 5 , max : 5 } ) @Column name : string ;
243
246
}
247
+
244
248
const sequelizeValidationOnly = createSequelizeValidationOnly ( false ) ;
245
249
sequelizeValidationOnly . addModels ( [ User ] ) ;
246
250
const user = new User ( { name : 'elli' } ) ;
@@ -257,6 +261,7 @@ describe('validation', () => {
257
261
class User extends Model < User > {
258
262
@Length ( { max : 5 } ) @Column name : string ;
259
263
}
264
+
260
265
const sequelizeValidationOnly = createSequelizeValidationOnly ( false ) ;
261
266
sequelizeValidationOnly . addModels ( [ User ] ) ;
262
267
const user = new User ( { name : 'elisa' } ) ;
@@ -273,6 +278,7 @@ describe('validation', () => {
273
278
class User extends Model < User > {
274
279
@Length ( { max : 5 } ) @Column name : string ;
275
280
}
281
+
276
282
const sequelizeValidationOnly = createSequelizeValidationOnly ( false ) ;
277
283
sequelizeValidationOnly . addModels ( [ User ] ) ;
278
284
const user = new User ( { name : 'elisa tree' } ) ;
@@ -289,6 +295,7 @@ describe('validation', () => {
289
295
class User extends Model < User > {
290
296
@Length ( { min : 4 } ) @Column name : string ;
291
297
}
298
+
292
299
const sequelizeValidationOnly = createSequelizeValidationOnly ( false ) ;
293
300
sequelizeValidationOnly . addModels ( [ User ] ) ;
294
301
const user = new User ( { name : 'elisa' } ) ;
@@ -305,6 +312,7 @@ describe('validation', () => {
305
312
class User extends Model < User > {
306
313
@Length ( { min : 5 } ) @Column name : string ;
307
314
}
315
+
308
316
const sequelizeValidationOnly = createSequelizeValidationOnly ( false ) ;
309
317
sequelizeValidationOnly . addModels ( [ User ] ) ;
310
318
const user = new User ( { name : 'elli' } ) ;
@@ -325,6 +333,7 @@ describe('validation', () => {
325
333
class User extends Model < User > {
326
334
@NotEmpty @Column name : string ;
327
335
}
336
+
328
337
const sequelizeValidationOnly = createSequelizeValidationOnly ( false ) ;
329
338
sequelizeValidationOnly . addModels ( [ User ] ) ;
330
339
const user = new User ( { name : 'elisa' } ) ;
@@ -341,6 +350,7 @@ describe('validation', () => {
341
350
class User extends Model < User > {
342
351
@NotEmpty @Column name : string ;
343
352
}
353
+
344
354
const sequelizeValidationOnly = createSequelizeValidationOnly ( false ) ;
345
355
sequelizeValidationOnly . addModels ( [ User ] ) ;
346
356
const user = new User ( { name : '' } ) ;
@@ -357,6 +367,7 @@ describe('validation', () => {
357
367
class User extends Model < User > {
358
368
@NotEmpty ( { msg : 'NotEmpty' } ) @Column name : string ;
359
369
}
370
+
360
371
const sequelizeValidationOnly = createSequelizeValidationOnly ( false ) ;
361
372
sequelizeValidationOnly . addModels ( [ User ] ) ;
362
373
const user = new User ( { name : 'elisa' } ) ;
@@ -373,6 +384,7 @@ describe('validation', () => {
373
384
class User extends Model < User > {
374
385
@NotEmpty ( { msg : 'NotEmpty' } ) @Column name : string ;
375
386
}
387
+
376
388
const sequelizeValidationOnly = createSequelizeValidationOnly ( false ) ;
377
389
sequelizeValidationOnly . addModels ( [ User ] ) ;
378
390
const user = new User ( { name : '' } ) ;
@@ -385,6 +397,114 @@ describe('validation', () => {
385
397
} ) ;
386
398
} ) ;
387
399
400
+ describe ( 'Validator' , ( ) => {
401
+
402
+ describe ( 'simple model, one validator' , ( ) => {
403
+
404
+ const VALID_NAME = 'bob' ;
405
+ const ERROR_MESSAGE = `Invalid name: Only '${ VALID_NAME } ' is valid` ;
406
+ const _sequelize = createSequelize ( { modelPaths : [ ] } ) ;
407
+
408
+ @Table
409
+ class User extends Model < User > {
410
+ @Column name : string ;
411
+ @Validator userValidator ( ) : void {
412
+ if ( this . name !== VALID_NAME ) {
413
+ throw new Error ( ERROR_MESSAGE ) ;
414
+ }
415
+ }
416
+ }
417
+
418
+ _sequelize . addModels ( [ User ] ) ;
419
+
420
+ it ( 'should throw' , ( ) => {
421
+ const user = new User ( { name : 'will' } ) ;
422
+
423
+ if ( majorVersion === 3 ) {
424
+ return user . validate ( ) . then ( err => expect ( err . errors [ 0 ] . message ) . to . eq ( ERROR_MESSAGE ) ) ;
425
+ } else if ( majorVersion === 4 ) {
426
+ return expect ( user . validate ( ) ) . to . be . rejected ;
427
+ }
428
+ } ) ;
429
+
430
+ it ( 'should not throw' , ( ) => {
431
+ const user = new User ( { name : VALID_NAME } ) ;
432
+
433
+ if ( majorVersion === 3 ) {
434
+ return user . validate ( ) . then ( err => expect ( err ) . to . be . null ) ;
435
+ } else if ( majorVersion === 4 ) {
436
+ return expect ( user . validate ( ) ) . to . be . fulfilled ;
437
+ }
438
+ } ) ;
439
+
440
+ } ) ;
441
+
442
+ describe ( 'simple model, multiple validators' , ( ) => {
443
+
444
+ const VALID_NAME = 'bob' ;
445
+ const NAME_ERROR_MESSAGE = `Invalid name: Only '${ VALID_NAME } ' is valid` ;
446
+ const VALID_AGE = 99 ;
447
+ const AGE_ERROR_MESSAGE = `Invalid age: Only '${ VALID_AGE } ' is valid` ;
448
+ const _sequelize = createSequelize ( { modelPaths : [ ] } ) ;
449
+
450
+ @Table
451
+ class User extends Model < User > {
452
+ @Column name : string ;
453
+ @Column age : number ;
454
+ @Validator nameValidator ( ) : void {
455
+ if ( this . name !== VALID_NAME ) {
456
+ throw new Error ( NAME_ERROR_MESSAGE ) ;
457
+ }
458
+ }
459
+ @Validator ageValidator ( ) : void {
460
+ if ( this . age !== VALID_AGE ) {
461
+ throw new Error ( AGE_ERROR_MESSAGE ) ;
462
+ }
463
+ }
464
+ }
465
+
466
+ _sequelize . addModels ( [ User ] ) ;
467
+
468
+ it ( 'should have metadata for multiple validators' , ( ) => {
469
+ const { validate} = Reflect . getMetadata ( 'sequelize:options' , User . prototype ) ;
470
+ expect ( validate ) . to . have . property ( 'nameValidator' ) ;
471
+ expect ( validate ) . to . have . property ( 'ageValidator' ) ;
472
+ } ) ;
473
+
474
+ it ( 'should throw due to wrong name' , ( ) => {
475
+ const user = new User ( { name : 'will' , age : VALID_AGE } ) ;
476
+
477
+ if ( majorVersion === 3 ) {
478
+ return user . validate ( ) . then ( err => expect ( err . errors [ 0 ] . message ) . to . eq ( NAME_ERROR_MESSAGE ) ) ;
479
+ } else if ( majorVersion === 4 ) {
480
+ return expect ( user . validate ( ) ) . to . be . rejectedWith ( NAME_ERROR_MESSAGE ) ;
481
+ }
482
+ } ) ;
483
+
484
+ it ( 'should throw due to wrong age' , ( ) => {
485
+ const user = new User ( { name : VALID_NAME , age : 1 } ) ;
486
+
487
+ if ( majorVersion === 3 ) {
488
+ return user . validate ( ) . then ( err => expect ( err . errors [ 0 ] . message ) . to . eq ( AGE_ERROR_MESSAGE ) ) ;
489
+ } else if ( majorVersion === 4 ) {
490
+ return expect ( user . validate ( ) ) . to . be . rejectedWith ( AGE_ERROR_MESSAGE ) ;
491
+ }
492
+ } ) ;
493
+
494
+ // it('should not throw', () => {
495
+ // const user = new User({name: VALID_NAME});
496
+ //
497
+ // if (majorVersion === 3) {
498
+ // return user.validate().then(err => expect(err).to.be.null);
499
+ // } else if (majorVersion === 4) {
500
+ // return expect(user.validate()).to.be.fulfilled;
501
+ // }
502
+ // });
503
+
504
+ } ) ;
505
+
506
+ } ) ;
507
+
388
508
} ) ;
389
509
390
510
describe ( 'only' , ( ) => {
0 commit comments