File: validations.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (93 lines) | stat: -rw-r--r-- 3,374 bytes parent folder | download
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from typing import Final

from moto.stepfunctions.parser.api import (
    EvaluationFailedEventDetails,
    HistoryEventType,
)
from moto.stepfunctions.parser.asl.component.common.error_name.failure_event import (
    FailureEvent,
    FailureEventException,
)
from moto.stepfunctions.parser.asl.component.common.error_name.states_error_name import (
    StatesErrorName,
)
from moto.stepfunctions.parser.asl.component.common.error_name.states_error_name_type import (
    StatesErrorNameType,
)
from moto.stepfunctions.parser.asl.eval.environment import Environment
from moto.stepfunctions.parser.asl.eval.event.event_detail import EventDetails
from moto.stepfunctions.parser.asl.jsonata.jsonata import (
    eval_jsonata_expression,
)

_SUPPORTED_JSONATA_TYPES: Final[set[str]] = {
    "null",
    "number",
    "string",
    "boolean",
    "array",
    "object",
}


def _validate_null_output(
    env: Environment, expression: str, rich_jsonata_expression: str
) -> None:
    exists: bool = eval_jsonata_expression(f"$exists({rich_jsonata_expression})")
    if exists:
        return
    error_name = StatesErrorName(typ=StatesErrorNameType.StatesQueryEvaluationError)
    failure_event = FailureEvent(
        env=env,
        error_name=error_name,
        event_type=HistoryEventType.EvaluationFailed,
        event_details=EventDetails(
            evaluationFailedEventDetails=EvaluationFailedEventDetails(
                # TODO: Add snapshot test to investigate behaviour for field string
                cause=f"The JSONata expression '{expression}' returned nothing (undefined).",
                error=error_name.error_name,
            )
        ),
    )
    raise FailureEventException(failure_event=failure_event)


def _validate_string_output(
    env: Environment, expression: str, rich_jsonata_expression: str
) -> None:
    jsonata_type: str = eval_jsonata_expression(f"$type({rich_jsonata_expression})")
    if jsonata_type in _SUPPORTED_JSONATA_TYPES:
        return
    error_name = StatesErrorName(typ=StatesErrorNameType.StatesQueryEvaluationError)
    failure_event = FailureEvent(
        env=env,
        error_name=error_name,
        event_type=HistoryEventType.EvaluationFailed,
        event_details=EventDetails(
            evaluationFailedEventDetails=EvaluationFailedEventDetails(
                # TODO: Add snapshot test to investigate behaviour for field string
                cause=f"The JSONata expression '{expression}' returned an unsupported result type.",
                error=error_name.error_name,
            )
        ),
    )
    raise FailureEventException(failure_event=failure_event)


def validate_jsonata_expression_output(
    env: Environment, expression: str, rich_jsonata_expression: str, jsonata_result: str
) -> None:
    try:
        if jsonata_result is None:
            _validate_null_output(env, expression, rich_jsonata_expression)
        if isinstance(jsonata_result, str):
            _validate_string_output(env, expression, rich_jsonata_expression)
    except FailureEventException as ex:
        env.event_manager.add_event(
            context=env.event_history_context,
            event_type=HistoryEventType.EvaluationFailed,
            event_details=EventDetails(
                evaluationFailedEventDetails=ex.get_evaluation_failed_event_details()
            ),
        )
        raise ex