Created
August 2, 2019 06:38
-
-
Save euxn23/cf964a95f5d09235e8c7d3b4045947bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Image } from './image' | |
import { | |
IsInt, | |
IsNotEmpty, | |
validateSync, | |
ValidationError | |
} from 'class-validator' | |
export class ImageEntity implements Image { | |
@IsNotEmpty() | |
readonly id: number | |
@IsNotEmpty() | |
readonly productId: number | |
@IsNotEmpty() | |
readonly createdAt: number | |
@IsNotEmpty() | |
readonly updatedAt: number | |
constructor({ | |
id, | |
productId, | |
createdAt, | |
updatedAt | |
}: Image) { | |
this.id = id | |
this.productId = productId | |
this.createdAt = createdAt | |
this.updatedAt = updatedAt | |
} | |
toObject(): Image { | |
const { | |
id, | |
productId, | |
createdAt, | |
updatedAt | |
}: Image = this | |
return { | |
id, | |
productId, | |
createdAt, | |
updatedAt | |
} | |
} | |
validate(): void { | |
try { | |
validateSync(this) | |
} catch (e) { | |
// not to handle error because validating response is duty of controllers | |
throw new ValidationError() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment