import { validate, validateSync as _validateSync } from 'class-validator'; export enum UserRole { ADMIN = 2, USER = 1, DISABLED = 0 } export function validateSync(data: T): void { const errors = _validateSync(data); if (errors.length > 0) { console.error('Validation failed, errors: ', errors); throw new Error('Validation failed'); } } export async function validateAsync(data: T): Promise { const errors = await validate(data); if (errors.length > 0) { console.error('Validation failed, errors: ', errors); throw new Error('Validation failed'); } } export async function validateAsyncInline( data: T ): Promise { await validateAsync(data); return data; } export function ValidateConstructor( constr: T ) { return class extends constr { constructor(...args: any[]) { super(...args); validateSync(this); } }; }