Files
outline/server/models/ApiKey.ts
Tom Moor 8bb88b8550 chore: Audit of all model column validations (#3757)
* chore: Updating all model validations before the white-hatters get to it ;)

* test

* Remove isUrl validation, thinking about it need to account for minio and other weird urls here
2022-07-09 08:04:40 -07:00

50 lines
880 B
TypeScript

import randomstring from "randomstring";
import {
Column,
Table,
Unique,
BeforeValidate,
BelongsTo,
ForeignKey,
} from "sequelize-typescript";
import User from "./User";
import ParanoidModel from "./base/ParanoidModel";
import Fix from "./decorators/Fix";
import Length from "./validators/Length";
@Table({ tableName: "apiKeys", modelName: "apiKey" })
@Fix
class ApiKey extends ParanoidModel {
@Length({
min: 3,
max: 255,
msg: "Name must be between 3 and 255 characters",
})
@Column
name: string;
@Unique
@Column
secret: string;
// hooks
@BeforeValidate
static async generateSecret(model: ApiKey) {
if (!model.secret) {
model.secret = randomstring.generate(38);
}
}
// associations
@BelongsTo(() => User, "userId")
user: User;
@ForeignKey(() => User)
@Column
userId: string;
}
export default ApiKey;