Skip to content

Commit

Permalink
#12278 to_internal_value is called before validation! need to raise v…
Browse files Browse the repository at this point in the history
…alidation error if incorrect format
  • Loading branch information
arthanson committed Apr 18, 2023
1 parent 95e37e4 commit 38f1b91
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions netbox/ipam/api/nested_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ipam.models.l2vpn import L2VPNTermination, L2VPN
from ipam.validators import validate_ipaddress_with_mask
from netbox.api.serializers import WritableNestedSerializer
from netaddr import IPNetwork
from netaddr import AddrFormatError, IPNetwork

__all__ = [
'IPAddressField',
Expand Down Expand Up @@ -48,7 +48,12 @@ def __init__(self, **kwargs):
self.validators.append(validator)

def to_internal_value(self, data):
return IPNetwork(data)
try:
IPNetwork(data)
except AddrFormatError:
raise serializers.ValidationError("Invalid IP address format: {}".format(data))
except (TypeError, ValueError) as e:
raise serializers.ValidationError(e)

def to_representation(self, value):
return str(value)
Expand Down

0 comments on commit 38f1b91

Please sign in to comment.