Skip to content

Commit 5463edc

Browse files
authored
Merge pull request #1328 from Sage-Bionetworks/fix_check_name
Separated check_name into org and schema functions
2 parents 57e1ff5 + ae178b6 commit 5463edc

2 files changed

Lines changed: 68 additions & 19 deletions

File tree

synapseclient/models/schema_organization.py

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ class SchemaOrganization(SchemaOrganizationProtocol):
254254

255255
def __post_init__(self) -> None:
256256
if self.name:
257-
_check_name(self.name)
257+
_check_org_name(self.name)
258258

259259
async def get_async(
260260
self, *, synapse_client: Optional["Synapse"] = None
@@ -826,9 +826,9 @@ class JSONSchema(JSONSchemaProtocol):
826826

827827
def __post_init__(self) -> None:
828828
if self.name:
829-
_check_name(self.name)
829+
_check_schema_name(self.name)
830830
if self.organization_name:
831-
_check_name(self.organization_name)
831+
_check_org_name(self.organization_name)
832832
if self.name and self.organization_name:
833833
self.uri = f"{self.organization_name}-{self.name}"
834834
else:
@@ -1313,8 +1313,8 @@ class CreateSchemaRequest(AsynchronousCommunicator):
13131313

13141314
def __post_init__(self) -> None:
13151315
self.concrete_type = CREATE_SCHEMA_REQUEST
1316-
_check_name(self.name)
1317-
_check_name(self.organization_name)
1316+
_check_schema_name(self.name)
1317+
_check_org_name(self.organization_name)
13181318
uri = f"{self.organization_name}-{self.name}"
13191319
if self.version:
13201320
self._check_semantic_version(self.version)
@@ -1428,9 +1428,9 @@ def list_json_schema_organizations(
14281428
return all_orgs
14291429

14301430

1431-
def _check_name(name: str) -> None:
1431+
def _check_org_name(name: str) -> None:
14321432
"""
1433-
Checks that the input name is a valid Synapse Organization or JSONSchema name
1433+
Checks that the input name is a valid Synapse Organization
14341434
- Length requirement of 6 ≤ x ≤ 250
14351435
- Names do not contain the string sagebionetworks (case insensitive)
14361436
- May contain periods (each part is separated by periods)
@@ -1449,11 +1449,42 @@ def _check_name(name: str) -> None:
14491449
raise ValueError(f"The name must not contain 'sagebionetworks' : {name}")
14501450
parts = name.split(".")
14511451
for part in parts:
1452-
if not re.match(r"^([A-Za-z])([A-Za-z]|\d|)*$", part):
1452+
if len(part) == 0:
1453+
raise ValueError(f"Organization name sections must not be empty: {name}")
1454+
if not re.match(r"^([A-Za-z])([A-Za-z0-9])*$", part):
14531455
raise ValueError(
14541456
(
1455-
"Name may be separated by periods, "
1456-
"but each part must start with a letter and contain "
1457-
f"only letters and numbers: {name}"
1457+
f"Un-allowed characters in organization name: {name}"
1458+
"Organization name may separated into sections by periods. "
1459+
"For example 'my.organization.name'. "
1460+
"Each section must start with a letter and contain only letters and numbers."
1461+
)
1462+
)
1463+
1464+
1465+
def _check_schema_name(name: str) -> None:
1466+
"""
1467+
Checks that the input name is a valid Synapse JSONSchema name
1468+
- May contain periods (each part is separated by periods)
1469+
- Each part must start with a letter
1470+
- Each part contains only letters and numbers
1471+
1472+
Arguments:
1473+
name: The name of the schema to be checked
1474+
1475+
Raises:
1476+
ValueError: When the name isn't valid
1477+
"""
1478+
parts = name.split(".")
1479+
for part in parts:
1480+
if len(part) == 0:
1481+
raise ValueError(f"Schema name sections must not be empty: {name}")
1482+
if not re.match(r"^([A-Za-z])([A-Za-z0-9])*$", part):
1483+
raise ValueError(
1484+
(
1485+
f"Un-allowed characters in schema name: {name}"
1486+
"Schema name may separated into sections by periods. "
1487+
"For example 'my.schema.name'. "
1488+
"Each section must start with a letter and contain only letters and numbers."
14581489
)
14591490
)

tests/unit/synapseclient/models/async/unit_test_schema_organization_async.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
CreateSchemaRequest,
1111
JSONSchema,
1212
SchemaOrganization,
13-
_check_name,
13+
_check_org_name,
14+
_check_schema_name,
1415
)
1516

1617
ORG_NAME = "mytest.organization"
@@ -97,42 +98,59 @@ def _get_acl_response():
9798
}
9899

99100

100-
class TestCheckName:
101-
"""Tests for the _check_name validation function."""
101+
class TestCheckOrgName:
102+
"""Tests for the _check_org_name validation function."""
102103

103104
def test_valid_name(self) -> None:
104105
# GIVEN a valid name
105106
# WHEN I check the name
106107
# THEN no exception should be raised
107-
_check_name("mytest.organization")
108+
_check_org_name("mytest.organization")
108109

109110
def test_name_too_short(self) -> None:
110111
# GIVEN a name that is too short
111112
# WHEN I check the name
112113
# THEN it should raise ValueError
113114
with pytest.raises(ValueError, match="length 6 to 250"):
114-
_check_name("abc")
115+
_check_org_name("abc")
115116

116117
def test_name_too_long(self) -> None:
117118
# GIVEN a name that is too long
118119
# WHEN I check the name
119120
# THEN it should raise ValueError
120121
with pytest.raises(ValueError, match="length 6 to 250"):
121-
_check_name("a" * 251)
122+
_check_org_name("a" * 251)
122123

123124
def test_name_contains_sagebionetworks(self) -> None:
124125
# GIVEN a name containing 'sagebionetworks'
125126
# WHEN I check the name
126127
# THEN it should raise ValueError
127128
with pytest.raises(ValueError, match="sagebionetworks"):
128-
_check_name("my.sagebionetworks.test")
129+
_check_org_name("my.sagebionetworks.test")
129130

130131
def test_name_part_starts_with_number(self) -> None:
131132
# GIVEN a name where a part starts with a number
132133
# WHEN I check the name
133134
# THEN it should raise ValueError
134135
with pytest.raises(ValueError, match="must start with a letter"):
135-
_check_name("mytest.1invalid")
136+
_check_org_name("mytest.1invalid")
137+
138+
139+
class TestCheckSchemaName:
140+
"""Tests for the _check_schema_name validation function."""
141+
142+
def test_valid_name(self) -> None:
143+
# GIVEN a valid name
144+
# WHEN I check the name
145+
# THEN no exception should be raised
146+
_check_schema_name("mytest.schema")
147+
148+
def test_name_part_starts_with_number(self) -> None:
149+
# GIVEN a name where a part starts with a number
150+
# WHEN I check the name
151+
# THEN it should raise ValueError
152+
with pytest.raises(ValueError, match="must start with a letter"):
153+
_check_schema_name("mytest.1invalid")
136154

137155

138156
class TestSchemaOrganization:

0 commit comments

Comments
 (0)