Files
rk-backend/src/_dto/band-oben.dto.ts
T
2026-10-22 07:57:46 +02:00

34 lines
1.9 KiB
TypeScript

import { BadRequestException } from '@nestjs/common';
import type { BandObenConfig, BandObenOptions } from '../graphics/elements/types';
export class BandObenRenderDto {
config!: BandObenConfig;
options?: Partial<BandObenOptions>;
}
export function parseBandObenRequest(input: unknown): BandObenRenderDto {
const object = (value: unknown): value is Record<string, unknown> =>
typeof value === 'object' && value !== null && !Array.isArray(value);
if (!object(input) || Object.keys(input).some(key => !['config', 'options'].includes(key)) || !object(input.config))
throw new BadRequestException('Erwartet werden config und optional options');
const config = input.config;
if (Object.keys(config).some(key => !['width', 'length'].includes(key)) ||
typeof config.width !== 'number' || !Number.isFinite(config.width) || config.width <= 10 ||
typeof config.length !== 'number' || !Number.isFinite(config.length) || config.length <= 0)
throw new BadRequestException('config.width muss größer als 10 und config.length positiv sein; beide Werte müssen endliche Zahlen sein');
const options = input.options === undefined ? {} : input.options;
if (!object(options) || Object.entries(options).some(([key, value]) =>
!['fromNorthToSouth', 'trichterStart', 'trichterEnd'].includes(key) || typeof value !== 'boolean'))
throw new BadRequestException('options erlaubt nur fromNorthToSouth, trichterStart und trichterEnd als Boolean');
if ((options.trichterStart || options.trichterEnd) && config.length < 20)
throw new BadRequestException('Mit Trichter muss die Bandlänge mindestens 20 sein');
return {
config: { width: config.width, length: config.length },
options: {
fromNorthToSouth: options.fromNorthToSouth ?? false,
trichterStart: options.trichterStart ?? false,
trichterEnd: options.trichterEnd ?? false,
} as BandObenOptions,
};
}