Skip to content

Commit

Permalink
chore: Feedback for PR 2136
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Jan 4, 2024
1 parent b7664d6 commit 9c9dc54
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
16 changes: 15 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ python-dateutil = ">=2.8.2"
python-dotenv = ">=0.20"
pytz = ">=2022.2.1"
PyYAML = ">=6.0"
rfc3339-validator = ">=0.1.4"
requests = ">=2.25.1"
simpleeval = ">=0.9.13"
simplejson = ">=3.17.6"
Expand Down
11 changes: 11 additions & 0 deletions singer_sdk/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,14 @@ class InvalidJSONSchema(Exception):

class InvalidRecord(Exception):
"""Raised when a stream record is invalid according to its declared schema."""

def __init__(self, error_message: str, record: dict) -> None:
"""Initialize an InvalidRecord exception.
Args:
error_message: A message describing the error.
record: The invalid record.
"""
super().__init__(f"Record Message Validation Error: {error_message}")
self.error_message = error_message
self.record = record
14 changes: 8 additions & 6 deletions singer_sdk/sinks/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,18 @@ def __init__(
format_checker = jsonschema_validator.FORMAT_CHECKER
else:
format_checker = format_validators

try:
jsonschema_validator.check_schema(schema)
self.validator = jsonschema_validator(
schema=schema, format_checker=format_checker
)
except jsonschema.SchemaError as e:
error_message = f"Schema Validation Error: {e}"
raise InvalidJSONSchema(error_message) from e

self.validator = jsonschema_validator(
schema=schema,
format_checker=format_checker,
)

@override
def validate(self, record: dict): # noqa: ANN201
"""Validate a record message.
Expand All @@ -119,8 +122,7 @@ def validate(self, record: dict): # noqa: ANN201
try:
self.validator.validate(record)
except jsonschema.ValidationError as e:
error_message = f"Record Message Validation Error: {e.message}"
raise InvalidRecord(error_message) from e
raise InvalidRecord(e.message, record) from e


class Sink(metaclass=abc.ABCMeta):
Expand Down Expand Up @@ -443,7 +445,7 @@ def _validate_and_parse(self, record: dict) -> dict:
self._validator.validate(record)
except InvalidRecord as e:
if self.fail_on_record_validation_exception:
raise InvalidRecord(e) from e
raise
self.logger.exception("Record validation failed %s", e)

self._parse_timestamps_in_record(
Expand Down
4 changes: 2 additions & 2 deletions tests/core/sinks/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def test_validate_record_jsonschema_format_checking_enabled_stop_on_error(
}
with pytest.raises(
InvalidRecord,
match=r"data.invalid_datetime must be date-time",
match=r"Record Message Validation Error",
):
sink._validate_and_parse(record)

Expand Down Expand Up @@ -170,7 +170,7 @@ def test_validate_record_jsonschema_format_checking_enabled_continue_on_error(
)
assert updated_record["missing_datetime"] == "2021-01-01T00:00:00+00:00"
assert updated_record["invalid_datetime"] == "9999-12-31 23:59:59.999999"
assert "data.invalid_datetime must be date-time" in captured.err
assert "Record Message Validation Error" in captured.err


@pytest.fixture
Expand Down

0 comments on commit 9c9dc54

Please sign in to comment.