5
5
class Validate :
6
6
@staticmethod
7
7
def this_email (email_address : str ) -> bool :
8
- if (" " in email_address or "@" not in email_address ) and (not (1 <= len (email_address ) <= 320 )):
9
- return False
10
- local_part , domain_part = email_address .rsplit ("@" , 1 )
11
- if not local_part or not domain_part or "." not in domain_part :
12
- return False
13
- domain_labels = domain_part .split ("." )
14
- if (not all (label .isalnum () for label in domain_labels )) and (not 2 <= len (domain_labels [- 1 ]) <= 63 ):
15
- return False
16
- return True
8
+ if (1 <= len (email_address ) <= 320 ) and " " not in email_address and "@" in email_address :
9
+ local_part , domain_part = email_address .rsplit ("@" , 1 )
10
+ if local_part and domain_part and "." in domain_part :
11
+ domain_labels = domain_part .split ("." )
12
+ if all (label .isalnum () for label in domain_labels ) and 2 <= len (domain_labels [- 1 ]) <= 63 :
13
+ return True
14
+ return False
17
15
18
16
@staticmethod
19
17
def _url_by_parsing (url : str ) -> dict | bool :
@@ -55,49 +53,56 @@ def this_url(cls, url: str, text_error: bool = False,
55
53
enforce_https : bool = False ) -> bool :
56
54
parsed_url = cls ._url_by_parsing (url )
57
55
if not parsed_url :
58
- error_message = 'Invalid URL: Reason - URL is empty or does not contain a protocol'
59
- return error_message if text_error else False
60
-
61
- # Validate protocol
62
- if parsed_url ['protocol' ] not in ['http' , 'https' ]:
63
- error_message = 'Invalid URL: Reason - Protocol is not HTTP or HTTPS'
64
- return error_message if text_error else False
65
- elif enforce_https and parsed_url ['protocol' ] != 'https' :
66
- error_message = 'Invalid URL: Reason - HTTPS protocol is enforced'
67
- return error_message if text_error else False
68
-
69
- # Validate hostname
70
- hostname = parsed_url ['hostname' ]
56
+ return cls .__return_error ('Invalid URL: Reason - URL is empty or does not contain a protocol' , text_error )
57
+
58
+ if not cls .__validate_protocol (parsed_url ['protocol' ], enforce_https , text_error ):
59
+ return False
60
+
61
+ if not cls .__validate_hostname (parsed_url ['hostname' ], text_error ):
62
+ return False
63
+
64
+ if not cls .__validate_filename (parsed_url ['filename' ], text_error ):
65
+ return False
66
+
67
+ if not parsed_url ['TLD' ]:
68
+ return cls .__return_error ('Invalid URL: Reason - TLD is not registered' , text_error )
69
+
70
+ return True
71
+
72
+ @staticmethod
73
+ def __return_error (message : str , text_error : bool ) -> bool :
74
+ return message if text_error else False
75
+
76
+ @classmethod
77
+ def __validate_protocol (cls , protocol : str , enforce_https : bool , text_error : bool ) -> bool :
78
+ if protocol not in ['http' , 'https' ]:
79
+ return cls .__return_error ('Invalid URL: Reason - Protocol is not HTTP or HTTPS' , text_error )
80
+ if enforce_https and protocol != 'https' :
81
+ return cls .__return_error ('Invalid URL: Reason - HTTPS protocol is enforced' , text_error )
82
+ return True
83
+
84
+ @classmethod
85
+ def __validate_hostname (cls , hostname : str , text_error : bool ) -> bool :
71
86
if len (hostname ) > 253 :
72
- error_message = 'Invalid URL: Reason - Hostname is too long'
73
- return error_message if text_error else False
87
+ return cls .__return_error ('Invalid URL: Reason - Hostname is too long' , text_error )
74
88
if hostname .count ('.' ) < 1 :
75
- error_message = 'Invalid URL: Reason - Hostname does not contain a domain'
76
- return error_message if text_error else False
89
+ return cls .__return_error ('Invalid URL: Reason - Hostname does not contain a domain' , text_error )
77
90
hostname_labels = hostname .split ('.' )
78
91
for label in hostname_labels :
79
92
if not all (c .isalnum () or c == '-' for c in label ):
80
- error_message = 'Invalid URL: Reason - Invalid character in hostname'
81
- return error_message if text_error else False
93
+ return cls .__return_error ('Invalid URL: Reason - Invalid character in hostname' , text_error )
82
94
if label .startswith ('-' ) or label .endswith ('-' ):
83
- error_message = 'Invalid URL: Reason - Label starts or ends with a hyphen'
84
- return error_message if text_error else False
95
+ return cls .__return_error ('Invalid URL: Reason - Label starts or ends with a hyphen' , text_error )
85
96
if label .isdigit ():
86
- error_message = 'Invalid URL: Reason - Label is a number'
87
- return error_message if text_error else False
97
+ return cls . __return_error ( 'Invalid URL: Reason - Label is a number' , text_error )
98
+ return True
88
99
89
- # Validate filename
90
- filename = parsed_url [ 'filename' ]
100
+ @ classmethod
101
+ def __validate_filename ( cls , filename : str , text_error : bool ) -> bool :
91
102
if len (filename ) > 256 :
92
- error_message = 'Invalid URL: Reason - Filename is too long'
93
- return error_message if text_error else False
103
+ return cls .__return_error ('Invalid URL: Reason - Filename is too long' , text_error )
94
104
if ' ' in filename :
95
- error_message = 'Invalid URL: Reason - Filename contains spaces'
96
- return error_message if text_error else False
97
-
98
- if not parsed_url ['TLD' ]:
99
- error_message = 'Invalid URL: Reason - TLD is not registered'
100
- return error_message if text_error else False
105
+ return cls .__return_error ('Invalid URL: Reason - Filename contains spaces' , text_error )
101
106
return True
102
107
103
108
@staticmethod
@@ -196,8 +201,8 @@ def __init__(self):
196
201
"""
197
202
pass
198
203
199
- @staticmethod
200
- def __luhn_algorithm (card_number : str ) -> bool :
204
+ @classmethod
205
+ def __luhn_algorithm (cls , card_number : str ) -> bool :
201
206
"""
202
207
Validates a card number using the Luhn algorithm.
203
208
@@ -209,19 +214,27 @@ def __luhn_algorithm(card_number: str) -> bool:
209
214
"""
210
215
if len (card_number ) < 13 or len (card_number ) > 19 :
211
216
return False
217
+
212
218
num_list = [int (digit ) for digit in card_number ]
213
219
num_list .reverse ()
214
- total = 0
215
- for i , num in enumerate (num_list ):
216
- if i % 2 == 1 :
217
- doubled = num * 2
218
- if doubled > 9 :
219
- doubled -= 9
220
- total += doubled
221
- else :
222
- total += num
220
+
221
+ total = sum (cls .__luhn_double (num ) if i % 2 == 1 else num for i , num in enumerate (num_list ))
223
222
return total % 10 == 0
224
223
224
+ @staticmethod
225
+ def __luhn_double (num : int ) -> int :
226
+ """
227
+ Doubles the number and subtracts 9 if the result is greater than 9.
228
+
229
+ Args:
230
+ num (int): The number to be doubled.
231
+
232
+ Returns:
233
+ int: The processed number.
234
+ """
235
+ doubled = num * 2
236
+ return doubled - 9 if doubled > 9 else doubled
237
+
225
238
@classmethod
226
239
def american_express (cls , card_number : str ) -> bool :
227
240
"""
0 commit comments