22
22
CloudEventValidationError ,
23
23
CustomExtensionAttributeError ,
24
24
InvalidAttributeTypeError ,
25
+ InvalidAttributeValueError ,
25
26
MissingRequiredAttributeError ,
26
27
)
27
28
@@ -84,41 +85,37 @@ def _validate_required_attributes(
84
85
errors = defaultdict (list )
85
86
86
87
if "id" not in attributes :
87
- errors ["id" ].append (MissingRequiredAttributeError (missing = "id" ))
88
+ errors ["id" ].append (MissingRequiredAttributeError (attribute_name = "id" ))
88
89
if attributes .get ("id" ) is None :
89
90
errors ["id" ].append (
90
- InvalidAttributeTypeError ( "Attribute 'id' must not be None" )
91
+ InvalidAttributeValueError ( "id" , "Attribute 'id' must not be None" )
91
92
)
92
93
if not isinstance (attributes .get ("id" ), str ):
93
- errors ["id" ].append (
94
- InvalidAttributeTypeError ("Attribute 'id' must be a string" )
95
- )
94
+ errors ["id" ].append (InvalidAttributeTypeError ("id" , str ))
96
95
97
96
if "source" not in attributes :
98
- errors ["source" ].append (MissingRequiredAttributeError (missing = "source" ))
99
- if not isinstance (attributes .get ("source" ), str ):
100
97
errors ["source" ].append (
101
- InvalidAttributeTypeError ( "Attribute ' source' must be a string " )
98
+ MissingRequiredAttributeError ( attribute_name = " source" )
102
99
)
100
+ if not isinstance (attributes .get ("source" ), str ):
101
+ errors ["source" ].append (InvalidAttributeTypeError ("source" , str ))
103
102
104
103
if "type" not in attributes :
105
- errors ["type" ].append (MissingRequiredAttributeError (missing = "type" ))
104
+ errors ["type" ].append (MissingRequiredAttributeError (attribute_name = "type" ))
106
105
if not isinstance (attributes .get ("type" ), str ):
107
- errors ["type" ].append (
108
- InvalidAttributeTypeError ("Attribute 'type' must be a string" )
109
- )
106
+ errors ["type" ].append (InvalidAttributeTypeError ("type" , str ))
110
107
111
108
if "specversion" not in attributes :
112
109
errors ["specversion" ].append (
113
- MissingRequiredAttributeError (missing = "specversion" )
110
+ MissingRequiredAttributeError (attribute_name = "specversion" )
114
111
)
115
112
if not isinstance (attributes .get ("specversion" ), str ):
116
- errors ["specversion" ].append (
117
- InvalidAttributeTypeError ("Attribute 'specversion' must be a string" )
118
- )
113
+ errors ["specversion" ].append (InvalidAttributeTypeError ("specversion" , str ))
119
114
if attributes .get ("specversion" ) != "1.0" :
120
115
errors ["specversion" ].append (
121
- InvalidAttributeTypeError ("Attribute 'specversion' must be '1.0'" )
116
+ InvalidAttributeValueError (
117
+ "specversion" , "Attribute 'specversion' must be '1.0'"
118
+ )
122
119
)
123
120
return errors
124
121
@@ -136,46 +133,43 @@ def _validate_optional_attributes(
136
133
137
134
if "time" in attributes :
138
135
if not isinstance (attributes ["time" ], datetime ):
139
- errors ["time" ].append (
140
- InvalidAttributeTypeError (
141
- "Attribute 'time' must be a datetime object"
142
- )
143
- )
136
+ errors ["time" ].append (InvalidAttributeTypeError ("time" , datetime ))
144
137
if hasattr (attributes ["time" ], "tzinfo" ) and not attributes ["time" ].tzinfo :
145
138
errors ["time" ].append (
146
- InvalidAttributeTypeError ("Attribute 'time' must be timezone aware" )
139
+ InvalidAttributeValueError (
140
+ "time" , "Attribute 'time' must be timezone aware"
141
+ )
147
142
)
148
143
if "subject" in attributes :
149
144
if not isinstance (attributes ["subject" ], str ):
150
- errors ["subject" ].append (
151
- InvalidAttributeTypeError ("Attribute 'subject' must be a string" )
152
- )
145
+ errors ["subject" ].append (InvalidAttributeTypeError ("subject" , str ))
153
146
if not attributes ["subject" ]:
154
147
errors ["subject" ].append (
155
- InvalidAttributeTypeError ("Attribute 'subject' must not be empty" )
148
+ InvalidAttributeValueError (
149
+ "subject" , "Attribute 'subject' must not be empty"
150
+ )
156
151
)
157
152
if "datacontenttype" in attributes :
158
153
if not isinstance (attributes ["datacontenttype" ], str ):
159
154
errors ["datacontenttype" ].append (
160
- InvalidAttributeTypeError (
161
- "Attribute 'datacontenttype' must be a string"
162
- )
155
+ InvalidAttributeTypeError ("datacontenttype" , str )
163
156
)
164
157
if not attributes ["datacontenttype" ]:
165
158
errors ["datacontenttype" ].append (
166
- InvalidAttributeTypeError (
167
- "Attribute 'datacontenttype' must not be empty"
159
+ InvalidAttributeValueError (
160
+ "datacontenttype" ,
161
+ "Attribute 'datacontenttype' must not be empty" ,
168
162
)
169
163
)
170
164
if "dataschema" in attributes :
171
165
if not isinstance (attributes ["dataschema" ], str ):
172
166
errors ["dataschema" ].append (
173
- InvalidAttributeTypeError ("Attribute ' dataschema' must be a string" )
167
+ InvalidAttributeTypeError ("dataschema" , str )
174
168
)
175
169
if not attributes ["dataschema" ]:
176
170
errors ["dataschema" ].append (
177
- InvalidAttributeTypeError (
178
- "Attribute 'dataschema' must not be empty"
171
+ InvalidAttributeValueError (
172
+ "dataschema" , " Attribute 'dataschema' must not be empty"
179
173
)
180
174
)
181
175
return errors
@@ -200,19 +194,22 @@ def _validate_extension_attributes(
200
194
if extension_attribute == "data" :
201
195
errors [extension_attribute ].append (
202
196
CustomExtensionAttributeError (
203
- "Extension attribute 'data' is reserved and must not be used"
197
+ extension_attribute ,
198
+ "Extension attribute 'data' is reserved and must not be used" ,
204
199
)
205
200
)
206
201
if not (1 <= len (extension_attribute ) <= 20 ):
207
202
errors [extension_attribute ].append (
208
203
CustomExtensionAttributeError (
209
- f"Extension attribute '{ extension_attribute } ' should be between 1 and 20 characters long"
204
+ extension_attribute ,
205
+ f"Extension attribute '{ extension_attribute } ' should be between 1 and 20 characters long" ,
210
206
)
211
207
)
212
208
if not re .match (r"^[a-z0-9]+$" , extension_attribute ):
213
209
errors [extension_attribute ].append (
214
210
CustomExtensionAttributeError (
215
- f"Extension attribute '{ extension_attribute } ' should only contain lowercase letters and numbers"
211
+ extension_attribute ,
212
+ f"Extension attribute '{ extension_attribute } ' should only contain lowercase letters and numbers" ,
216
213
)
217
214
)
218
215
return errors
0 commit comments