mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 14:03:30 +00:00
realm filters: Make validation error messages more useful.
This commit is contained in:
@@ -565,7 +565,8 @@ post_delete.connect(flush_realm_emoji, sender=RealmEmoji)
|
|||||||
|
|
||||||
def filter_pattern_validator(value: str) -> None:
|
def filter_pattern_validator(value: str) -> None:
|
||||||
regex = re.compile(r'^(?:(?:[\w\-#_= /:]*|[+]|[!])(\(\?P<\w+>.+\)))+$')
|
regex = re.compile(r'^(?:(?:[\w\-#_= /:]*|[+]|[!])(\(\?P<\w+>.+\)))+$')
|
||||||
error_msg = 'Invalid filter pattern, you must use the following format OPTIONAL_PREFIX(?P<id>.+)'
|
error_msg = _('Invalid filter pattern. Valid characters are %s.' % (
|
||||||
|
'[ a-zA-Z_#=/:+!-]',))
|
||||||
|
|
||||||
if not regex.match(str(value)):
|
if not regex.match(str(value)):
|
||||||
raise ValidationError(error_msg)
|
raise ValidationError(error_msg)
|
||||||
@@ -580,8 +581,7 @@ def filter_format_validator(value: str) -> None:
|
|||||||
regex = re.compile(r'^([\.\/:a-zA-Z0-9#_?=-]+%\(([a-zA-Z0-9_-]+)\)s)+[a-zA-Z0-9_-]*$')
|
regex = re.compile(r'^([\.\/:a-zA-Z0-9#_?=-]+%\(([a-zA-Z0-9_-]+)\)s)+[a-zA-Z0-9_-]*$')
|
||||||
|
|
||||||
if not regex.match(value):
|
if not regex.match(value):
|
||||||
raise ValidationError('URL format string must be in the following format: '
|
raise ValidationError(_('Invalid URL format string.'))
|
||||||
r'`https://example.com/%(\w+)s`')
|
|
||||||
|
|
||||||
class RealmFilter(models.Model):
|
class RealmFilter(models.Model):
|
||||||
"""Realm-specific regular expressions to automatically linkify certain
|
"""Realm-specific regular expressions to automatically linkify certain
|
||||||
|
|||||||
@@ -30,11 +30,11 @@ class RealmFilterTest(ZulipTestCase):
|
|||||||
|
|
||||||
data['pattern'] = '$a'
|
data['pattern'] = '$a'
|
||||||
result = self.client_post("/json/realm/filters", info=data)
|
result = self.client_post("/json/realm/filters", info=data)
|
||||||
self.assert_json_error(result, 'Invalid filter pattern, you must use the following format OPTIONAL_PREFIX(?P<id>.+)')
|
self.assert_json_error(result, 'Invalid filter pattern. Valid characters are [ a-zA-Z_#=/:+!-].')
|
||||||
|
|
||||||
data['pattern'] = r'ZUL-(?P<id>\d++)'
|
data['pattern'] = r'ZUL-(?P<id>\d++)'
|
||||||
result = self.client_post("/json/realm/filters", info=data)
|
result = self.client_post("/json/realm/filters", info=data)
|
||||||
self.assert_json_error(result, 'Invalid filter pattern, you must use the following format OPTIONAL_PREFIX(?P<id>.+)')
|
self.assert_json_error(result, 'Invalid filter pattern. Valid characters are [ a-zA-Z_#=/:+!-].')
|
||||||
|
|
||||||
data['pattern'] = r'ZUL-(?P<id>\d+)'
|
data['pattern'] = r'ZUL-(?P<id>\d+)'
|
||||||
data['url_format_string'] = '$fgfg'
|
data['url_format_string'] = '$fgfg'
|
||||||
@@ -44,7 +44,7 @@ class RealmFilterTest(ZulipTestCase):
|
|||||||
data['pattern'] = r'ZUL-(?P<id>\d+)'
|
data['pattern'] = r'ZUL-(?P<id>\d+)'
|
||||||
data['url_format_string'] = 'https://realm.com/my_realm_filter/'
|
data['url_format_string'] = 'https://realm.com/my_realm_filter/'
|
||||||
result = self.client_post("/json/realm/filters", info=data)
|
result = self.client_post("/json/realm/filters", info=data)
|
||||||
self.assert_json_error(result, 'URL format string must be in the following format: `https://example.com/%(\\w+)s`')
|
self.assert_json_error(result, 'Invalid URL format string.')
|
||||||
|
|
||||||
data['url_format_string'] = 'https://realm.com/my_realm_filter/#hashtag/%(id)s'
|
data['url_format_string'] = 'https://realm.com/my_realm_filter/#hashtag/%(id)s'
|
||||||
result = self.client_post("/json/realm/filters", info=data)
|
result = self.client_post("/json/realm/filters", info=data)
|
||||||
|
|||||||
Reference in New Issue
Block a user