Skip to content
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

feat(targets): Allow users to disable schema validation in targets #2208

Merged
merged 1 commit into from
Jan 31, 2024
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
11 changes: 11 additions & 0 deletions singer_sdk/helpers/capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@
default=False,
),
).to_dict()
TARGET_VALIDATE_RECORDS_CONFIG = PropertiesList(
Property(
"validate_records",
BooleanType(),
description="Whether to validate the schema of the incoming streams.",
default=True,
),
).to_dict()


class TargetLoadMethods(str, Enum):
Expand Down Expand Up @@ -355,3 +363,6 @@ class TargetCapabilities(CapabilitiesEnum):

#: Allow setting the target schema.
TARGET_SCHEMA = "target-schema"

#: Validate the schema of the incoming records.
VALIDATE_RECORDS = "validate-records"
13 changes: 10 additions & 3 deletions singer_sdk/sinks/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import json
import time
import typing as t
from functools import cached_property
from gzip import GzipFile
from gzip import open as gzip_open
from types import MappingProxyType
Expand Down Expand Up @@ -131,9 +132,6 @@ class Sink(metaclass=abc.ABCMeta):

MAX_SIZE_DEFAULT = 10000

validate_schema = True
"""Enable JSON schema record validation."""

validate_field_string_format = False
"""Enable JSON schema format validation, for example `date-time` string fields."""

Expand Down Expand Up @@ -186,6 +184,15 @@ def __init__(

self._validator: BaseJSONSchemaValidator | None = self.get_validator()

@cached_property
def validate_schema(self) -> bool:
"""Enable JSON schema record validation.

Returns:
True if JSON schema validation is enabled.
"""
return self.config.get("validate_records", True)

def get_validator(self) -> BaseJSONSchemaValidator | None:
"""Get a record validator for this sink.

Expand Down
5 changes: 5 additions & 0 deletions singer_sdk/target_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
TARGET_HARD_DELETE_CONFIG,
TARGET_LOAD_METHOD_CONFIG,
TARGET_SCHEMA_CONFIG,
TARGET_VALIDATE_RECORDS_CONFIG,
CapabilitiesEnum,
PluginCapabilities,
TargetCapabilities,
Expand Down Expand Up @@ -104,6 +105,7 @@ def capabilities(self) -> list[CapabilitiesEnum]:
PluginCapabilities.ABOUT,
PluginCapabilities.STREAM_MAPS,
PluginCapabilities.FLATTENING,
TargetCapabilities.VALIDATE_RECORDS,
]

@property
Expand Down Expand Up @@ -614,6 +616,9 @@ def _merge_missing(source_jsonschema: dict, target_jsonschema: dict) -> None:
if PluginCapabilities.BATCH in capabilities:
_merge_missing(BATCH_CONFIG, config_jsonschema)

if TargetCapabilities.VALIDATE_RECORDS in capabilities:
_merge_missing(TARGET_VALIDATE_RECORDS_CONFIG, config_jsonschema)

super().append_builtin_config(config_jsonschema)


Expand Down
3 changes: 2 additions & 1 deletion tests/core/test_target_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
MissingKeyPropertiesError,
RecordsWithoutSchemaException,
)
from singer_sdk.helpers.capabilities import PluginCapabilities
from singer_sdk.helpers.capabilities import PluginCapabilities, TargetCapabilities
from tests.conftest import BatchSinkMock, SQLSinkMock, SQLTargetMock, TargetMock


Expand Down Expand Up @@ -67,6 +67,7 @@ def test_target_about_info():
PluginCapabilities.ABOUT,
PluginCapabilities.STREAM_MAPS,
PluginCapabilities.FLATTENING,
TargetCapabilities.VALIDATE_RECORDS,
PluginCapabilities.BATCH,
]

Expand Down
Loading