Skip to content

Commit 5962207

Browse files
author
Alan Christie
committed
feat: Add initial RUN level validation
1 parent 5556090 commit 5962207

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

workflow/workflow_validator.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""The WorkflowEngine validation logic."""
22

3+
import json
34
from dataclasses import dataclass
45
from enum import Enum
56
from typing import Any
@@ -47,7 +48,59 @@ def validate(
4748
if workflow_inputs:
4849
assert isinstance(workflow_inputs, dict)
4950

51+
# ALl levels require a schema validation
5052
if error := validate_schema(workflow_definition):
5153
return ValidationResult(error_num=1, error_msg=[error])
5254

55+
if level == ValidationLevel.RUN:
56+
run_level_result: ValidationResult = WorkflowValidator._validate_run_level(
57+
workflow_definition=workflow_definition,
58+
workflow_inputs=workflow_inputs,
59+
)
60+
if run_level_result.error_num:
61+
return run_level_result
62+
63+
return _VALIDATION_SUCCESS
64+
65+
@classmethod
66+
def _validate_run_level(
67+
cls,
68+
*,
69+
workflow_definition: dict[str, Any],
70+
workflow_inputs: dict[str, Any] | None = None,
71+
) -> ValidationResult:
72+
assert workflow_definition
73+
del workflow_inputs
74+
75+
# RUN level requires that the specification is a valid JSON string.
76+
# and contains properties for 'collection', 'job', and 'version'.
77+
try:
78+
specification = json.loads(workflow_definition["specification"])
79+
except json.decoder.JSONDecodeError as e:
80+
return ValidationResult(
81+
error_num=1,
82+
error_msg=[
83+
f"Error decoding specification, which is not valid JSON: {e}"
84+
],
85+
)
86+
except TypeError as e:
87+
return ValidationResult(
88+
error_num=2,
89+
error_msg=[
90+
f"Error decoding specification, which is not valid JSON: {e}"
91+
],
92+
)
93+
expected_keys: set[str] = {"collection", "job", "version"}
94+
missing_keys: list[str] = []
95+
missing_keys.extend(
96+
expected_key
97+
for expected_key in expected_keys
98+
if expected_key not in specification
99+
)
100+
if missing_keys:
101+
return ValidationResult(
102+
error_num=2,
103+
error_msg=[f"Specification is missing: {', '.join(missing_keys)}"],
104+
)
105+
53106
return _VALIDATION_SUCCESS

0 commit comments

Comments
 (0)