|
1 | 1 | """The WorkflowEngine validation logic."""
|
2 | 2 |
|
| 3 | +import json |
3 | 4 | from dataclasses import dataclass
|
4 | 5 | from enum import Enum
|
5 | 6 | from typing import Any
|
@@ -47,7 +48,59 @@ def validate(
|
47 | 48 | if workflow_inputs:
|
48 | 49 | assert isinstance(workflow_inputs, dict)
|
49 | 50 |
|
| 51 | + # ALl levels require a schema validation |
50 | 52 | if error := validate_schema(workflow_definition):
|
51 | 53 | return ValidationResult(error_num=1, error_msg=[error])
|
52 | 54 |
|
| 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 | + |
53 | 106 | return _VALIDATION_SUCCESS
|
0 commit comments