Skip to content

Commit dcb763d

Browse files
committed
Return the output of fastjsonschema.validate()
1 parent 25816d8 commit dcb763d

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

aws_lambda_powertools/utilities/validation/base.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def validate_data_against_schema(
1414
formats: Optional[Dict] = None,
1515
handlers: Optional[Dict] = None,
1616
provider_options: Optional[Dict] = None,
17-
):
17+
) -> Union[Dict, str]:
1818
"""Validate dict data against given JSON Schema
1919
2020
Parameters
@@ -31,6 +31,12 @@ def validate_data_against_schema(
3131
Arguments that will be passed directly to the underlying validation call, in this case fastjsonchema.validate.
3232
For all supported arguments see: https://horejsek.github.io/python-fastjsonschema/#fastjsonschema.validate
3333
34+
Returns
35+
-------
36+
Dict
37+
The validated event. If the schema includes a `default` for fields that are not provided, they will be included
38+
in the response.
39+
3440
Raises
3541
------
3642
SchemaValidationError
@@ -42,7 +48,13 @@ def validate_data_against_schema(
4248
formats = formats or {}
4349
handlers = handlers or {}
4450
provider_options = provider_options or {}
45-
fastjsonschema.validate(definition=schema, data=data, formats=formats, handlers=handlers, **provider_options)
51+
return fastjsonschema.validate(
52+
definition=schema,
53+
data=data,
54+
formats=formats,
55+
handlers=handlers,
56+
**provider_options,
57+
)
4658
except (TypeError, AttributeError, fastjsonschema.JsonSchemaDefinitionException) as e:
4759
raise InvalidSchemaFormatError(f"Schema received: {schema}, Formats: {formats}. Error: {e}")
4860
except fastjsonschema.JsonSchemaValueException as e:

aws_lambda_powertools/utilities/validation/validator.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def validate(
172172
provider_options: Optional[Dict] = None,
173173
envelope: Optional[str] = None,
174174
jmespath_options: Optional[Dict] = None,
175-
):
175+
) -> Any:
176176
"""Standalone function to validate event data using a JSON Schema
177177
178178
Typically used when you need more control over the validation process.
@@ -245,6 +245,12 @@ def handler(event, context):
245245
validate(event=event, schema=json_schema_dict, envelope="awslogs.powertools_base64_gzip(data) | powertools_json(@).logEvents[*]")
246246
return event
247247
248+
Returns
249+
-------
250+
Dict
251+
The validated event. If the schema includes a `default` for fields that are not provided, they will be included
252+
in the response.
253+
248254
Raises
249255
------
250256
SchemaValidationError
@@ -261,7 +267,7 @@ def handler(event, context):
261267
jmespath_options=jmespath_options,
262268
)
263269

264-
validate_data_against_schema(
270+
return validate_data_against_schema(
265271
data=event,
266272
schema=schema,
267273
formats=formats,

tests/functional/validator/_fastjsonschema/test_validator.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ def test_validate_raw_event(schema, raw_event):
1616
validate(event=raw_event, schema=schema)
1717

1818

19+
def test_validate_raw_event_default(schema_default, raw_event_default):
20+
resp = validate(event=raw_event_default, schema=schema_default)
21+
assert resp["username"] == "blah blah"
22+
assert resp["message"] == "The default message"
23+
24+
1925
def test_validate_wrapped_event_raw_envelope(schema, wrapped_event):
2026
validate(event=wrapped_event, schema=schema, envelope="data.payload")
2127

tests/functional/validator/conftest.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,34 @@ def schema():
3030
}
3131

3232

33+
@pytest.fixture
34+
def schema_default():
35+
return {
36+
"$schema": "http://json-schema.org/draft-07/schema",
37+
"$id": "http://example.com/example.json",
38+
"type": "object",
39+
"title": "Sample schema",
40+
"description": "The root schema comprises the entire JSON document.",
41+
"examples": [{"message": "hello world", "username": "lessa"}, {"username": "lessa"}],
42+
"required": ["username"],
43+
"properties": {
44+
"message": {
45+
"$id": "#/properties/message",
46+
"type": "string",
47+
"title": "The message",
48+
"examples": ["hello world"],
49+
"default": "The default message",
50+
},
51+
"username": {
52+
"$id": "#/properties/username",
53+
"type": "string",
54+
"title": "The username",
55+
"examples": ["lessa"],
56+
},
57+
},
58+
}
59+
60+
3361
@pytest.fixture
3462
def schema_array():
3563
return {
@@ -137,6 +165,11 @@ def raw_event():
137165
return {"message": "hello hello", "username": "blah blah"}
138166

139167

168+
@pytest.fixture
169+
def raw_event_default():
170+
return {"username": "blah blah"}
171+
172+
140173
@pytest.fixture
141174
def wrapped_event():
142175
return {"data": {"payload": {"message": "hello hello", "username": "blah blah"}}}

0 commit comments

Comments
 (0)