Skip to content

Commit 27701de

Browse files
Merge pull request #48 from Better-Boy/patch-2
Datasource name validation
2 parents 7a50aed + 3ca8e0b commit 27701de

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

minds/datasources/datasources.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import List, Optional, Union
22

33
from pydantic import BaseModel, Field
4-
4+
import minds.utils as utils
55
import minds.exceptions as exc
66

77
class DatabaseConfig(BaseModel):
@@ -37,6 +37,8 @@ def create(self, ds_config: DatabaseConfig, update=False):
3737

3838
name = ds_config.name
3939

40+
utils.validate_datasource_name(name)
41+
4042
if update:
4143
self.api.put(f'/datasources/{name}', data=ds_config.model_dump())
4244
else:

minds/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ class UnknownError(Exception):
2020

2121

2222
class MindNameInvalid(Exception):
23+
...
24+
25+
26+
class DatasourceNameInvalid(Exception):
2327
...

minds/utils.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,27 @@ def validate_mind_name(mind_name):
3838
# Check if the Mind name matches the pattern
3939
if not re.match(pattern, mind_name):
4040
raise exc.MindNameInvalid("Mind name should start with a letter and contain only letters, numbers or underscore, with a maximum of 32 characters. Spaces are not allowed.")
41+
42+
43+
def validate_datasource_name(ds_name):
44+
"""
45+
Validate the datasource name.
46+
47+
A valid datasource name should:
48+
- Start with a letter
49+
- Contain only letters, numbers, or underscores
50+
- Have a maximum length of 62 characters
51+
- Not contain spaces
52+
53+
Parameters:
54+
ds_name (str): The datasource name to validate.
55+
56+
Returns:
57+
bool: True if valid, False otherwise.
58+
"""
59+
# Regular expression pattern
60+
pattern = r'^[a-zA-Z][a-zA-Z0-9_]{0,61}$'
61+
62+
# Check if the datasource name matches the pattern
63+
if not re.match(pattern, ds_name):
64+
raise exc.DatasourceNameInvalid("Datasource name should start with a letter and contain only letters, numbers or underscore, with a maximum of 62 characters. Spaces are not allowed.")

tests/integration/test_base_flow.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
logging.basicConfig(level=logging.DEBUG)
99

1010
from minds.datasources.examples import example_ds
11+
from minds.datasources import DatabaseConfig
1112

12-
from minds.exceptions import ObjectNotFound, MindNameInvalid
13+
from minds.exceptions import ObjectNotFound, MindNameInvalid, DatasourceNameInvalid
1314

1415

1516
def get_client():
@@ -41,6 +42,14 @@ def test_datasources():
4142
ds = client.datasources.create(example_ds, update=True)
4243
assert ds.name == example_ds.name
4344

45+
valid_ds_name = example_ds.name
46+
47+
with pytest.raises(DatasourceNameInvalid):
48+
example_ds.name = "invalid-ds-name"
49+
client.datasources.create(example_ds)
50+
51+
example_ds.name = valid_ds_name
52+
4453
# get
4554
ds = client.datasources.get(example_ds.name)
4655

0 commit comments

Comments
 (0)