Skip to content

Commit 25c693d

Browse files
committed
refactor(validators): simplify validation logic and improve error messages for required, file, and CIDR checks
1 parent e28d700 commit 25c693d

3 files changed

Lines changed: 13 additions & 79 deletions

File tree

bugscanx/utils/cidr.py

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33

44

55
def get_hosts_from_cidr(cidr_input):
6-
"""
7-
Legacy function for backward compatibility.
8-
WARNING: This loads all IPs into memory. Use cidr_hosts_generator for large ranges.
9-
"""
106
hosts = []
117
cidr_ranges = [c.strip() for c in cidr_input.split(',')]
128

@@ -19,47 +15,6 @@ def get_hosts_from_cidr(cidr_input):
1915
return hosts
2016

2117

22-
# def cidr_hosts_generator(cidr_input):
23-
# """
24-
# Generator function that yields hosts from CIDR ranges without loading all in memory.
25-
# Use this for memory-efficient CIDR scanning.
26-
# """
27-
# cidr_ranges = [c.strip() for c in cidr_input.split(',')]
28-
29-
# for cidr in cidr_ranges:
30-
# try:
31-
# network = ipaddress.ip_network(cidr, strict=False)
32-
# for ip in network.hosts():
33-
# yield str(ip)
34-
# except ValueError:
35-
# continue
36-
37-
38-
def get_total_cidr_hosts(cidr_input):
39-
"""
40-
Calculate the total number of hosts in CIDR ranges without generating them.
41-
"""
42-
total = 0
43-
cidr_ranges = [c.strip() for c in cidr_input.split(',')]
44-
45-
for cidr in cidr_ranges:
46-
try:
47-
network = ipaddress.ip_network(cidr, strict=False)
48-
# For host addresses, subtract network and broadcast addresses
49-
total += max(0, network.num_addresses - 2)
50-
except ValueError:
51-
continue
52-
return total
53-
54-
55-
def validate_cidr(cidr):
56-
try:
57-
ipaddress.ip_network(cidr, strict=False)
58-
return True
59-
except ValueError:
60-
return False
61-
62-
6318
def read_cidrs_from_file(filepath):
6419
valid_cidrs = []
6520
try:

bugscanx/utils/common.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def get_input(
7676

7777
if validate_input and validators:
7878
if input_type == "number":
79-
validators = [lambda x: is_digit(x, allow_comma_separated)]
79+
validators = [required, lambda x: is_digit(x, allow_comma_separated)]
8080
common_params["validate"] = create_validator(validators)
8181

8282
input_type_params = {
@@ -95,8 +95,6 @@ def get_input(
9595
common_params.update(kwargs)
9696

9797
handler = INPUT_HANDLERS.get(input_type)
98-
if not handler:
99-
raise ValueError(f"Unsupported input_type: {input_type}")
10098

10199
return strip_handler(handler, strip_input)(common_params)
102100

bugscanx/utils/validators.py

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,47 +34,28 @@ def validate(self, document):
3434

3535

3636
def required(text):
37-
return bool(text.strip()) or "Input cannot be empty"
37+
return True if text.strip() else "Input is required."
3838

3939

4040
def is_file(text):
41-
return os.path.isfile(text) or f"File does not exist: {text}"
41+
return os.path.isfile(text) or f"File not found: {text}"
4242

4343

4444
def is_cidr(text):
4545
if not text.strip():
4646
return "CIDR input cannot be empty"
47-
48-
if text.endswith(',') or ',,' in text:
49-
return "Empty value after comma"
50-
51-
parts = [p.strip() for p in text.split(',')]
52-
for part in parts:
53-
if not part:
54-
return "Empty value between commas"
55-
try:
56-
ipaddress.ip_network(part, strict=False)
57-
except ValueError:
58-
return f"Invalid CIDR notation: {part}"
59-
return True
47+
48+
try:
49+
ipaddress.ip_network(text.strip(), strict=False)
50+
return True
51+
except ValueError:
52+
return f"Invalid CIDR notation: {text.strip()}"
6053

6154

6255
def is_digit(text, allow_comma=True):
63-
if not text.strip():
64-
return "Input cannot be empty"
65-
6656
if not allow_comma and ',' in text:
67-
return "Multi values are not allowed"
68-
69-
parts = text.split(',') if allow_comma else [text]
70-
71-
if text.endswith(',') or ',,' in text:
72-
return "Empty value after comma"
73-
74-
for part in parts:
75-
part = part.strip()
76-
if not part:
77-
return "Empty value between commas"
78-
if not part.isdigit():
79-
return f"Not a valid number: {part}"
57+
return "Only a single value allowed"
58+
59+
if not text.strip().replace(',', '').replace(' ', '').isdigit():
60+
return f"Invalid number: {text}"
8061
return True

0 commit comments

Comments
 (0)