Skip to content

fix: dpath_validator should not raise when key does not exist #591

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
merged 2 commits into from
Jun 9, 2025
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
16 changes: 9 additions & 7 deletions airbyte_cdk/sources/declarative/validators/dpath_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
#

import logging
from dataclasses import dataclass
from typing import Any, List, Union
from typing import Any, List

import dpath.util

from airbyte_cdk.sources.declarative.interpolation.interpolated_string import InterpolatedString
from airbyte_cdk.sources.declarative.validators.validation_strategy import ValidationStrategy
from airbyte_cdk.sources.declarative.validators.validator import Validator

logger = logging.getLogger("airbyte")


@dataclass
class DpathValidator(Validator):
Expand Down Expand Up @@ -47,17 +50,16 @@ def validate(self, input_data: dict[str, Any]) -> None:
if "*" in path:
try:
values = dpath.values(input_data, path)
if not values:
return
for value in values:
self.strategy.validate(value)
except KeyError as e:
raise ValueError(f"Error validating path '{self.field_path}': {e}")
logger.warning(f"Error validating path. Key not found: {e}")
return

else:
try:
value = dpath.get(input_data, path)
if not value:
return
self.strategy.validate(value)
except KeyError as e:
raise ValueError(f"Error validating path '{self.field_path}': {e}")
logger.warning(f"Error validating path. Key not found: {e}")
return
26 changes: 9 additions & 17 deletions unit_tests/sources/declarative/validators/test_dpath_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,18 @@ def test_given_valid_path_and_input_validate_is_successful(self):
assert strategy.validate_called
assert strategy.validated_value

def test_given_invalid_path_when_validate_then_raise_key_error(self):

class TestDpathValidator(TestCase):
def test_given_valid_top_level_path_and_input_validate_is_successful(self):
strategy = MockValidationStrategy()
validator = DpathValidator(field_path=["user", "profile", "phone"], strategy=strategy)
validator = DpathValidator(field_path=["user"], strategy=strategy)

test_data = {"user": {"profile": {"email": "test@example.com"}}}
test_data = {"user": {"profile": {"email": "test@example.com", "name": "Test User"}}}

with pytest.raises(ValueError) as context:
validator.validate(test_data)
validator.validate(test_data)

assert "Error validating path" in str(context.value)
assert not strategy.validate_called
assert strategy.validate_called
assert strategy.validated_value

def test_given_strategy_fails_when_validate_then_raise_value_error(self):
error_message = "Invalid email format"
Expand All @@ -53,7 +54,7 @@ def test_given_strategy_fails_when_validate_then_raise_value_error(self):

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

with pytest.raises(ValueError) as context:
with pytest.raises(ValueError):
validator.validate(test_data)

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

def test_given_empty_input_data_when_validate_then_validate_raises_exception(self):
strategy = MockValidationStrategy()
validator = DpathValidator(field_path=["data", "field"], strategy=strategy)

test_data = {}

with pytest.raises(ValueError):
validator.validate(test_data)

def test_path_with_wildcard_when_validate_then_validate_is_successful(self):
strategy = MockValidationStrategy()
validator = DpathValidator(field_path=["users", "*", "email"], strategy=strategy)
Expand Down
Loading