Skip to content

Commit e1511b4

Browse files
authored
fix(validator): handle built-in custom formats correctly (#498)
1 parent e6ab53c commit e1511b4

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

Diff for: aws_lambda_powertools/utilities/validation/base.py

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def validate_data_against_schema(data: Dict, schema: Dict, formats: Optional[Dic
3232
When JSON schema provided is invalid
3333
"""
3434
try:
35+
formats = formats or {}
3536
fastjsonschema.validate(definition=schema, data=data, formats=formats)
3637
except (TypeError, AttributeError, fastjsonschema.JsonSchemaDefinitionException) as e:
3738
raise InvalidSchemaFormatError(f"Schema received: {schema}, Formats: {formats}. Error: {e}")

Diff for: tests/functional/validator/conftest.py

+21
Original file line numberDiff line numberDiff line change
@@ -565,3 +565,24 @@ def eventbridge_schema_registry_cloudtrail_v2_s3():
565565
"x-amazon-events-detail-type": "AWS API Call via CloudTrail",
566566
"x-amazon-events-source": "aws.s3",
567567
}
568+
569+
570+
@pytest.fixture
571+
def schema_datetime_format():
572+
return {
573+
"$schema": "http://json-schema.org/draft-07/schema",
574+
"$id": "http://example.com/example.json",
575+
"type": "object",
576+
"title": "Sample schema with string date-time format",
577+
"description": "The root schema comprises the entire JSON document.",
578+
"required": ["message"],
579+
"properties": {
580+
"message": {
581+
"$id": "#/properties/message",
582+
"type": "string",
583+
"format": "date-time",
584+
"title": "The message",
585+
"examples": ["hello world"],
586+
},
587+
},
588+
}

Diff for: tests/functional/validator/test_validator.py

+9
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,12 @@ def _func_echo_decoder(self, value):
151151
envelope="powertools_json(data).payload",
152152
jmespath_options=jmespath_opts,
153153
)
154+
155+
156+
def test_validate_date_time_format(schema_datetime_format):
157+
raw_event = {"message": "2021-06-29T14:46:06.804Z"}
158+
validate(event=raw_event, schema=schema_datetime_format)
159+
160+
invalid_datetime = {"message": "2021-06-29T14"}
161+
with pytest.raises(exceptions.SchemaValidationError, match="data.message must be date-time"):
162+
validate(event=invalid_datetime, schema=schema_datetime_format)

0 commit comments

Comments
 (0)