44@validator
55def ipv4 (value ):
66 """
7- Return whether or not given value is a valid IP version 4 address.
7+ Return whether a given value is a valid IP version 4 address.
88
99 This validator is based on `WTForms IPAddress validator`_
1010
@@ -32,7 +32,7 @@ def ipv4(value):
3232@validator
3333def ipv4_cidr (value ):
3434 """
35- Return whether or not given value is a valid CIDR-notated IP version 4
35+ Return whether a given value is a valid CIDR-notated IP version 4
3636 address range.
3737
3838 This validator is based on RFC4632 3.1.
@@ -57,7 +57,7 @@ def ipv4_cidr(value):
5757@validator
5858def ipv6 (value ):
5959 """
60- Return whether or not given value is a valid IP version 6 address
60+ Return whether a given value is a valid IP version 6 address
6161 (including IPv4-mapped IPv6 addresses).
6262
6363 This validator is based on `WTForms IPAddress validator`_.
@@ -95,10 +95,6 @@ def ipv6(value):
9595 else :
9696 ipv4_groups = []
9797
98- max_groups = 6 if ipv4_groups else 8
99- if len (ipv6_groups ) > max_groups :
100- return False
101-
10298 count_blank = 0
10399 for part in ipv6_groups :
104100 if not part :
@@ -109,20 +105,32 @@ def ipv6(value):
109105 except ValueError :
110106 return False
111107 else :
112- if not 0 <= num <= 65536 :
108+ if not 0 <= num <= 65536 or len ( part ) > 4 :
113109 return False
114110
115- if count_blank < 2 :
111+ max_groups = 6 if ipv4_groups else 8
112+ part_count = len (ipv6_groups ) - count_blank
113+ if count_blank == 0 and part_count == max_groups :
114+ # no :: -> must have size of max_groups
115+ return True
116+ elif count_blank == 1 and ipv6_groups [- 1 ] and ipv6_groups [0 ] and part_count < max_groups :
117+ # one :: inside the address or prefix or suffix : -> filter least two cases
118+ return True
119+ elif count_blank == 2 and part_count < max_groups and (
120+ ((ipv6_groups [0 ] and not ipv6_groups [- 1 ]) or (not ipv6_groups [0 ] and ipv6_groups [- 1 ])) or ipv4_groups ):
121+ # leading or trailing :: or : at end and begin -> filter last case
122+ # Check if it has ipv4 groups because they get removed from the ipv6_groups
116123 return True
117- elif count_blank == 2 and not ipv6_groups [0 ] and not ipv6_groups [1 ]:
124+ elif count_blank == 3 and part_count == 0 :
125+ # :: is the address -> filter everything else
118126 return True
119127 return False
120128
121129
122130@validator
123131def ipv6_cidr (value ):
124132 """
125- Returns whether or not given value is a valid CIDR-notated IP version 6
133+ Returns whether a given value is a valid CIDR-notated IP version 6
126134 address range.
127135
128136 This validator is based on RFC4632 3.1.
0 commit comments