Skip to content

Commit b1d20f3

Browse files
committed
fix dpath key error
1 parent 9dbf2f2 commit b1d20f3

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

airbyte_cdk/sources/declarative/validators/dpath_validator.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
33
#
44

5+
import logging
56
from dataclasses import dataclass
67
from typing import Any, List
78

@@ -11,6 +12,9 @@
1112
from airbyte_cdk.sources.declarative.validators.validation_strategy import ValidationStrategy
1213
from airbyte_cdk.sources.declarative.validators.validator import Validator
1314

15+
logger = logging.getLogger("airbyte")
16+
17+
1418
@dataclass
1519
class DpathValidator(Validator):
1620
"""
@@ -49,10 +53,13 @@ def validate(self, input_data: dict[str, Any]) -> None:
4953
for value in values:
5054
self.strategy.validate(value)
5155
except KeyError as e:
56+
logger.warning(f"Error validating path. Key not found: {e}")
5257
return
58+
5359
else:
5460
try:
5561
value = dpath.get(input_data, path)
5662
self.strategy.validate(value)
5763
except KeyError as e:
64+
logger.warning(f"Error validating path. Key not found: {e}")
5865
return

unit_tests/sources/declarative/validators/test_dpath_validator.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,18 @@ def test_given_valid_path_and_input_validate_is_successful(self):
3434
assert strategy.validate_called
3535
assert strategy.validated_value
3636

37-
def test_given_invalid_path_when_validate_then_raise_key_error(self):
37+
38+
class TestDpathValidator(TestCase):
39+
def test_given_valid_top_level_path_and_input_validate_is_successful(self):
3840
strategy = MockValidationStrategy()
39-
validator = DpathValidator(field_path=["user", "profile", "phone"], strategy=strategy)
41+
validator = DpathValidator(field_path=["user"], strategy=strategy)
4042

41-
test_data = {"user": {"profile": {"email": "test@example.com"}}}
43+
test_data = {"user": {"profile": {"email": "test@example.com", "name": "Test User"}}}
4244

43-
with pytest.raises(ValueError) as context:
44-
validator.validate(test_data)
45+
validator.validate(test_data)
4546

46-
assert "Error validating path" in str(context.value)
47-
assert not strategy.validate_called
47+
assert strategy.validate_called
48+
assert strategy.validated_value
4849

4950
def test_given_strategy_fails_when_validate_then_raise_value_error(self):
5051
error_message = "Invalid email format"
@@ -53,7 +54,7 @@ def test_given_strategy_fails_when_validate_then_raise_value_error(self):
5354

5455
test_data = {"user": {"email": "invalid-email"}}
5556

56-
with pytest.raises(ValueError) as context:
57+
with pytest.raises(ValueError):
5758
validator.validate(test_data)
5859

5960
assert strategy.validate_called
@@ -67,15 +68,6 @@ def test_given_empty_path_list_when_validate_then_validate_raises_exception(self
6768
with pytest.raises(ValueError):
6869
validator.validate(test_data)
6970

70-
def test_given_empty_input_data_when_validate_then_validate_raises_exception(self):
71-
strategy = MockValidationStrategy()
72-
validator = DpathValidator(field_path=["data", "field"], strategy=strategy)
73-
74-
test_data = {}
75-
76-
with pytest.raises(ValueError):
77-
validator.validate(test_data)
78-
7971
def test_path_with_wildcard_when_validate_then_validate_is_successful(self):
8072
strategy = MockValidationStrategy()
8173
validator = DpathValidator(field_path=["users", "*", "email"], strategy=strategy)

0 commit comments

Comments
 (0)