|
17 | 17 | import uuid |
18 | 18 |
|
19 | 19 | import cloudevents.v1.exceptions as cloud_exceptions |
| 20 | +from cloudevents.core.spec import is_valid_attribute_name |
20 | 21 | from cloudevents.v1 import abstract |
21 | 22 | from cloudevents.v1.sdk.event import v03, v1 |
22 | 23 |
|
|
26 | 27 | } |
27 | 28 |
|
28 | 29 |
|
| 30 | +def _validate_attribute_name(name: str) -> None: |
| 31 | + if not is_valid_attribute_name(name): |
| 32 | + raise cloud_exceptions.InvalidAttributeName( |
| 33 | + f"Invalid CloudEvent attribute name '{name}': " |
| 34 | + "attribute names must only contain lowercase ASCII letters and digits" |
| 35 | + ) |
| 36 | + |
| 37 | + |
29 | 38 | class CloudEvent(abstract.CloudEvent): |
30 | 39 | """ |
31 | 40 | Python-friendly cloudevent class supporting v1 events |
@@ -59,6 +68,8 @@ def __init__(self, attributes: typing.Mapping[str, str], data: typing.Any = None |
59 | 68 | :type data: typing.Any |
60 | 69 | """ |
61 | 70 | self._attributes = {k.lower(): v for k, v in attributes.items()} |
| 71 | + for attribute_name in self._attributes: |
| 72 | + _validate_attribute_name(attribute_name) |
62 | 73 | self.data = data |
63 | 74 | if "specversion" not in self._attributes: |
64 | 75 | self._attributes["specversion"] = "1.0" |
@@ -88,6 +99,7 @@ def get_data(self) -> typing.Optional[typing.Any]: |
88 | 99 | return self.data |
89 | 100 |
|
90 | 101 | def __setitem__(self, key: str, value: typing.Any) -> None: |
| 102 | + _validate_attribute_name(key) |
91 | 103 | self._attributes[key] = value |
92 | 104 |
|
93 | 105 | def __delitem__(self, key: str) -> None: |
|
0 commit comments