diff --git a/aws_lambda_powertools/utilities/validation/base.py b/aws_lambda_powertools/utilities/validation/base.py index eb84f300ded..ec4165f4876 100644 --- a/aws_lambda_powertools/utilities/validation/base.py +++ b/aws_lambda_powertools/utilities/validation/base.py @@ -32,6 +32,7 @@ def validate_data_against_schema(data: Dict, schema: Dict, formats: Optional[Dic When JSON schema provided is invalid """ try: + formats = formats or {} fastjsonschema.validate(definition=schema, data=data, formats=formats) except (TypeError, AttributeError, fastjsonschema.JsonSchemaDefinitionException) as e: raise InvalidSchemaFormatError(f"Schema received: {schema}, Formats: {formats}. Error: {e}") diff --git a/tests/functional/validator/conftest.py b/tests/functional/validator/conftest.py index ab7a26012ba..b492e7846f1 100644 --- a/tests/functional/validator/conftest.py +++ b/tests/functional/validator/conftest.py @@ -565,3 +565,24 @@ def eventbridge_schema_registry_cloudtrail_v2_s3(): "x-amazon-events-detail-type": "AWS API Call via CloudTrail", "x-amazon-events-source": "aws.s3", } + + +@pytest.fixture +def schema_datetime_format(): + return { + "$schema": "http://json-schema.org/draft-07/schema", + "$id": "http://example.com/example.json", + "type": "object", + "title": "Sample schema with string date-time format", + "description": "The root schema comprises the entire JSON document.", + "required": ["message"], + "properties": { + "message": { + "$id": "#/properties/message", + "type": "string", + "format": "date-time", + "title": "The message", + "examples": ["hello world"], + }, + }, + } diff --git a/tests/functional/validator/test_validator.py b/tests/functional/validator/test_validator.py index 4a773571ddc..d8986ba90de 100644 --- a/tests/functional/validator/test_validator.py +++ b/tests/functional/validator/test_validator.py @@ -151,3 +151,12 @@ def _func_echo_decoder(self, value): envelope="powertools_json(data).payload", jmespath_options=jmespath_opts, ) + + +def test_validate_date_time_format(schema_datetime_format): + raw_event = {"message": "2021-06-29T14:46:06.804Z"} + validate(event=raw_event, schema=schema_datetime_format) + + invalid_datetime = {"message": "2021-06-29T14"} + with pytest.raises(exceptions.SchemaValidationError, match="data.message must be date-time"): + validate(event=invalid_datetime, schema=schema_datetime_format)