Skip to content

Instantly share code, notes, and snippets.

@euxn23
Created August 2, 2019 06:38
Show Gist options
  • Save euxn23/cf964a95f5d09235e8c7d3b4045947bc to your computer and use it in GitHub Desktop.
Save euxn23/cf964a95f5d09235e8c7d3b4045947bc to your computer and use it in GitHub Desktop.
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