mirror of
https://github.com/immich-app/immich.git
synced 2026-05-18 03:10:24 +03:00
add controller tests, move validation testing from e2e
revert unnecessary change update mocks add structured-headers to e2e deps
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user