mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-27 02:48:38 -06:00
Introduce MultipleOfValidator
This commit is contained in:
parent
3cda074cbd
commit
93bd2ee5b8
@ -1,3 +1,4 @@
|
|||||||
|
import decimal
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
@ -10,6 +11,7 @@ __all__ = (
|
|||||||
'ColorValidator',
|
'ColorValidator',
|
||||||
'EnhancedURLValidator',
|
'EnhancedURLValidator',
|
||||||
'ExclusionValidator',
|
'ExclusionValidator',
|
||||||
|
'MultipleOfValidator',
|
||||||
'validate_regex',
|
'validate_regex',
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -54,6 +56,22 @@ class ExclusionValidator(BaseValidator):
|
|||||||
return a in b
|
return a in b
|
||||||
|
|
||||||
|
|
||||||
|
class MultipleOfValidator(BaseValidator):
|
||||||
|
"""
|
||||||
|
Checks that a field's value is a numeric multiple of the given value. Both values are
|
||||||
|
cast as Decimals for comparison.
|
||||||
|
"""
|
||||||
|
def __init__(self, multiple):
|
||||||
|
self.multiple = decimal.Decimal(str(multiple))
|
||||||
|
super().__init__(limit_value=None)
|
||||||
|
|
||||||
|
def __call__(self, value):
|
||||||
|
if decimal.Decimal(str(value)) % self.multiple != 0:
|
||||||
|
raise ValidationError(
|
||||||
|
_("{value} must be a multiple of {multiple}.").format(value=value, multiple=self.multiple)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def validate_regex(value):
|
def validate_regex(value):
|
||||||
"""
|
"""
|
||||||
Checks that the value is a valid regular expression. (Don't confuse this with RegexValidator, which *uses* a regex
|
Checks that the value is a valid regular expression. (Don't confuse this with RegexValidator, which *uses* a regex
|
||||||
|
Loading…
Reference in New Issue
Block a user