add controller tests, move validation testing from e2e

revert unnecessary change

update mocks

add structured-headers to e2e deps
This commit is contained in:
mertalev
2025-10-06 15:14:26 -04:00
parent 81a66350f6
commit fe71662d24
16 changed files with 525 additions and 125 deletions
+30
View File
@@ -1,3 +1,7 @@
import { BadRequestException } from '@nestjs/common';
import { plainToInstance } from 'class-transformer';
import { validateSync } from 'class-validator';
import { IncomingHttpHeaders } from 'node:http';
import { UAParser } from 'ua-parser-js';
@@ -20,3 +24,29 @@ export const getUserAgentDetails = (headers: IncomingHttpHeaders) => {
appVersion,
};
};
export function validateSyncOrReject<T extends object>(cls: new () => T, obj: any): T {
const dto = plainToInstance(cls, obj, { excludeExtraneousValues: true });
const errors = validateSync(dto);
if (errors.length === 0) {
return dto;
}
const constraints = [];
for (const error of errors) {
if (error.constraints) {
constraints.push(...Object.values(error.constraints));
}
if (!error.children) {
continue;
}
for (const child of error.children) {
if (child.constraints) {
constraints.push(...Object.values(child.constraints));
}
}
}
throw new BadRequestException(constraints);
}