Skip to content

Commit

Permalink
Merge pull request #151 from dbt-labs/backport-0.2.latest--disallow-d…
Browse files Browse the repository at this point in the history
…unders-in-object-names

[BACKPORT 0.2.latest] Disallow dunders in object names (#150)
  • Loading branch information
QMalcolm authored Sep 18, 2023
2 parents e1e8a43 + cdbf75e commit 65664b5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230914-134708.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Disallow dunders semantic object names
time: 2023-09-14T13:47:08.907492-07:00
custom:
Author: QMalcolm
Issue: "149"
13 changes: 9 additions & 4 deletions dbt_semantic_interfaces/validations/unique_valid_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ class UniqueAndValidNameRule(SemanticManifestValidationRule[SemanticManifestT],
* Names of semantic models, dimension sets and metric sets in the model are unique / valid.
"""

NAME_REGEX = re.compile(r"\A[a-z][a-z0-9_]*[a-z0-9]\Z")
# name must start with a lower case letter
# name must end with a number or lower case letter
# name may include lower case letters, numbers, and underscores
# name may not contain dunders (two sequential underscores
NAME_REGEX = re.compile(r"\A[a-z]((?!__)[a-z0-9_])*[a-z0-9]\Z")

@staticmethod
def check_valid_name(name: str, context: Optional[ValidationContext] = None) -> List[ValidationIssue]: # noqa: D
Expand All @@ -69,9 +73,10 @@ def check_valid_name(name: str, context: Optional[ValidationContext] = None) ->
issues.append(
ValidationError(
context=context,
message=f"Invalid name `{name}` - names should only consist of lower case letters, numbers, "
f"and underscores. In addition, names should start with a lower case letter, and should not end "
f"with an underscore, and they must be at least 2 characters long.",
message=f"Invalid name `{name}` - names may only contain lower case letters, numbers, "
f"and underscores. Additionally, names must start with a lower case letter, cannot end "
f"with an underscore, cannot contain dunders (double underscores, or __), and must be "
f"at least 2 characters long.",
)
)
if name.upper() in TimeGranularity.list_names():
Expand Down
1 change: 1 addition & 0 deletions tests/validations/test_unique_valid_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def test_invalid_names() -> None: # noqa:D
assert UniqueAndValidNameRule.check_valid_name("_no_leading_underscore") != []
assert UniqueAndValidNameRule.check_valid_name("no_trailing_underscore_") != []
assert UniqueAndValidNameRule.check_valid_name("_definitely_no_leading_and_trailing_underscore_") != []
assert UniqueAndValidNameRule.check_valid_name("name__with__dunders") != []

# time granularity values are reserved
assert UniqueAndValidNameRule.check_valid_name("day") != []
Expand Down

0 comments on commit 65664b5

Please sign in to comment.