Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

feat: allow Hangul(korean character) in creating and renaming vfolder #59

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/ai/backend/common/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,12 +465,14 @@ def check_and_return(self, value: Any) -> datetime.timedelta:

class Slug(t.Trafaret, metaclass=StringLengthMeta):

_rx_slug = re.compile(r'^[a-zA-Z0-9]([a-zA-Z0-9._-]*[a-zA-Z0-9])?$')
_regexp = r'^((?!.*?\.{2,}))[\w.\-\/\\\s]+$'
_rx_slug = t.RegexpRaw(regexp=_regexp) # re.compile(r'^[\w\-.\s]+$')
lizable marked this conversation as resolved.
Show resolved Hide resolved

def __init__(self, *, min_length: Optional[int] = None, max_length: Optional[int] = None,
allow_dot: bool = False) -> None:
allow_dot: bool = False, ascii_only: bool = False) -> None:
super().__init__()
self._allow_dot = allow_dot
self._ascii_only = ascii_only
if min_length is not None and min_length < 0:
raise TypeError('min_length must be larger than or equal to zero.')
if max_length is not None and max_length < 0:
Expand All @@ -479,6 +481,8 @@ def __init__(self, *, min_length: Optional[int] = None, max_length: Optional[int
raise TypeError('min_length must be less than or equal to max_length when both set.')
self._min_length = min_length
self._max_length = max_length
if self._ascii_only:
type(self)._rx_slug = t.RegexpRaw(regexp=type(self)._regexp, re_flags=re.ASCII)
lizable marked this conversation as resolved.
Show resolved Hide resolved

def check_and_return(self, value: Any) -> str:
if isinstance(value, str):
Expand All @@ -490,8 +494,9 @@ def check_and_return(self, value: Any) -> str:
checked_value = value[1:]
else:
checked_value = value
m = type(self)._rx_slug.search(checked_value)
if not m:
try:
type(self)._rx_slug.check(checked_value)
except t.DataError:
self._failure('value must be a valid slug.', value=value)
else:
self._failure('value must be a string', value=value)
Expand Down
30 changes: 25 additions & 5 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,26 @@ def test_slug():
assert iv.check('a-b') == 'a-b'
assert iv.check('a_b') == 'a_b'

with pytest.raises(t.DataError):
iv.check('_')
with pytest.raises(t.DataError):
iv.check('')
# updates: '_' and empty string are allowed
# with pytest.raises(t.DataError):
# iv.check('_')
# with pytest.raises(t.DataError):
# iv.check('')
lizable marked this conversation as resolved.
Show resolved Hide resolved

iv = tx.Slug(allow_dot=True)
iv = tx.Slug(allow_dot=True, ascii_only=False)
assert iv.check('.a') == '.a'
assert iv.check('a') == 'a'
assert iv.check('.ㄱ') == '.ㄱ'
assert iv.check('ㄱ') == 'ㄱ'
assert iv.check('.Ç') == '.Ç'
assert iv.check('Ç') == 'Ç'
assert iv.check('.á') == '.á'
assert iv.check('á') == 'á'
assert iv.check('.あ') == '.あ'
assert iv.check('あ') == 'あ'
assert iv.check('.字') == '.字'
assert iv.check('字') == '字'

with pytest.raises(t.DataError):
iv.check('..a')

Expand Down Expand Up @@ -361,6 +373,14 @@ def test_slug():
with pytest.raises(TypeError):
tx.Slug[:-1]

# ascii only
iv = tx.Slug(allow_dot=True, ascii_only=True)
assert iv.check('.a') == '.a'
assert iv.check('a') == 'a'

with pytest.raises(t.DataError):
iv.check('.ㄱ')


def test_json_string():
iv = tx.JSONString()
Expand Down