Parser
The Parser utility simplifies data parsing and validation using Pydantic. It allows you to define data models in pure Python classes, parse and validate incoming events, and extract only the data you need.
Usage Documentation
| FUNCTION | DESCRIPTION |
|---|---|
event_parser |
Lambda handler decorator to parse & validate events using Pydantic models |
parse |
Standalone function to parse & validate events using Pydantic models |
event_parser ¶
event_parser(handler: Callable[[Any, LambdaContext], ReturnT]) -> LambdaHandler[dict[str, Any] | str, LambdaContext, ReturnT]
event_parser(*, model: type[ModelT] | TypeAdapter[ModelT], envelope: None = None) -> _EventParserModelHandlerDecorator[ModelT]
event_parser(*, model: Any = None, envelope: type[BaseEnvelope] | None = None) -> _EventParserHandlerDecorator
event_parser(handler: Callable[..., EventParserReturnType], event: dict[str, Any], context: LambdaContext, model: type[T] | None = None, envelope: type[Envelope] | None = None, **kwargs: Any) -> EventParserReturnType
Lambda handler decorator to parse & validate events using Pydantic models
It requires a model that implements Pydantic BaseModel to parse & validate the event.
When an envelope is given, it'll use the following logic:
- Parse the event against the envelope model first e.g. EnvelopeModel(**event)
- Envelope will extract a given key to be parsed against the model e.g. event.detail
This is useful when you need to confirm event wrapper structure, and b) selectively extract a portion of your payload for parsing & validation.
NOTE: If envelope is omitted, the complete event is parsed to match the model parameter definition.
Example
Lambda handler decorator to parse & validate event
1 2 3 4 5 6 7 8 | |
Lambda handler decorator to parse & validate event - using built-in envelope
1 2 3 4 5 6 7 8 | |
| PARAMETER | DESCRIPTION |
|---|---|
handler
|
Method to annotate on
TYPE:
|
event
|
Lambda event to be parsed & validated |
context
|
Lambda context object
TYPE:
|
model
|
Your data model that will replace the event.
TYPE:
|
envelope
|
Optional envelope to extract the model from
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValidationError
|
When input event does not conform with the provided model |
InvalidModelTypeError
|
When the model given does not implement BaseModel, is not provided |
InvalidEnvelopeError
|
When envelope given does not implement BaseEnvelope |
Source code in aws_lambda_powertools/utilities/parser/parser.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
parse ¶
parse(event: dict[str, Any], model: type[T]) -> T
parse(event: dict[str, Any], model: type[T], envelope: type[Envelope]) -> T | list[T | None] | list[dict[str, T | None]] | None
parse(event: dict[str, Any], model: type[T], envelope: type[Envelope] | None = None)
Standalone function to parse & validate events using Pydantic models
Typically used when you need fine-grained control over error handling compared to event_parser decorator.
Example
Lambda handler decorator to parse & validate event
1 2 3 4 5 6 7 8 9 10 11 12 | |
Lambda handler decorator to parse & validate event - using built-in envelope
1 2 3 4 5 6 7 8 9 10 | |
| PARAMETER | DESCRIPTION |
|---|---|
event
|
Lambda event to be parsed & validated |
model
|
Your data model that will replace the event
TYPE:
|
envelope
|
Optional envelope to extract the model from
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValidationError
|
When input event does not conform with model provided |
InvalidModelTypeError
|
When model given does not implement BaseModel |
InvalidEnvelopeError
|
When envelope given does not implement BaseEnvelope |
Source code in aws_lambda_powertools/utilities/parser/parser.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |