All files / redis-smq-api/src/utils validate-dto.ts

100% Statements 8/8
100% Branches 1/1
100% Functions 1/1
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2157x 57x   57x       113x 113x             113x 1x   112x    
import { ClassConstructor, plainToClass } from 'class-transformer';
import { validate } from 'class-validator';
 
export async function validateDTO<T extends Record<string, any>>(
  dto: ClassConstructor<T>,
  plain: Record<string, any>,
): Promise<T> {
  const object = plainToClass(dto, plain);
  const errors = await validate(object, {
    stopAtFirstError: true,
    // See https://github.com/typestack/class-validator/issues/305#issuecomment-504778830
    // forbidUnknownValues: true,
    whitelist: true,
    forbidNonWhitelisted: true,
  });
  if (errors.length) {
    throw errors[0];
  }
  return object;
}