mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-06-22 23:45:02 +00:00
Validation for Action.values and malformed JSON
This commit is contained in:
parent
6d81fa1a9e
commit
90f0ee0bc5
@ -20,8 +20,17 @@ router.post("/:workflowUuid?", [
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Validate input further (json may be invalid or not be in workflow format)
|
||||
const workflow = JSON.parse(req.body.workflow);
|
||||
try {
|
||||
var workflow = JSON.parse(req.body.workflow);
|
||||
} catch (err) {
|
||||
if (err instanceof Error) {
|
||||
console.error("malformed workflow-json was provided", err.message);
|
||||
res.status(400).json({error: "Malformed workflow-JSON was provided. See Server-Logs for more info", details: err.message});
|
||||
return;
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Replace with static multer function of pdffile
|
||||
const inputs = await Promise.all((req.files as Express.Multer.File[]).map(async file => {
|
||||
@ -32,12 +41,17 @@ router.post("/:workflowUuid?", [
|
||||
if(req.body.async === "false") {
|
||||
console.log("Don't do async");
|
||||
|
||||
let pdfResults = await traverseOperations(workflow.operations, inputs, (state) => {
|
||||
traverseOperations(workflow.operations, inputs, (state) => {
|
||||
console.log("State: ", state);
|
||||
}).then(async (pdfResults) => {
|
||||
console.log("Download");
|
||||
await respondWithPdfFiles(res, pdfResults, "workflow-results");
|
||||
}).catch((err) => {
|
||||
if(err.validationError)
|
||||
res.status(400).json({error: err});
|
||||
else
|
||||
throw err;
|
||||
})
|
||||
|
||||
console.log("Download");
|
||||
await respondWithPdfFiles(res, pdfResults, "workflow-results");
|
||||
}
|
||||
else {
|
||||
console.log("Start Aync Workflow");
|
||||
@ -63,6 +77,7 @@ router.post("/:workflowUuid?", [
|
||||
}
|
||||
});
|
||||
|
||||
// TODO: Handle when this throws errors
|
||||
let pdfResults = await traverseOperations(workflow.operations, inputs, (state) => {
|
||||
console.log("State: ", state);
|
||||
if(activeWorkflow.eventStream)
|
||||
|
@ -6,11 +6,11 @@ import { validateOperations } from "./validateOperations";
|
||||
import { getOperatorByName } from "./getOperatorByName";
|
||||
|
||||
export async function traverseOperations(operations: Action[], input: PdfFile[], progressCallback: (state: Progress) => void): Promise<PdfFile[]> {
|
||||
|
||||
const validationResult = validateOperations(operations)
|
||||
const validationResult = validateOperations(operations);
|
||||
if(!validationResult.valid) {
|
||||
throw Error(validationResult.reason);
|
||||
return Promise.reject({validationError: validationResult.reason});
|
||||
}
|
||||
|
||||
const waitOperations = organizeWaitOperations(operations);
|
||||
|
||||
let results: PdfFile[] = [];
|
||||
|
@ -1,16 +1,35 @@
|
||||
import { Action } from "../../declarations/Action";
|
||||
import { getOperatorByName } from "./getOperatorByName";
|
||||
|
||||
export function validateOperations(actions: Action[]): { valid: boolean, reason?: string} {
|
||||
// TODO: Validate using inbuilt validators:
|
||||
/*
|
||||
validationResult = impose.validate()
|
||||
if(validationResult.valid) {
|
||||
// Check Next
|
||||
}
|
||||
else {
|
||||
return validationResult.reason
|
||||
}
|
||||
*/
|
||||
function validateOperation(actions: Action[]): { valid: boolean; reason?: string; } {
|
||||
for (const action of actions) {
|
||||
if (action.type === "wait" || action.type === "done") {
|
||||
// TODO: Validate these too ):
|
||||
return { valid: true };
|
||||
}
|
||||
else {
|
||||
const operator = getOperatorByName(action.type);
|
||||
if(!operator) {
|
||||
return { valid: false, reason: `action.type ${action.type} does not exist` }
|
||||
}
|
||||
const validationResult = new operator(action).validate();
|
||||
|
||||
return { valid: true };
|
||||
if(!validationResult.valid) {
|
||||
return validationResult;
|
||||
}
|
||||
}
|
||||
|
||||
if (action.actions) {
|
||||
const validationResult = validateOperation(action.actions);
|
||||
|
||||
if(!validationResult.valid) {
|
||||
return validationResult;
|
||||
}
|
||||
}
|
||||
}
|
||||
return { valid: true };
|
||||
}
|
||||
|
||||
return validateOperation(actions);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user