Skip to content

Commit

Permalink
fix(validator): handle built-in custom formats correctly (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
heitorlessa authored Jul 2, 2021
1 parent e6ab53c commit e1511b4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
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)

0 comments on commit e1511b4

Please sign in to comment.