-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b292efb
commit 94823f9
Showing
3 changed files
with
48 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,65 @@ | ||
import { ExecutionPlan, Step, WorkflowOptions } from "@openfn/lexicon"; | ||
import { Logger } from "@openfn/logger"; | ||
import { ExecutionPlan, Step, WorkflowOptions } from '@openfn/lexicon'; | ||
import { Logger } from '@openfn/logger'; | ||
|
||
const assertWorkflowStructure = (plan: ExecutionPlan, logger: Logger) => { | ||
const { workflow, options } = plan; | ||
const { workflow, options } = plan; | ||
|
||
if (!workflow || typeof workflow !== 'object') { | ||
throw new Error(`Missing or invalid "workflow" key in execution plan`); | ||
} | ||
if (!workflow || typeof workflow !== 'object') { | ||
throw new Error(`Missing or invalid "workflow" key in execution plan`); | ||
} | ||
|
||
if (!Array.isArray(workflow.steps)) { | ||
throw new Error('The workflow.steps key must be an array'); | ||
} | ||
if (!Array.isArray(workflow.steps)) { | ||
throw new Error('The workflow.steps key must be an array'); | ||
} | ||
|
||
if (workflow.steps.length === 0) { | ||
logger.warn('The workflow.steps array is empty'); | ||
} | ||
if (workflow.steps.length === 0) { | ||
logger.warn('The workflow.steps array is empty'); | ||
} | ||
|
||
workflow.steps.forEach((step, index) => { | ||
assertStepStructure(step, index); | ||
}); | ||
workflow.steps.forEach((step, index) => { | ||
assertStepStructure(step, index); | ||
}); | ||
|
||
assertOptionsStructure(options, logger); | ||
assertOptionsStructure(options, logger); | ||
}; | ||
|
||
const assertStepStructure = (step: Step, index: number) => { | ||
const allowedKeys = ['id', 'name', 'next', 'previous', 'adaptor', 'expression', 'state', 'configuration', 'linker']; | ||
const allowedKeys = [ | ||
'id', | ||
'name', | ||
'next', | ||
'previous', | ||
'adaptor', | ||
'expression', | ||
'state', | ||
'configuration', | ||
'linker', | ||
]; | ||
|
||
for (const key in step) { | ||
if (!allowedKeys.includes(key)) { | ||
throw new Error(`Invalid key "${key}" in step ${step.id || index}`); | ||
} | ||
for (const key in step) { | ||
if (!allowedKeys.includes(key)) { | ||
throw new Error(`Invalid key "${key}" in step ${step.id || index}`); | ||
} | ||
} | ||
|
||
if ('adaptor' in step && !('expression' in step)) { | ||
throw new Error(`Step ${step.id ?? index} with an adaptor must also have an expression`); | ||
} | ||
if ('adaptor' in step && !('expression' in step)) { | ||
throw new Error( | ||
`Step ${step.id ?? index} with an adaptor must also have an expression` | ||
); | ||
} | ||
}; | ||
|
||
const assertOptionsStructure = (options: WorkflowOptions = {}, logger: Logger) => { | ||
const allowedKeys = ['timeout', 'stepTimeout', 'start', 'end', 'sanitize']; | ||
const assertOptionsStructure = ( | ||
options: WorkflowOptions = {}, | ||
logger: Logger | ||
) => { | ||
const allowedKeys = ['timeout', 'stepTimeout', 'start', 'end', 'sanitize']; | ||
|
||
for (const key in options) { | ||
if (!allowedKeys.includes(key)) { | ||
logger.warn(`Unrecognized option "${key}" in options object`); | ||
} | ||
for (const key in options) { | ||
if (!allowedKeys.includes(key)) { | ||
logger.warn(`Unrecognized option "${key}" in options object`); | ||
} | ||
} | ||
}; | ||
|
||
export default assertWorkflowStructure; | ||
export default assertWorkflowStructure; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters