Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: types for validate in Column decorator #330

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array<Literal>> | string;

interface ColumnOption {
type?: AbstractDataType<DataType>;
Expand All @@ -13,7 +14,10 @@ interface ColumnOption {
primaryKey?: boolean;
columnName?: string;
validate?: {
[key: string]: boolean | RegExp | Function | Array<Array<Literal>> | string;
[key: string]: BaseValidateArgs | {
args: BaseValidateArgs;
msg: string;
};
}
}

Expand Down
19 changes: 19 additions & 0 deletions test/types/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ describe('=> Decorators (TypeScript)', function() {
}
})
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: '' });
Expand All @@ -179,6 +193,11 @@ describe('=> Decorators (TypeScript)', function() {
await assert.rejects(async () => {
await note.save();
}, /Validation notIn on name failed/);

note = new Note({ name: 'Github', status: 3 });
await assert.rejects(async () => {
await note.save();
}, /Error status/);
});
});

Expand Down