Skip to content

Commit 8e4d946

Browse files
authored
fix: dpath_validator should not raise when key does not exist (#591)
1 parent 99a1a96 commit 8e4d946

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

airbyte_cdk/sources/declarative/validators/dpath_validator.py

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

5+
import logging
56
from dataclasses import dataclass
6-
from typing import Any, List, Union
7+
from typing import Any, List
78

89
import dpath.util
910

1011
from airbyte_cdk.sources.declarative.interpolation.interpolated_string import InterpolatedString
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+
1417

1518
@dataclass
1619
class DpathValidator(Validator):
@@ -47,17 +50,16 @@ def validate(self, input_data: dict[str, Any]) -> None:
4750
if "*" in path:
4851
try:
4952
values = dpath.values(input_data, path)
50-
if not values:
51-
return
5253
for value in values:
5354
self.strategy.validate(value)
5455
except KeyError as e:
55-
raise ValueError(f"Error validating path '{self.field_path}': {e}")
56+
logger.warning(f"Error validating path. Key not found: {e}")
57+
return
58+
5659
else:
5760
try:
5861
value = dpath.get(input_data, path)
59-
if not value:
60-
return
6162
self.strategy.validate(value)
6263
except KeyError as e:
63-
raise ValueError(f"Error validating path '{self.field_path}': {e}")
64+
logger.warning(f"Error validating path. Key not found: {e}")
65+
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": "[email protected]"}}}
43+
test_data = {"user": {"profile": {"email": "[email protected]", "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)