From ab7b5d42585cf271c053b7ff07c34c3c5de3ac26 Mon Sep 17 00:00:00 2001 From: Caspar Neumann Date: Fri, 3 Nov 2023 14:22:45 +0100 Subject: [PATCH] Restructure AjaxPostBodyParamsTransformPipe --- .../ajax/post.body.params.transform-pipe.ts | 37 ++++++++----------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/apps/server/src/modules/h5p-editor/controller/dto/ajax/post.body.params.transform-pipe.ts b/apps/server/src/modules/h5p-editor/controller/dto/ajax/post.body.params.transform-pipe.ts index f37d3e02f1a..f4ab7c18e69 100644 --- a/apps/server/src/modules/h5p-editor/controller/dto/ajax/post.body.params.transform-pipe.ts +++ b/apps/server/src/modules/h5p-editor/controller/dto/ajax/post.body.params.transform-pipe.ts @@ -16,29 +16,24 @@ import { @Injectable() export class AjaxPostBodyParamsTransformPipe implements PipeTransform { async transform(value: AjaxPostBodyParams): Promise { - if (value) { - let transformed: Exclude; - - if ('libraries' in value) { - transformed = plainToClass(LibrariesBodyParams, value); - } else if ('contentId' in value) { - transformed = plainToClass(ContentBodyParams, value); - } else if ('libraryParameters' in value) { - transformed = plainToClass(LibraryParametersBodyParams, value); - } else { - return undefined; - } - - const validationResult = await validate(transformed); - if (validationResult.length > 0) { - const validationPipe = new ValidationPipe(); - const exceptionFactory = validationPipe.createExceptionFactory(); - throw exceptionFactory(validationResult); - } + if (value === undefined) { + return undefined; + } + if ('libraries' in value) { + value = plainToClass(LibrariesBodyParams, value); + } else if ('contentId' in value) { + value = plainToClass(ContentBodyParams, value); + } else if ('libraryParameters' in value) { + value = plainToClass(LibraryParametersBodyParams, value); + } - return transformed; + const validationResult = await validate(value); + if (validationResult.length > 0) { + const validationPipe = new ValidationPipe(); + const exceptionFactory = validationPipe.createExceptionFactory(); + throw exceptionFactory(validationResult); } - return undefined; + return value; } }