Skip to content

Commit 1ca7d21

Browse files
committed
As per the spec, only dotted-quad IPv4 addresses are valid.
Closes: python-jsonschema#149
1 parent 11490df commit 1ca7d21

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

jsonschema/_format.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,15 @@ def is_email(instance):
123123
return "@" in instance
124124

125125

126-
@_checks_drafts(draft3="ip-address", draft4="ipv4", raises=socket.error)
126+
_ipv4_re = re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
127+
128+
@_checks_drafts(draft3="ip-address", draft4="ipv4")
127129
def is_ipv4(instance):
128130
if not isinstance(instance, str_types):
129131
return True
130-
return socket.inet_aton(instance)
132+
if not _ipv4_re.match(instance):
133+
return False
134+
return all(0 <= int(component) <= 255 for component in instance.split("."))
131135

132136

133137
if hasattr(socket, "inet_pton"):

0 commit comments

Comments
 (0)