Skip to content

fix(validator): handle built-in custom formats correctly #498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions aws_lambda_powertools/utilities/validation/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
21 changes: 21 additions & 0 deletions tests/functional/validator/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
},
},
}
9 changes: 9 additions & 0 deletions tests/functional/validator/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)