Skip to content

Commit 8c3a84b

Browse files
committed
docs: document new handlers param in validation utilities
1 parent d4270f1 commit 8c3a84b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

docs/utilities/validation.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,33 @@ You can use our built-in [JMESPath functions](./jmespath_functions.md){target="_
199199

200200
???+ info
201201
We use these for [built-in envelopes](#built-in-envelopes) to easily to decode and unwrap events from sources like Kinesis, CloudWatch Logs, etc.
202+
203+
### Validating with external references
204+
205+
JSON schema [allows schemas to reference other schemas](https://json-schema.org/understanding-json-schema/structuring#dollarref) using the `$ref` keyword. The value of `$ref` is a URI reference, but you likely don't want to launch an HTTP request to resolve this URI. Instead, you can pass resolving functions through the `handlers` parameter:
206+
207+
```python title="custom_reference_handlers.py"
208+
from aws_lambda_powertools.utilities.validation import validate
209+
210+
SCHEMA = {
211+
"ParentSchema": {
212+
"type": "object",
213+
"properties": {
214+
"child_object": {"$ref": "testschema://ChildSchema"},
215+
...
216+
},
217+
...
218+
},
219+
"ChildSchema": ...,
220+
}
221+
222+
def handle_test_schema(uri):
223+
schema_key = uri.split("://")[1]
224+
return SCHEMA[schema_key]
225+
226+
test_schema_handlers = {"testschema": handle_test_schema}
227+
228+
parent_event = {"child_object": {...}}
229+
230+
validate(event=parent_event, schema=SCHEMA["ParentSchema"], handlers=test_schema_handlers)
231+
```

0 commit comments

Comments
 (0)