some validation on entities' properties

This commit is contained in:
2020-10-14 09:05:31 +03:00
committed by Stepan Usatiuk
parent e792fc6adc
commit 0dbf4b020e
5 changed files with 88 additions and 2 deletions

View File

@@ -6,7 +6,9 @@ import { constants as fsConstants } from "fs";
import {
AfterRemove,
BaseEntity,
BeforeInsert,
BeforeRemove,
BeforeUpdate,
Column,
Entity,
Index,
@@ -14,6 +16,15 @@ import {
PrimaryGeneratedColumn,
} from "typeorm";
import { User } from "./User";
import {
isAlphanumeric,
IsAlphanumeric,
IsHash,
IsMimeType,
Length,
Matches,
validateOrReject,
} from "class-validator";
export interface IPhotoJSON {
id: number;
@@ -32,14 +43,16 @@ export class Photo extends BaseEntity {
@Column({ length: 190 })
@Index()
@IsHash("md5")
public hash: string;
@Column({ length: 190 })
@Index()
@IsAlphanumeric()
@Matches(/\d*x\d*/)
public size: string;
@Column({ length: 190 })
@Index()
@IsMimeType()
public format: string;
@Column({ type: "timestamp", default: null })
@@ -61,6 +74,16 @@ export class Photo extends BaseEntity {
return path.join(this.user.getDataPath(), this.getFileName());
}
@BeforeInsert()
async beforeInsertValidate(): Promise<void> {
return validateOrReject(this);
}
@BeforeUpdate()
async beforeUpdateValidate(): Promise<void> {
return validateOrReject(this);
}
@BeforeRemove()
async cleanupFiles(): Promise<void> {
try {