Skip to content

Commit 650cf9b

Browse files
committed
Add provider_options validation arg, fix test JSON schema
1 parent fd933ea commit 650cf9b

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

aws_lambda_powertools/utilities/validation/base.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import Dict, Optional, Union
2+
from typing import Any, Dict, Optional, Union
33

44
import fastjsonschema # type: ignore
55

@@ -9,7 +9,11 @@
99

1010

1111
def validate_data_against_schema(
12-
data: Union[Dict, str], schema: Dict, formats: Optional[Dict] = None, handlers: Optional[Dict] = None
12+
data: Union[Dict, str],
13+
schema: Dict,
14+
formats: Optional[Dict] = None,
15+
handlers: Optional[Dict] = None,
16+
**provider_options: Any,
1317
):
1418
"""Validate dict data against given JSON Schema
1519
@@ -23,6 +27,8 @@ def validate_data_against_schema(
2327
Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool
2428
handlers: Dict
2529
Custom methods to retrieve remote schemes, keyed off of URI scheme
30+
**provider options: Dict, optional
31+
Arguments that will be passed directly to the underlying validate call
2632
2733
Raises
2834
------
@@ -34,7 +40,7 @@ def validate_data_against_schema(
3440
try:
3541
formats = formats or {}
3642
handlers = handlers or {}
37-
fastjsonschema.validate(definition=schema, data=data, formats=formats, handlers=handlers)
43+
fastjsonschema.validate(definition=schema, data=data, formats=formats, handlers=handlers, **provider_options)
3844
except (TypeError, AttributeError, fastjsonschema.JsonSchemaDefinitionException) as e:
3945
raise InvalidSchemaFormatError(f"Schema received: {schema}, Formats: {formats}. Error: {e}")
4046
except fastjsonschema.JsonSchemaValueException as e:

aws_lambda_powertools/utilities/validation/validator.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ def validator(
1717
inbound_schema: Optional[Dict] = None,
1818
inbound_formats: Optional[Dict] = None,
1919
inbound_handlers: Optional[Dict] = None,
20+
inbound_provider_options: Dict = {},
2021
outbound_schema: Optional[Dict] = None,
2122
outbound_formats: Optional[Dict] = None,
2223
outbound_handlers: Optional[Dict] = None,
24+
outbound_provider_options: Dict = {},
2325
envelope: str = "",
2426
jmespath_options: Optional[Dict] = None,
2527
**kwargs: Any,
@@ -50,6 +52,10 @@ def validator(
5052
Custom methods to retrieve remote schemes, keyed off of URI scheme
5153
outbound_handlers: Dict
5254
Custom methods to retrieve remote schemes, keyed off of URI scheme
55+
inbound_provider_options: Dict
56+
Arguments that will be passed directly to the underlying validate call for the inbound event
57+
outbound_provider_options: Dict
58+
Arguments that will be passed directly to the underlying validate call for the outbound event
5359
5460
Example
5561
-------
@@ -134,15 +140,23 @@ def handler(event, context):
134140
if inbound_schema:
135141
logger.debug("Validating inbound event")
136142
validate_data_against_schema(
137-
data=event, schema=inbound_schema, formats=inbound_formats, handlers=inbound_handlers
143+
data=event,
144+
schema=inbound_schema,
145+
formats=inbound_formats,
146+
handlers=inbound_handlers,
147+
**inbound_provider_options,
138148
)
139149

140150
response = handler(event, context, **kwargs)
141151

142152
if outbound_schema:
143153
logger.debug("Validating outbound event")
144154
validate_data_against_schema(
145-
data=response, schema=outbound_schema, formats=outbound_formats, handlers=outbound_handlers
155+
data=response,
156+
schema=outbound_schema,
157+
formats=outbound_formats,
158+
handlers=outbound_handlers,
159+
**outbound_provider_options,
146160
)
147161

148162
return response
@@ -153,6 +167,7 @@ def validate(
153167
schema: Dict,
154168
formats: Optional[Dict] = None,
155169
handlers: Optional[Dict] = None,
170+
provider_options: Dict = {},
156171
envelope: Optional[str] = None,
157172
jmespath_options: Optional[Dict] = None,
158173
):
@@ -174,6 +189,8 @@ def validate(
174189
Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool
175190
handlers: Dict
176191
Custom methods to retrieve remote schemes, keyed off of URI scheme
192+
provider_options: Dict
193+
Arguments that will be passed directly to the underlying validate call
177194
178195
Example
179196
-------
@@ -242,4 +259,4 @@ def handler(event, context):
242259
jmespath_options=jmespath_options,
243260
)
244261

245-
validate_data_against_schema(data=event, schema=schema, formats=formats, handlers=handlers)
262+
validate_data_against_schema(data=event, schema=schema, formats=formats, handlers=handlers, **provider_options)

tests/functional/validator/conftest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def schema_refs():
9595
"title": "Sample schema",
9696
"description": "Sample JSON Schema that references another schema",
9797
"examples": [{"parent_object": {"child_string": "hello world"}}],
98-
"required": "parent_object",
98+
"required": ["parent_object"],
9999
"properties": {
100100
"parent_object": {
101101
"$id": "#/properties/parent_object",
@@ -110,7 +110,7 @@ def schema_refs():
110110
"title": "Sample schema",
111111
"description": "Sample JSON Schema that is referenced by another schema",
112112
"examples": [{"child_string": "hello world"}],
113-
"required": "child_string",
113+
"required": ["child_string"],
114114
"properties": {
115115
"child_string": {
116116
"$id": "#/properties/child_string",

0 commit comments

Comments
 (0)