extract, comma seperated list fields in Joi & genericField
This commit is contained in:
@@ -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"));
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
import { PDFPage } from "pdf-lib";
|
||||
import { PdfFile, RepresentationType } from "../wrappers/PdfFile";
|
||||
|
||||
|
||||
27
shared-operations/src/wrappers/CommaArrayJoiExt.ts
Normal file
27
shared-operations/src/wrappers/CommaArrayJoiExt.ts
Normal 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) { }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user