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[..., 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
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 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 |
|
parse ¶
parse(event: dict[str, Any], model: type[T]) -> T
parse(
event: dict[str, Any],
model: type[T],
envelope: type[Envelope],
) -> T
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
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 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 |
|