Fixes #12639 - Make sure name expansions throws a validation error on decrementing ranges (#13326)

* Fixes #12639 - Make sure name expansions throws a validation error on decrementing ranges

* Fix pep8

* Also fail on equal start & end values

---------

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
This commit is contained in:
kkthxbye 2023-08-11 17:53:16 +02:00 committed by GitHub
parent 40afe6cf36
commit 5de9d3f15f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 3 deletions

View File

@ -52,7 +52,10 @@ class ComponentCreateForm(forms.Form):
super().clean()
# Validate that all replication fields generate an equal number of values
pattern_count = len(self.cleaned_data[self.replication_fields[0]])
if not (patterns := self.cleaned_data.get(self.replication_fields[0])):
return
pattern_count = len(patterns)
for field_name in self.replication_fields:
value_count = len(self.cleaned_data[field_name])
if self.cleaned_data[field_name] and value_count != pattern_count:

View File

@ -60,6 +60,9 @@ def parse_alphanumeric_range(string):
except ValueError:
begin, end = dash_range, dash_range
if begin.isdigit() and end.isdigit():
if int(begin) >= int(end):
raise forms.ValidationError(f'Range "{dash_range}" is invalid.')
for n in list(range(int(begin), int(end) + 1)):
values.append(n)
else:
@ -71,6 +74,10 @@ def parse_alphanumeric_range(string):
# Not a valid range (more than a single character)
if not len(begin) == len(end) == 1:
raise forms.ValidationError(f'Range "{dash_range}" is invalid.')
if ord(begin) >= ord(end):
raise forms.ValidationError(f'Range "{dash_range}" is invalid.')
for n in list(range(ord(begin), ord(end) + 1)):
values.append(chr(n))
return values

View File

@ -264,8 +264,9 @@ class ExpandAlphanumeric(TestCase):
self.assertEqual(sorted(expand_alphanumeric_pattern('r[a-9]a')), [])
def test_invalid_range_bounds(self):
self.assertEqual(sorted(expand_alphanumeric_pattern('r[9-8]a')), [])
self.assertEqual(sorted(expand_alphanumeric_pattern('r[b-a]a')), [])
with self.assertRaises(forms.ValidationError):
sorted(expand_alphanumeric_pattern('r[9-8]a'))
sorted(expand_alphanumeric_pattern('r[b-a]a'))
def test_invalid_range_len(self):
with self.assertRaises(forms.ValidationError):