From 586d03c8f28b017d010d5f299cb937c760b1813d Mon Sep 17 00:00:00 2001 From: vagusx Date: Wed, 24 Aug 2022 13:52:52 +0800 Subject: [PATCH 1/2] fix: types for validate in `Column` decorator --- src/decorators.ts | 6 +++++- test/types/decorators.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/decorators.ts b/src/decorators.ts index 69b4d82e..f9e61ee9 100644 --- a/src/decorators.ts +++ b/src/decorators.ts @@ -4,6 +4,7 @@ import { ASSOCIATE_METADATA_MAP } from './constants'; import 'reflect-metadata'; type Literal = null | undefined | boolean | number | bigint | string | Date | object | ArrayBuffer; +type BaseValidateArgs = boolean | RegExp | Function | Array> | string; interface ColumnOption { type?: AbstractDataType; @@ -13,7 +14,10 @@ interface ColumnOption { primaryKey?: boolean; columnName?: string; validate?: { - [key: string]: boolean | RegExp | Function | Array> | string; + [key: string]: BaseValidateArgs | { + args: BaseValidateArgs; + msg: string; + }; } } diff --git a/test/types/decorators.test.ts b/test/types/decorators.test.ts index 7f62be43..7d67412b 100644 --- a/test/types/decorators.test.ts +++ b/test/types/decorators.test.ts @@ -166,9 +166,24 @@ describe('=> Decorators (TypeScript)', function() { if(!v) throw new Error('name cannot be null') }, notIn: [ [ 'Yhorm', 'Gwyn' ] ], + } }) name: string; + + @Column({ + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: 1, + validate: { + isNumeric: true, + isIn: { + args: [ [ '1', '2' ] ], + msg: 'Error status', + }, + } + }) + status: number; } await Note.sync({ force: true }); let note = new Note({ name: '' }); @@ -179,6 +194,13 @@ describe('=> Decorators (TypeScript)', function() { await assert.rejects(async () => { await note.save(); }, /Validation notIn on name failed/); + + + note = new Note({ name: 'Yhorm', status: '3' }); + + await assert.rejects(async () => { + await note.save(); + }, /Error status/); }); }); From 3ab7a3621f4fa5c731fabfa5861fda0c7f4d6558 Mon Sep 17 00:00:00 2001 From: vagusx Date: Wed, 24 Aug 2022 14:04:47 +0800 Subject: [PATCH 2/2] fix: test cases --- test/types/decorators.test.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/types/decorators.test.ts b/test/types/decorators.test.ts index 7d67412b..6a07dcca 100644 --- a/test/types/decorators.test.ts +++ b/test/types/decorators.test.ts @@ -166,7 +166,6 @@ describe('=> Decorators (TypeScript)', function() { if(!v) throw new Error('name cannot be null') }, notIn: [ [ 'Yhorm', 'Gwyn' ] ], - } }) name: string; @@ -195,9 +194,7 @@ describe('=> Decorators (TypeScript)', function() { await note.save(); }, /Validation notIn on name failed/); - - note = new Note({ name: 'Yhorm', status: '3' }); - + note = new Note({ name: 'Github', status: 3 }); await assert.rejects(async () => { await note.save(); }, /Error status/);