From c056d6447d3ba5992163e840f8ce706ca2d31bc8 Mon Sep 17 00:00:00 2001 From: Norbert Chmiel Date: Sun, 5 Jun 2022 19:03:39 +0200 Subject: [PATCH] added ipv4 digit lengts validation (#191) Co-authored-by: Norbert Chmiel --- tests/test_ipv4.py | 1 + validators/ip_address.py | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/test_ipv4.py b/tests/test_ipv4.py index 3dac98d8..f0f2f372 100644 --- a/tests/test_ipv4.py +++ b/tests/test_ipv4.py @@ -19,6 +19,7 @@ def test_returns_true_on_valid_ipv4_address(address): ('1278.0.0.1',), ('127.0.0.abc',), ('900.200.100.75',), + ('0127.0.0.1',), ]) def test_returns_failed_validation_on_invalid_ipv4_address(address): assert isinstance(ipv4(address), ValidationFailure) diff --git a/validators/ip_address.py b/validators/ip_address.py index f8b138d1..e0c061db 100644 --- a/validators/ip_address.py +++ b/validators/ip_address.py @@ -23,8 +23,12 @@ def ipv4(value): :param value: IP address string to validate """ - groups = value.split('.') - if len(groups) != 4 or any(not x.isdigit() for x in groups): + groups = value.split(".") + if ( + len(groups) != 4 + or any(not x.isdigit() for x in groups) + or any(len(x) > 3 for x in groups) + ): return False return all(0 <= int(part) < 256 for part in groups)