Launching soon!!!

logo

If it 's code, show how it works
generate data flow graphs for your backend codebase.

Nodejs
1import { createParamDecorator, ExecutionContext, BadRequestException } from "@nestjs/common";
2import { plainToClass } from "class-transformer";
3import { validate } from "class-validator";
4export const ValidateAndTransform = createParamDecorator(async <T>(data: new () => T, ctx: ExecutionContext) =>{
5 const dto = plainToClass(data, ctx.switchToHttp().getRequest().body, { excludeExtraneousValues: true });
6 const errors = await validate(dto, { whitelist: true, forbidNonWhitelisted: true });
7 if (errors.length > 0) throw new BadRequestException(errors.flatMap(e => Object.values(e.constraints)));
8 return Object.assign(new data(), JSON.parse(JSON.stringify(dto)));
9});