Skip to content

Commit fbde330

Browse files
committed
adds new ValidateInLineCondition component
1 parent f03dc9f commit fbde330

File tree

8 files changed

+383
-60
lines changed

8 files changed

+383
-60
lines changed

airbyte_cdk/sources/declarative/declarative_component_schema.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4275,6 +4275,7 @@ definitions:
42754275
description: The condition that the specified config value will be evaluated against
42764276
anyOf:
42774277
- "$ref": "#/definitions/ValidateAdheresToSchema"
4278+
- "$ref": "#/definitions/ValidateInLineCondition"
42784279
PredicateValidator:
42794280
title: Predicate Validator
42804281
description: Validator that applies a validation strategy to a specified value.
@@ -4303,12 +4304,24 @@ definitions:
43034304
- "test-value"
43044305
- "{{ config['api_version'] }}"
43054306
- "{{ config['tenant_id'] }}"
4307+
- "{{ config['start_date'] < now_utc() }}"
43064308
- 123
43074309
validation_strategy:
43084310
title: Validation Strategy
43094311
description: The validation strategy to apply to the value.
43104312
anyOf:
43114313
- "$ref": "#/definitions/ValidateAdheresToSchema"
4314+
- "$ref": "#/definitions/ValidateInLineCondition"
4315+
ValidateInLineCondition:
4316+
title: Validate In Line Condition
4317+
description: Validation strategy that evalutaes the value as an InterpolatedBoolean.
4318+
type: object
4319+
required:
4320+
- type
4321+
properties:
4322+
type:
4323+
type: string
4324+
enum: [ValidateInLineCondition]
43124325
ValidateAdheresToSchema:
43134326
title: Validate Adheres To Schema
43144327
description: Validates that a user-provided schema adheres to a specified JSON schema.

airbyte_cdk/sources/declarative/models/declarative_component_schema.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
2+
13
# generated by datamodel-codegen:
24
# filename: declarative_component_schema.yaml
35

@@ -1532,6 +1534,10 @@ class Config:
15321534
query: Dict[str, Any] = Field(..., description="The GraphQL query to be executed")
15331535

15341536

1537+
class ValidateInLineCondition(BaseModel):
1538+
type: Literal["ValidateInLineCondition"]
1539+
1540+
15351541
class ValidateAdheresToSchema(BaseModel):
15361542
type: Literal["ValidateAdheresToSchema"]
15371543
base_schema: Union[str, Dict[str, Any]] = Field(
@@ -2003,7 +2009,7 @@ class DpathValidator(BaseModel):
20032009
],
20042010
title="Field Path",
20052011
)
2006-
validation_strategy: ValidateAdheresToSchema = Field(
2012+
validation_strategy: Union[ValidateAdheresToSchema, ValidateInLineCondition] = Field(
20072013
...,
20082014
description="The condition that the specified config value will be evaluated against",
20092015
title="Validation Strategy",
@@ -2019,11 +2025,12 @@ class PredicateValidator(BaseModel):
20192025
"test-value",
20202026
"{{ config['api_version'] }}",
20212027
"{{ config['tenant_id'] }}",
2028+
"{{ config['start_date'] < now_utc() }}",
20222029
123,
20232030
],
20242031
title="Value",
20252032
)
2026-
validation_strategy: ValidateAdheresToSchema = Field(
2033+
validation_strategy: Union[ValidateAdheresToSchema, ValidateInLineCondition] = Field(
20272034
...,
20282035
description="The validation strategy to apply to the value.",
20292036
title="Validation Strategy",

airbyte_cdk/sources/declarative/parsers/model_to_component_factory.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,9 @@
424424
from airbyte_cdk.sources.declarative.models.declarative_component_schema import (
425425
ValidateAdheresToSchema as ValidateAdheresToSchemaModel,
426426
)
427+
from airbyte_cdk.sources.declarative.models.declarative_component_schema import (
428+
ValidateInLineCondition as ValidateInLineConditionModel,
429+
)
427430
from airbyte_cdk.sources.declarative.models.declarative_component_schema import ValueType
428431
from airbyte_cdk.sources.declarative.models.declarative_component_schema import (
429432
WaitTimeFromHeader as WaitTimeFromHeaderModel,
@@ -567,6 +570,7 @@
567570
DpathValidator,
568571
PredicateValidator,
569572
ValidateAdheresToSchema,
573+
ValidateInLineCondition,
570574
)
571575
from airbyte_cdk.sources.http_logger import format_http_message
572576
from airbyte_cdk.sources.message import (
@@ -894,6 +898,12 @@ def create_validate_adheres_to_schema(
894898
schema=base_schema,
895899
)
896900

901+
@staticmethod
902+
def create_validate_in_line_condition(
903+
model: ValidateInLineConditionModel, config: Config, **kwargs: Any
904+
) -> ValidateInLineCondition:
905+
return ValidateInLineCondition(config=config)
906+
897907
@staticmethod
898908
def create_added_field_definition(
899909
model: AddedFieldDefinitionModel, config: Config, **kwargs: Any

airbyte_cdk/sources/declarative/validators/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from airbyte_cdk.sources.declarative.validators.validate_adheres_to_schema import (
88
ValidateAdheresToSchema,
99
)
10+
from airbyte_cdk.sources.declarative.validators.validate_in_line_condition import (
11+
ValidateInLineCondition,
12+
)
1013
from airbyte_cdk.sources.declarative.validators.validation_strategy import ValidationStrategy
1114
from airbyte_cdk.sources.declarative.validators.validator import Validator
1215

@@ -16,4 +19,5 @@
1619
"ValidationStrategy",
1720
"ValidateAdheresToSchema",
1821
"PredicateValidator",
22+
"ValidateInLineCondition",
1923
]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#
2+
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
3+
#
4+
5+
from dataclasses import dataclass
6+
from typing import Any
7+
8+
from jinja2.exceptions import TemplateError
9+
10+
from airbyte_cdk.sources.declarative.interpolation.interpolated_boolean import InterpolatedBoolean
11+
from airbyte_cdk.sources.declarative.validators.validation_strategy import ValidationStrategy
12+
from airbyte_cdk.sources.types import Config
13+
14+
15+
@dataclass
16+
class ValidateInLineCondition(ValidationStrategy):
17+
"""
18+
Validation strategy that evaluates the argument as an InterpolatedBoolean.
19+
"""
20+
21+
config: Config
22+
23+
def validate(self, value: Any) -> None:
24+
"""
25+
Validates the argument as an InterpolatedBoolean.
26+
27+
:param value: The value to validate
28+
:raises ValueError: If the condition is not a string or evaluates to False
29+
"""
30+
31+
if isinstance(value, str):
32+
interpolated_condition = InterpolatedBoolean(value, parameters={})
33+
try:
34+
result = interpolated_condition.eval(self.config)
35+
except TemplateError as e:
36+
raise ValueError(f"Invalid jinja expression: {value}.") from e
37+
except Exception as e:
38+
raise ValueError(f"Unexpected error evaluating condition: {value}.") from e
39+
40+
if not result:
41+
raise ValueError(f"Condition evaluated to False: {value}.")
42+
else:
43+
raise ValueError(f"Invalid condition argument: {value}. Should be a string.")

0 commit comments

Comments
 (0)