1
+ /* tslint:disable:max-classes-per-file */
2
+
1
3
import { expect , use } from 'chai' ;
2
4
import * as chaiAsPromised from 'chai-as-promised' ;
3
5
import { DefineValidateOptions } from "sequelize" ;
@@ -8,6 +10,10 @@ import {
8
10
} from "../models/ShoeWithValidation" ;
9
11
import { majorVersion } from "../../lib/utils/versioning" ;
10
12
import { Is } from "../../lib/annotations/validation/Is" ;
13
+ import { Model } from "../../lib/models/Model" ;
14
+ import { Table } from "../../lib/annotations/Table" ;
15
+ import { Column } from "../../lib/annotations/Column" ;
16
+ import { Length } from "../../lib/annotations/validation/Length" ;
11
17
12
18
use ( chaiAsPromised ) ;
13
19
@@ -20,7 +26,7 @@ describe('validation', () => {
20
26
describe ( `rawAttributes of ${ ShoeWithValidation . name } ` , ( ) => {
21
27
22
28
const rawAttributes = ShoeWithValidation [ 'rawAttributes' ] ;
23
- const shoeAttributes : { [ key : string ] : DefineValidateOptions } = {
29
+ const shoeAttributes : { [ key : string ] : DefineValidateOptions } = {
24
30
id : {
25
31
isUUID : UUID_VERSION
26
32
} ,
@@ -98,7 +104,7 @@ describe('validation', () => {
98
104
99
105
describe ( 'validation' , ( ) => {
100
106
101
- const data : { [ key : string ] : { valid : any [ ] ; invalid : any [ ] } } = {
107
+ const data : { [ key : string ] : { valid : any [ ] ; invalid : any [ ] } } = {
102
108
id : {
103
109
valid : [ '903830b8-4dcc-4f10-a5aa-35afa8445691' , null , undefined ] ,
104
110
invalid : [ '' , 'abc' , 1 ] ,
@@ -190,9 +196,123 @@ describe('validation', () => {
190
196
describe ( 'Is' , ( ) => {
191
197
192
198
it ( 'Should throw due to missing name of function' , ( ) => {
193
-
194
199
expect ( ( ) => Is ( ( ) => null ) ) . to . throw ( / P a s s e d v a l i d a t o r f u n c t i o n m u s t h a v e a n a m e / ) ;
200
+ } ) ;
201
+
202
+ } ) ;
203
+
204
+ describe ( 'Length' , ( ) => {
205
+
206
+ it ( 'should not produce an error' , ( ) => {
207
+ @Table
208
+ class User extends Model < User > {
209
+ @Length ( { min : 0 , max : 5 } ) @Column name : string ;
210
+ }
211
+ const sequelizeValidationOnly = createSequelizeValidationOnly ( false ) ;
212
+ sequelizeValidationOnly . addModels ( [ User ] ) ;
213
+ const user = new User ( { name : 'elisa' } ) ;
214
+
215
+ if ( majorVersion === 3 ) {
216
+ return user . validate ( ) . then ( err => expect ( err ) . to . be . not . an ( 'object' ) ) ;
217
+ } else if ( majorVersion === 4 ) {
218
+ return expect ( user . validate ( ) ) . to . be . not . rejected ;
219
+ }
220
+ } ) ;
221
+
222
+ it ( 'should produce an error due to unfulfilled max' , ( ) => {
223
+ @Table
224
+ class User extends Model < User > {
225
+ @Length ( { min : 0 , max : 5 } ) @Column name : string ;
226
+ }
227
+ const sequelizeValidationOnly = createSequelizeValidationOnly ( false ) ;
228
+ sequelizeValidationOnly . addModels ( [ User ] ) ;
229
+ const user = new User ( { name : 'elisa tree' } ) ;
230
+
231
+ if ( majorVersion === 3 ) {
232
+ return user . validate ( ) . then ( err => expect ( err ) . to . be . an ( 'object' ) ) ;
233
+ } else if ( majorVersion === 4 ) {
234
+ return expect ( user . validate ( ) ) . to . be . rejected ;
235
+ }
236
+ } ) ;
237
+
238
+ it ( 'should produce an error due to unfulfilled min' , ( ) => {
239
+ @Table
240
+ class User extends Model < User > {
241
+ @Length ( { min : 5 , max : 5 } ) @Column name : string ;
242
+ }
243
+ const sequelizeValidationOnly = createSequelizeValidationOnly ( false ) ;
244
+ sequelizeValidationOnly . addModels ( [ User ] ) ;
245
+ const user = new User ( { name : 'elli' } ) ;
246
+
247
+ if ( majorVersion === 3 ) {
248
+ return user . validate ( ) . then ( err => expect ( err ) . to . be . an ( 'object' ) ) ;
249
+ } else if ( majorVersion === 4 ) {
250
+ return expect ( user . validate ( ) ) . to . be . rejected ;
251
+ }
252
+ } ) ;
253
+
254
+ it ( 'should not produce an error (max only)' , ( ) => {
255
+ @Table
256
+ class User extends Model < User > {
257
+ @Length ( { max : 5 } ) @Column name : string ;
258
+ }
259
+ const sequelizeValidationOnly = createSequelizeValidationOnly ( false ) ;
260
+ sequelizeValidationOnly . addModels ( [ User ] ) ;
261
+ const user = new User ( { name : 'elisa' } ) ;
262
+
263
+ if ( majorVersion === 3 ) {
264
+ return user . validate ( ) . then ( err => expect ( err ) . to . be . not . an ( 'object' ) ) ;
265
+ } else if ( majorVersion === 4 ) {
266
+ return expect ( user . validate ( ) ) . to . be . not . rejected ;
267
+ }
268
+ } ) ;
269
+
270
+ it ( 'should produce an error (max only)' , ( ) => {
271
+ @Table
272
+ class User extends Model < User > {
273
+ @Length ( { max : 5 } ) @Column name : string ;
274
+ }
275
+ const sequelizeValidationOnly = createSequelizeValidationOnly ( false ) ;
276
+ sequelizeValidationOnly . addModels ( [ User ] ) ;
277
+ const user = new User ( { name : 'elisa tree' } ) ;
278
+
279
+ if ( majorVersion === 3 ) {
280
+ return user . validate ( ) . then ( err => expect ( err ) . to . be . an ( 'object' ) ) ;
281
+ } else if ( majorVersion === 4 ) {
282
+ return expect ( user . validate ( ) ) . to . be . rejected ;
283
+ }
284
+ } ) ;
285
+
286
+ it ( 'should not produce an error (min only)' , ( ) => {
287
+ @Table
288
+ class User extends Model < User > {
289
+ @Length ( { min : 4 } ) @Column name : string ;
290
+ }
291
+ const sequelizeValidationOnly = createSequelizeValidationOnly ( false ) ;
292
+ sequelizeValidationOnly . addModels ( [ User ] ) ;
293
+ const user = new User ( { name : 'elisa' } ) ;
294
+
295
+ if ( majorVersion === 3 ) {
296
+ return user . validate ( ) . then ( err => expect ( err ) . to . be . not . an ( 'object' ) ) ;
297
+ } else if ( majorVersion === 4 ) {
298
+ return expect ( user . validate ( ) ) . to . be . not . rejected ;
299
+ }
300
+ } ) ;
195
301
302
+ it ( 'should produce an error (min only)' , ( ) => {
303
+ @Table
304
+ class User extends Model < User > {
305
+ @Length ( { min : 5 } ) @Column name : string ;
306
+ }
307
+ const sequelizeValidationOnly = createSequelizeValidationOnly ( false ) ;
308
+ sequelizeValidationOnly . addModels ( [ User ] ) ;
309
+ const user = new User ( { name : 'elli' } ) ;
310
+
311
+ if ( majorVersion === 3 ) {
312
+ return user . validate ( ) . then ( err => expect ( err ) . to . be . an ( 'object' ) ) ;
313
+ } else if ( majorVersion === 4 ) {
314
+ return expect ( user . validate ( ) ) . to . be . rejected ;
315
+ }
196
316
} ) ;
197
317
198
318
} ) ;
0 commit comments