1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Closes #609: Add min/max value and regex validation for custom fields

This commit is contained in:
Jeremy Stretch
2020-10-15 15:06:01 -04:00
parent e7d26ca5dc
commit 8781cf1c57
7 changed files with 135 additions and 7 deletions

View File

@ -1,6 +1,7 @@
import re
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.validators import _lazy_re_compile, BaseValidator, URLValidator
@ -29,3 +30,14 @@ class ExclusionValidator(BaseValidator):
def compare(self, a, b):
return a in b
def validate_regex(value):
"""
Checks that the value is a valid regular expression. (Don't confuse this with RegexValidator, which *uses* a regex
to validate a value.)
"""
try:
re.compile(value)
except re.error:
raise ValidationError(f"{value} is not a valid regular expression.")