WIP joi validation for impose

This commit is contained in:
Felix Kaspar
2023-11-27 23:35:18 +01:00
parent 09aa3a8bc9
commit ba2588ea24
7 changed files with 154 additions and 115 deletions

View File

@@ -91,7 +91,7 @@ export class PdfFile {
this.filename = filename ? filename : originalFilename;
if (this.filename.toLowerCase().endsWith(".pdf"))
this.filename = this.filename.slice(0, -4);
this.filename = this.filename.slice(0, -4);
this.representation = representation;
this.representationType = representationType;
@@ -99,6 +99,7 @@ export class PdfFile {
static fromMulterFile(value: Express.Multer.File): PdfFile {
return new PdfFile(value.originalname, value.buffer as Uint8Array, RepresentationType.Uint8Array);
}
static fromMulterFiles(values: Express.Multer.File[]): PdfFile[] {
return values.map(v => PdfFile.fromMulterFile(v));
@@ -128,11 +129,4 @@ export class PdfFile {
}));
return docCache;
}
}
export const PdfFileSchema = Joi.any().custom((value) => {
if (!(value instanceof PdfFile)) {
throw new Error('value is not a PdfFile');
}
return value;
}, "PdfFile validation");
}

View File

@@ -0,0 +1,14 @@
import Joi from "joi";
import { PdfFile } from "./PdfFile";
export const JoiPDFFileSchema = Joi.binary().custom((value: Express.Multer.File[] | PdfFile, helpers) => {
if (!(value instanceof PdfFile)) {
try {
return PdfFile.fromMulterFiles(value);
} catch (error) {
console.error(error);
throw new Error('value is not of type PdfFile');
}
}
return value;
}, "pdffile validation");