Workflow and API validation for input file types

(still needs to be ckecked if a pdf is valid)
This commit is contained in:
Felix Kaspar
2023-12-21 16:42:00 +01:00
parent efd8b48a3f
commit 3e10972efa
6 changed files with 37 additions and 40 deletions

View File

@@ -1,8 +1,17 @@
import Joi from "joi";
import { PdfFile } from "./PdfFile";
export const JoiPDFFileSchema = Joi.binary().custom((value: Express.Multer.File[] | PdfFile, helpers) => {
if (!(value instanceof PdfFile)) {
export const JoiPDFFileSchema = Joi.binary().custom((value: Express.Multer.File[] | PdfFile | PdfFile[], helpers) => {
if (value instanceof PdfFile) {
return value;
}
else if (Array.isArray(value)) {
if(value.every((e) => e instanceof PdfFile))
return value;
else
throw new Error("Some elements in the array are not of type PdfFile");
}
else {
try {
return PdfFile.fromMulterFiles(value);
} catch (error) {
@@ -10,5 +19,4 @@ export const JoiPDFFileSchema = Joi.binary().custom((value: Express.Multer.File[
throw new Error('value is not of type PdfFile');
}
}
return value;
}, "pdffile validation");