File size: 2,147 Bytes
d9494a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { isDefined } from '@/utils';
import {
  type ValidatableWorkflow,
  type ValidatableWorkflowStep,
  type WorkflowValidationIssue,
  type WorkflowValidationResult,
} from '@/workflow/validation/types/workflow-validation.type';
import { buildWorkflowGraph } from '@/workflow/validation/utils/build-workflow-graph.util';
import { validateWorkflowGraph } from '@/workflow/validation/utils/validate-workflow-graph.util';
import { validateWorkflowStepParams } from '@/workflow/validation/utils/validate-workflow-step-params.util';
import { validateWorkflowVariableReferences } from '@/workflow/validation/utils/validate-workflow-variable-references.util';

const buildResult = (
  issues: WorkflowValidationIssue[],
): WorkflowValidationResult => {
  const errors = issues.filter((issue) => issue.severity === 'error');
  const warnings = issues.filter((issue) => issue.severity === 'warning');

  return {
    valid: errors.length === 0,
    errors,
    warnings,
  };
};

export const validateWorkflowStructure = (
  workflow: ValidatableWorkflow,
): WorkflowValidationResult => {
  const issues: WorkflowValidationIssue[] = [];

  if (!isDefined(workflow.trigger)) {
    issues.push({
      severity: 'error',
      code: 'MISSING_TRIGGER',
      message: 'The workflow has no trigger.',
    });
  } else if (!isDefined(workflow.trigger.type)) {
    issues.push({
      severity: 'error',
      code: 'MISSING_TRIGGER_TYPE',
      message: 'The workflow trigger has no type.',
    });
  }

  const steps = workflow.steps ?? [];

  if (steps.length === 0) {
    issues.push({
      severity: 'error',
      code: 'NO_STEPS',
      message: 'The workflow has no steps.',
    });

    return buildResult(issues);
  }

  const stepsById = new Map<string, ValidatableWorkflowStep>(
    steps.map((step) => [step.id, step]),
  );

  const graph = buildWorkflowGraph(workflow);

  issues.push(...validateWorkflowGraph({ workflow, graph }));
  issues.push(...validateWorkflowStepParams(workflow));
  issues.push(
    ...validateWorkflowVariableReferences({
      workflow,
      graph,
      stepsById,
    }),
  );

  return buildResult(issues);
};