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 all 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
1 change: 1 addition & 0 deletions changes/59.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix regex to accept `unicode` mode and add parameter `ascii_only` to enable toggle mode in the validation check.
11 changes: 8 additions & 3 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])?$')
_rx_slug_ascii_only = re.compile(r'^[\w\-_.\s]+$', re.ASCII)
_rx_slug = re.compile(r'^[\w\-_.\s]+$')
Comment on lines +468 to +469
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It additionally allows whitespaces compared to the previous version.
Please make it to stand out in the news fragment, and reflect that change in the test cases as well.
Also the code has "ascii_only" argument but the fragment says "unicode" mode -- please make them consistent.

Copy link
Member

@achimnol achimnol Mar 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's update as follows:

Update the conditions for Slug for existing use cases:

  • disallow the characters outside the regex [\w\-.] (no whitespace allowed!)
  • disallow consecutive dots such as .., ..., ... in any position
  • disallow zero-length (empty) strings

Add a new validator DirectoryNameComponent for vfolder name validation:

  • disallow the characters outside the regex [\w\s\-.] (whitespace allowed!)
  • disallow consecutive dots such as .., ..., ... in any position
  • disallow zero-length (empty) strings


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 @@ -490,7 +492,10 @@ 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 self._ascii_only:
m = type(self)._rx_slug_ascii_only.search(checked_value)
else:
m = type(self)._rx_slug.search(checked_value)
if not m:
self._failure('value must be a valid slug.', value=value)
else:
Expand Down
25 changes: 19 additions & 6 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,25 @@ def test_slug():
assert iv.check('abc') == 'abc'
assert iv.check('a-b') == 'a-b'
assert iv.check('a_b') == 'a_b'

with pytest.raises(t.DataError):
iv.check('_')
assert iv.check('_') == '_'
with pytest.raises(t.DataError):
iv.check('')

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'
with pytest.raises(t.DataError):
iv.check('..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('字') == '字'

assert iv.check('..a') == '..a'

iv = tx.Slug[:4]
assert iv.check('abc') == 'abc'
Expand Down Expand Up @@ -361,6 +369,11 @@ 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'


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