Skip to content

Commit

Permalink
fix(cli/properties): fix data type validation (#12170)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsheth2 authored Dec 19, 2024
1 parent da8f822 commit 4392d72
Showing 1 changed file with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
PropertyValueClass,
StructuredPropertyDefinitionClass,
)
from datahub.metadata.urns import StructuredPropertyUrn, Urn
from datahub.metadata.urns import DataTypeUrn, StructuredPropertyUrn, Urn
from datahub.utilities.urns._urn_base import URN_TYPES

logging.basicConfig(level=logging.INFO)
Expand Down Expand Up @@ -86,19 +86,31 @@ class StructuredProperties(ConfigModel):

@validator("type")
def validate_type(cls, v: str) -> str:
# Convert to lowercase if needed
if not v.islower():
# This logic is somewhat hacky, since we need to deal with
# 1. fully qualified urns
# 2. raw data types, that need to get the datahub namespace prefix
# While keeping the user-facing interface and error messages clean.

if not v.startswith("urn:li:") and not v.islower():
# Convert to lowercase if needed
v = v.lower()
logger.warning(
f"Structured property type should be lowercase. Updated to {v.lower()}"
f"Structured property type should be lowercase. Updated to {v}"
)
v = v.lower()

urn = Urn.make_data_type_urn(v)

# Check if type is allowed
if not AllowedTypes.check_allowed_type(v):
data_type_urn = DataTypeUrn.from_string(urn)
unqualified_data_type = data_type_urn.id
if unqualified_data_type.startswith("datahub."):
unqualified_data_type = unqualified_data_type[len("datahub.") :]
if not AllowedTypes.check_allowed_type(unqualified_data_type):
raise ValueError(
f"Type {v} is not allowed. Allowed types are {AllowedTypes.values()}"
f"Type {unqualified_data_type} is not allowed. Allowed types are {AllowedTypes.values()}"
)
return v

return urn

@property
def fqn(self) -> str:
Expand Down

0 comments on commit 4392d72

Please sign in to comment.