Skip to content

Commit 1102973

Browse files
committed
added validation for mind name
1 parent 6857c02 commit 1102973

File tree

4 files changed

+75
-3
lines changed

4 files changed

+75
-3
lines changed

minds/exceptions.py

+3
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ class Unauthorized(Exception):
1818
class UnknownError(Exception):
1919
...
2020

21+
22+
class MindNameInvalid(Exception):
23+
...

minds/minds.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from datetime import datetime
44

55
from openai import OpenAI
6-
6+
import minds.utils as utils
77
import minds.exceptions as exc
88

99
from minds.datasources import Datasource, DatabaseConfig
@@ -26,6 +26,11 @@ def __init__(
2626
self.client = client
2727
self.project = 'mindsdb'
2828

29+
if not utils.validate_mind_name(name):
30+
raise exc.MindNameInvalid("""
31+
Mind name should start with a letter and contain only letters, numbers or underscore, with a maximum of 32 characters.
32+
Spaces are not allowed.""")
33+
2934
self.name = name
3035
self.model_name = model_name
3136
self.provider = provider
@@ -74,6 +79,11 @@ def update(
7479
:param parameters, dict: alter other parameters of the mind, optional
7580
"""
7681
data = {}
82+
83+
if not utils.validate_mind_name(name):
84+
raise exc.MindNameInvalid("""
85+
Mind name should start with a letter and contain only letters, numbers or underscore, with a maximum of 32 characters.
86+
Spaces are not allowed.""")
7787

7888
if datasources is not None:
7989
ds_names = []
@@ -216,7 +226,11 @@ def get(self, name: str) -> Mind:
216226
:param name: name of the mind
217227
:return: a mind object
218228
"""
219-
229+
if not utils.validate_mind_name(name):
230+
raise exc.MindNameInvalid("""
231+
Mind name should start with a letter and contain only letters, numbers or underscore, with a maximum of 32 characters.
232+
Spaces are not allowed.""")
233+
220234
item = self.api.get(f'/projects/{self.project}/minds/{name}').json()
221235
return Mind(self.client, **item)
222236

@@ -261,6 +275,11 @@ def create(
261275
:param replace: if true - to remove existing mind, default is false
262276
:return: created mind
263277
"""
278+
279+
if not utils.validate_mind_name(name):
280+
raise exc.MindNameInvalid("""
281+
Mind name should start with a letter and contain only letters, numbers or underscore, with a maximum of 32 characters.
282+
Spaces are not allowed.""")
264283

265284
if replace:
266285
try:
@@ -305,4 +324,9 @@ def drop(self, name: str):
305324
:param name: name of the mind
306325
"""
307326

327+
if not utils.validate_mind_name(name):
328+
raise exc.MindNameInvalid("""
329+
Mind name should start with a letter and contain only letters, numbers or underscore, with a maximum of 32 characters.
330+
Spaces are not allowed.""")
331+
308332
self.api.delete(f'/projects/{self.project}/minds/{name}')

minds/utils.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import re
2+
3+
def validate_mind_name(mind_name):
4+
"""
5+
Validate the Mind name.
6+
7+
A valid Mind name should:
8+
- Start with a letter
9+
- Contain only letters, numbers, or underscores
10+
- Have a maximum length of 32 characters
11+
- Not contain spaces
12+
13+
Parameters:
14+
mind_name (str): The Mind name to validate.
15+
16+
Returns:
17+
bool: True if valid, False otherwise.
18+
"""
19+
# Regular expression pattern
20+
pattern = r'^[A-Za-z][A-Za-z0-9_]{0,31}$'
21+
22+
# Check if the Mind name matches the pattern
23+
return re.match(pattern, mind_name)

tests/integration/test_base_flow.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from minds.datasources.examples import example_ds
1111

12-
from minds.exceptions import ObjectNotFound
12+
from minds.exceptions import ObjectNotFound, MindNameInvalid
1313

1414

1515
def get_client():
@@ -57,6 +57,7 @@ def test_minds():
5757
ds_name = 'test_datasource_'
5858
ds_name2 = 'test_datasource2_'
5959
mind_name = 'int_test_mind_'
60+
invalid_mind_name = 'mind-123'
6061
mind_name2 = 'int_test_mind2_'
6162
prompt1 = 'answer in german'
6263
prompt2 = 'answer in spanish'
@@ -79,6 +80,13 @@ def test_minds():
7980
ds2_cfg.tables = ['home_rentals']
8081

8182
# create
83+
with pytest.raises(MindNameInvalid):
84+
mind = client.minds.create(
85+
invalid_mind_name,
86+
datasources=[ds],
87+
provider='openai'
88+
)
89+
8290
mind = client.minds.create(
8391
mind_name,
8492
datasources=[ds],
@@ -95,6 +103,9 @@ def test_minds():
95103
mind = client.minds.get(mind_name)
96104
assert len(mind.datasources) == 2
97105
assert mind.prompt_template == prompt1
106+
107+
with pytest.raises(MindNameInvalid):
108+
client.minds.get(invalid_mind_name)
98109

99110
# list
100111
mind_list = client.minds.list()
@@ -106,6 +117,14 @@ def test_minds():
106117
datasources=[ds.name],
107118
prompt_template=prompt2
108119
)
120+
121+
with pytest.raises(MindNameInvalid):
122+
mind.update(
123+
name=invalid_mind_name,
124+
datasources=[ds.name],
125+
prompt_template=prompt2
126+
)
127+
109128
with pytest.raises(ObjectNotFound):
110129
# this name not exists
111130
client.minds.get(mind_name)
@@ -153,3 +172,6 @@ def test_minds():
153172
client.minds.drop(mind_name2)
154173
client.datasources.drop(ds.name)
155174
client.datasources.drop(ds2_cfg.name)
175+
176+
with pytest.raises(MindNameInvalid):
177+
client.minds.drop(invalid_mind_name)

0 commit comments

Comments
 (0)