extract, comma seperated list fields in Joi & genericField

This commit is contained in:
Felix Kaspar
2024-05-12 20:54:34 +02:00
parent 534a7776cf
commit a91dd0e502
7 changed files with 85 additions and 102 deletions

View File

@@ -1,4 +1,3 @@
import { PdfFile, RepresentationType } from "../wrappers/PdfFile";
import { Operator, Progress, oneToOne } from ".";
@@ -9,26 +8,7 @@ import i18next from "i18next";
import { getPages } from "./common/getPagesByIndex";
import { parsePageIndexSpecification } from "./common/pageIndexesUtils";
export interface ExtractPagesParamsType {
file: PdfFile;
pageIndexes: string | number[];
}
export async function extractPages(params: ExtractPagesParamsType): Promise<PdfFile> {
const { file, pageIndexes } = params;
const pdfLibDocument = await file.pdfLibDocument;
let indexes = pageIndexes;
if (!Array.isArray(indexes)) {
indexes = parsePageIndexSpecification(indexes, pdfLibDocument.getPageCount());
}
const newFile = await getPages(file, indexes);
newFile.filename += "_extractedPages";
return newFile;
}
import CommaArrayJoiExt from "../wrappers/CommaArrayJoiExt";
export class ExtractPages extends Operator {
static type = "extractPages";
@@ -39,9 +19,9 @@ export class ExtractPages extends Operator {
protected static inputSchema = JoiPDFFileSchema.label(i18next.t("inputs.pdffile.name")).description(i18next.t("inputs.pdffile.description"));
protected static valueSchema = Joi.object({
pageIndexes: Joi.array().items(Joi.number().integer()).required()
pageIndexes: CommaArrayJoiExt.comma_array().items(Joi.number().integer()).required()
.label(i18next.t("values.pageIndexes.friendlyName", { ns: "extractPages" })).description(i18next.t("values.pageIndexes.description", { ns: "extractPages" }))
.example("3").example("4").required()
.example("1").example("1, 2, 3, 4").example("4, 2, 4, 3").required()
});
protected static outputSchema = JoiPDFFileSchema.label(i18next.t("outputs.pdffile.name")).description(i18next.t("outputs.pdffile.description"));

View File

@@ -1,4 +1,3 @@
import { PDFPage } from "pdf-lib";
import { PdfFile, RepresentationType } from "../wrappers/PdfFile";

View File

@@ -0,0 +1,27 @@
import Joi from "@stirling-tools/joi";
export default Joi.extend((joi) => {
return {
// e.g. "'1', '2', '3', '10', '100', 'hello'"
type: 'comma_array',
base: joi.array(),
messages: {
'million.base': '{{#label}} must be a comma seperated list',
},
coerce: {
from: 'string',
method(value, helpers) {
if (typeof value !== 'string' || !/(\d+)(,\s*\d+)*/.test(value)) {
return;
}
try {
return { value: value.split(",").map(v => v.trim()) };
}
catch (ignoreErr) { }
}
}
}
});

View File

@@ -20,7 +20,7 @@ export const JoiPDFFileSchema = Joi.custom((value: Express.Multer.File[] /* <- a
throw new Error("an invalid type (unhandeled, non-file-type) was provided to pdf validation process. Please report this to maintainers.");
}
}
}, "pdffile validation");
}, "pdffile");
function isPdfFileArray(value: any[]): value is PdfFile[] { // "is" is a ts-typeguard - https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates
return value.every((e) => e instanceof PdfFile);