Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: adding more validation to workflow #696

Merged
merged 11 commits into from
Jun 3, 2024
54 changes: 53 additions & 1 deletion packages/runtime/src/util/validate-plan.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { ExecutionPlan, Step } from '@openfn/lexicon';
import { ExecutionPlan, Job, Step, Trigger, WorkflowOptions } from '@openfn/lexicon';
import { ValidationError } from '../errors';
import createLogger from '@openfn/logger';

type ModelNode = {
up: Record<string, true>;
down: Record<string, true>;
};

const logger = createLogger(undefined, { json: true });

SatyamMattoo marked this conversation as resolved.
Show resolved Hide resolved
type Model = {
[nodeId: string]: ModelNode;
};

export default (plan: ExecutionPlan) => {
assertWorkflowStructure(plan);
assertStart(plan);

const model = buildModel(plan);
Expand All @@ -20,6 +24,54 @@ export default (plan: ExecutionPlan) => {
return true;
};

const assertWorkflowStructure = (plan: ExecutionPlan) => {
const { workflow, options } = plan;

if (!workflow || typeof workflow !== 'object') {
throw new ValidationError('Missing or invalid workflow key in execution plan.');
}

if (!Array.isArray(workflow.steps)) {
throw new ValidationError('The workflow.steps key must be an array.');
}

if (workflow.steps.length === 0) {
logger.warn('Warning: The workflow.steps array is empty.');
}

workflow.steps.forEach((step, index) => {
assertStepStructure(step, index);
});

if (options) {
assertOptionsStructure(options);
}
SatyamMattoo marked this conversation as resolved.
Show resolved Hide resolved
};

const assertStepStructure = (step: Job | Step | Trigger, index: number) => {
const allowedKeys = ['id', 'name', 'next', 'previous', 'adaptor', 'expression', 'state', 'configuration', 'linker'];

Object.keys(step).forEach((key) => {
if (!allowedKeys.includes(key)) {
throw new ValidationError(`Invalid key "${key}" in step ${step.id || index}.`);
}
});

if ('adaptor' in step && !('expression' in step)) {
throw new ValidationError(`Step ${index} with an adaptor must also have an expression.`);
}
SatyamMattoo marked this conversation as resolved.
Show resolved Hide resolved
};

const assertOptionsStructure = (options: WorkflowOptions) => {
const allowedKeys = ['timeout', 'stepTimeout', 'start', 'end', 'sanitize'];

Object.keys(options).forEach((key) => {
if (!allowedKeys.includes(key)) {
SatyamMattoo marked this conversation as resolved.
Show resolved Hide resolved
logger.warn(`Warning: Unrecognized option "${key}" in options object.`);
}
SatyamMattoo marked this conversation as resolved.
Show resolved Hide resolved
});
};

export const buildModel = ({ workflow }: ExecutionPlan) => {
const model: Model = {};

Expand Down
4 changes: 3 additions & 1 deletion packages/runtime/test/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,9 @@ test('stuff written to state before an error is preserved', async (t) => {
steps: [
{
id: 'a',
data: { x: 0 },
state: {
data: { x: 0 }
},
expression:
'export default [(s) => { s.x = 1; throw new Error("test") }]',
},
Expand Down
51 changes: 46 additions & 5 deletions packages/runtime/test/util/validate-plan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import type { ExecutionPlan, Job } from '@openfn/lexicon';
import validate, { buildModel } from '../../src/util/validate-plan';

const job = (id: string, next?: Record<string, boolean>) =>
({
id,
next,
expression: '.',
} as Job);
({
id,
next,
expression: '.',
} as Job);

test('builds a simple model', (t) => {
const plan: ExecutionPlan = {
Expand Down Expand Up @@ -148,3 +148,44 @@ test('throws for invalid string start', (t) => {
message: 'Could not find start job: z',
});
});

test('throws for adaptor without an expression', (t) => {
const plan: ExecutionPlan = {
options: {
start: 'a',
},
workflow: {
steps: [
{
id: 'a',
adaptor: 'z'
}
],
},
};

t.throws(() => validate(plan), {
message: 'Step 0 with an adaptor must also have an expression.',
});
});

test('throws for unknown key in a step', (t) => {
const plan: ExecutionPlan = {
options: {
start: 'a',
},
workflow: {
steps: [
{
id: 'a',
//@ts-ignore
key: 'z'
}
],
},
};

t.throws(() => validate(plan), {
message: 'Invalid key "key" in step a.',
});
});
Loading