Skip to content

Commit

Permalink
Improve IPv6 validation
Browse files Browse the repository at this point in the history
fixes #107
  • Loading branch information
SimonIT committed Feb 15, 2022
1 parent 29df081 commit 4154d1f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
13 changes: 12 additions & 1 deletion tests/test_ipv6.py
Expand Up @@ -5,8 +5,10 @@


@pytest.mark.parametrize(('address',), [
('::',),
('::1',),
('dead:beef:0:0:0:0:42:1',),
('1::',),
('dead:beef:0:0:0:0000:42:1',),
('abcd:ef::42:1',),
('0:0:0:0:0:ffff:1.2.3.4',),
('::192.168.30.2',),
Expand All @@ -21,6 +23,15 @@ def test_returns_true_on_valid_ipv6_address(address):
('abcd:1234::123::1',),
('1:2:3:4:5:6:7:8:9',),
('abcd::1ffff',),
('1111:',),
(':8888',),
(':1.2.3.4',),
('18:05',),
(':',),
(':1:2:',),
(':1:2::',),
('::1:2::',),
('8::1:2::9',),
])
def test_returns_failed_validation_on_invalid_ipv6_address(address):
assert isinstance(ipv6(address), ValidationFailure)
12 changes: 9 additions & 3 deletions validators/ip_address.py
@@ -1,3 +1,5 @@
from operator import xor

from .utils import validator


Expand Down Expand Up @@ -57,7 +59,7 @@ def ipv4_cidr(value):
@validator
def ipv6(value):
"""
Return whether or not given value is a valid IP version 6 address
Return whether a given value is a valid IP version 6 address
(including IPv4-mapped IPv6 addresses).
This validator is based on `WTForms IPAddress validator`_.
Expand Down Expand Up @@ -112,9 +114,13 @@ def ipv6(value):
if not 0 <= num <= 65536:
return False

if count_blank < 2:
if count_blank == 0 and len(ipv6_groups) == max_groups:
return True
elif count_blank == 1 and ipv6_groups[-1] and ipv6_groups[0]:
return True
elif count_blank == 2 and ((ipv6_groups[0] and not ipv6_groups[-1]) or (not ipv6_groups[0] and ipv6_groups[-1])):
return True
elif count_blank == 2 and not ipv6_groups[0] and not ipv6_groups[1]:
elif count_blank == 3 and len(ipv6_groups) == 3:
return True
return False

Expand Down

0 comments on commit 4154d1f

Please sign in to comment.