pyessential 0.1.9__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyessential-0.1.9 → pyessential-0.2.0}/CHANGELOG.md +10 -1
- {pyessential-0.1.9/pyessential.egg-info → pyessential-0.2.0}/PKG-INFO +2 -1
- {pyessential-0.1.9 → pyessential-0.2.0}/README.md +1 -0
- pyessential-0.2.0/pyessential/_version.py +1 -0
- pyessential-0.2.0/pyessential/validators.py +272 -0
- {pyessential-0.1.9 → pyessential-0.2.0/pyessential.egg-info}/PKG-INFO +2 -1
- {pyessential-0.1.9 → pyessential-0.2.0}/pyessential.egg-info/SOURCES.txt +2 -0
- pyessential-0.2.0/tests/test_validators.py +231 -0
- pyessential-0.1.9/pyessential/_version.py +0 -1
- {pyessential-0.1.9 → pyessential-0.2.0}/LICENSE +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/MANIFEST.in +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/pyessential/__init__.py +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/pyessential/__main__.py +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/pyessential/cli.py +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/pyessential/decorators/__init__.py +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/pyessential/decorators/deprecated.py +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/pyessential/decorators/retry.py +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/pyessential/decorators/timer.py +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/pyessential/folders.py +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/pyessential/generators.py +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/pyessential/json_utils.py +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/pyessential/logger.py +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/pyessential/py.typed +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/pyessential.egg-info/dependency_links.txt +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/pyessential.egg-info/entry_points.txt +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/pyessential.egg-info/top_level.txt +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/pyproject.toml +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/setup.cfg +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/setup.py +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/tests/decorators/test_deprecated.py +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/tests/decorators/test_retry.py +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/tests/decorators/test_timer.py +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/tests/test_cli.py +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/tests/test_folders.py +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/tests/test_generators.py +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/tests/test_json_utils.py +0 -0
- {pyessential-0.1.9 → pyessential-0.2.0}/tests/test_logger.py +0 -0
|
@@ -6,6 +6,14 @@ and this project uses [Semantic Versioning](https://semver.org/).
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [0.2.0] - 2026-11-24
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
- `pyessential.validators`: helpers for validating common data formats with zero external
|
|
13
|
+
dependencies (`is_email`, `is_url`, `is_uuid`, `is_ip`, `is_port`, `is_hex`,
|
|
14
|
+
`is_domain`, `is_mac_address`, `is_json`, and `is_slug`).
|
|
15
|
+
- Unit tests in `tests/test_validators.py` and documentation in `docs/validators.md`.
|
|
16
|
+
|
|
9
17
|
## [0.1.9] - 2026-11-14
|
|
10
18
|
|
|
11
19
|
### Added
|
|
@@ -65,7 +73,8 @@ and this project uses [Semantic Versioning](https://semver.org/).
|
|
|
65
73
|
- Minimum supported Python version is now 3.9.
|
|
66
74
|
- Releases are now published from `v*` git tags.
|
|
67
75
|
|
|
68
|
-
[Unreleased]: https://github.com/Abhi39054/PyEssential/compare/v0.
|
|
76
|
+
[Unreleased]: https://github.com/Abhi39054/PyEssential/compare/v0.2.0...HEAD
|
|
77
|
+
[0.2.0]: https://github.com/Abhi39054/PyEssential/releases/tag/v0.2.0
|
|
69
78
|
[0.1.9]: https://github.com/Abhi39054/PyEssential/releases/tag/v0.1.9
|
|
70
79
|
[0.1.8]: https://github.com/Abhi39054/PyEssential/releases/tag/v0.1.8
|
|
71
80
|
[0.1.7]: https://github.com/Abhi39054/PyEssential/releases/tag/v0.1.7
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pyessential
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: A growing collection of Python utilities and decorators.
|
|
5
5
|
Home-page: https://github.com/Abhi39054/PyEssential
|
|
6
6
|
Author: Abhishek Kumar
|
|
@@ -221,6 +221,7 @@ Detailed guides with parameters and examples:
|
|
|
221
221
|
- [Generators](https://github.com/Abhi39054/PyEssential/blob/main/.docs/generators.md)
|
|
222
222
|
- [Logger](https://github.com/Abhi39054/PyEssential/blob/main/.docs/logger.md)
|
|
223
223
|
- [JSON utilities](https://github.com/Abhi39054/PyEssential/blob/main/.docs/json_utils.md)
|
|
224
|
+
- [Validator Utility](https://github.com/Abhi39054/PyEssential/blob/main/.docs/validators.md)
|
|
224
225
|
|
|
225
226
|
## Development
|
|
226
227
|
|
|
@@ -196,6 +196,7 @@ Detailed guides with parameters and examples:
|
|
|
196
196
|
- [Generators](https://github.com/Abhi39054/PyEssential/blob/main/.docs/generators.md)
|
|
197
197
|
- [Logger](https://github.com/Abhi39054/PyEssential/blob/main/.docs/logger.md)
|
|
198
198
|
- [JSON utilities](https://github.com/Abhi39054/PyEssential/blob/main/.docs/json_utils.md)
|
|
199
|
+
- [Validator Utility](https://github.com/Abhi39054/PyEssential/blob/main/.docs/validators.md)
|
|
199
200
|
|
|
200
201
|
## Development
|
|
201
202
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.2.0"
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Name: pyessential.validators
|
|
3
|
+
Description: A module providing utility functions for validating common data formats.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import ipaddress
|
|
7
|
+
import json
|
|
8
|
+
import re
|
|
9
|
+
import uuid
|
|
10
|
+
from typing import Any, Optional, Sequence
|
|
11
|
+
from urllib.parse import urlparse
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"is_email",
|
|
15
|
+
"is_url",
|
|
16
|
+
"is_uuid",
|
|
17
|
+
"is_ip",
|
|
18
|
+
"is_port",
|
|
19
|
+
"is_hex",
|
|
20
|
+
"is_domain",
|
|
21
|
+
"is_mac_address",
|
|
22
|
+
"is_json",
|
|
23
|
+
"is_slug",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
# ---------------------------------------------------------------- patterns
|
|
27
|
+
_EMAIL_LOCAL_RE = re.compile(r"^[A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]+\Z")
|
|
28
|
+
_DOMAIN_LABEL_RE = re.compile(r"^[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\Z")
|
|
29
|
+
_TLD_RE = re.compile(r"^(?:[A-Za-z]{2,63}|xn--[A-Za-z0-9-]{2,59})\Z")
|
|
30
|
+
_HEX_RE = re.compile(r"^[0-9A-Fa-f]+\Z")
|
|
31
|
+
_MAC_RE = re.compile(
|
|
32
|
+
r"^(?:[0-9A-Fa-f]{2}([:-])[0-9A-Fa-f]{2}(?:\1[0-9A-Fa-f]{2}){4}"
|
|
33
|
+
r"|[0-9A-Fa-f]{4}\.[0-9A-Fa-f]{4}\.[0-9A-Fa-f]{4})\Z"
|
|
34
|
+
)
|
|
35
|
+
_SLUG_RE = re.compile(r"^[a-z0-9]+(?:-[a-z0-9]+)*\Z")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
# ------------------------------------------------------------- public API
|
|
39
|
+
def is_domain(value: Any) -> bool:
|
|
40
|
+
"""Check whether ``value`` is a valid domain name (e.g. ``example.com``).
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
value (Any): The value to validate.
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
bool: True if ``value`` is a valid domain name.
|
|
47
|
+
"""
|
|
48
|
+
if not isinstance(value, str) or not value or len(value) > 253:
|
|
49
|
+
return False
|
|
50
|
+
if value.endswith("."):
|
|
51
|
+
return False
|
|
52
|
+
labels = value.split(".")
|
|
53
|
+
if len(labels) < 2:
|
|
54
|
+
return False
|
|
55
|
+
if not _TLD_RE.match(labels[-1]):
|
|
56
|
+
return False
|
|
57
|
+
return all(_DOMAIN_LABEL_RE.match(label) for label in labels)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def is_email(value: Any) -> bool:
|
|
61
|
+
"""Check whether ``value`` is a valid email address.
|
|
62
|
+
|
|
63
|
+
Performs practical syntax validation (length limits, local-part rules, and a valid
|
|
64
|
+
domain with TLD). Does not verify whether the mailbox actually exists.
|
|
65
|
+
|
|
66
|
+
Args:
|
|
67
|
+
value (Any): The value to validate.
|
|
68
|
+
|
|
69
|
+
Returns:
|
|
70
|
+
bool: True if ``value`` is a valid email address.
|
|
71
|
+
"""
|
|
72
|
+
if not isinstance(value, str) or not value or len(value) > 254:
|
|
73
|
+
return False
|
|
74
|
+
if value.count("@") != 1:
|
|
75
|
+
return False
|
|
76
|
+
local, domain = value.split("@", 1)
|
|
77
|
+
if not local or len(local) > 64:
|
|
78
|
+
return False
|
|
79
|
+
if local.startswith(".") or local.endswith(".") or ".." in local:
|
|
80
|
+
return False
|
|
81
|
+
if not _EMAIL_LOCAL_RE.match(local):
|
|
82
|
+
return False
|
|
83
|
+
return is_domain(domain)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def is_url(
|
|
87
|
+
value: Any, *, schemes: Sequence[str] = ("http", "https"), allow_localhost: bool = True
|
|
88
|
+
) -> bool:
|
|
89
|
+
"""Check whether ``value`` is a valid URL.
|
|
90
|
+
|
|
91
|
+
Args:
|
|
92
|
+
value (Any): The value to validate.
|
|
93
|
+
schemes (Sequence[str]): Allowed URL schemes. Default is ``("http", "https")``.
|
|
94
|
+
allow_localhost (bool): Whether ``localhost`` is accepted as a host.
|
|
95
|
+
Default is True.
|
|
96
|
+
|
|
97
|
+
Returns:
|
|
98
|
+
bool: True if ``value`` is a valid URL.
|
|
99
|
+
"""
|
|
100
|
+
if not isinstance(value, str) or not value:
|
|
101
|
+
return False
|
|
102
|
+
if any(ch.isspace() for ch in value):
|
|
103
|
+
return False
|
|
104
|
+
|
|
105
|
+
try:
|
|
106
|
+
parsed = urlparse(value)
|
|
107
|
+
port = parsed.port
|
|
108
|
+
except ValueError:
|
|
109
|
+
return False
|
|
110
|
+
|
|
111
|
+
if parsed.scheme.lower() not in {s.lower() for s in schemes}:
|
|
112
|
+
return False
|
|
113
|
+
if not parsed.netloc or not parsed.hostname:
|
|
114
|
+
return False
|
|
115
|
+
if port is not None and not is_port(port):
|
|
116
|
+
return False
|
|
117
|
+
|
|
118
|
+
host = parsed.hostname
|
|
119
|
+
if allow_localhost and host.lower() == "localhost":
|
|
120
|
+
return True
|
|
121
|
+
return is_domain(host) or is_ip(host)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def is_uuid(value: Any, *, version: Optional[int] = None) -> bool:
|
|
125
|
+
"""Check whether ``value`` is a valid UUID string.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
value (Any): The value to validate.
|
|
129
|
+
version (int, optional): Require a specific UUID version (1 to 5).
|
|
130
|
+
Default is None (any valid UUID).
|
|
131
|
+
|
|
132
|
+
Returns:
|
|
133
|
+
bool: True if ``value`` is a valid UUID.
|
|
134
|
+
|
|
135
|
+
Raises:
|
|
136
|
+
ValueError: If ``version`` is not None and not between 1 and 5.
|
|
137
|
+
"""
|
|
138
|
+
if version is not None and version not in (1, 2, 3, 4, 5):
|
|
139
|
+
raise ValueError("version must be between 1 and 5")
|
|
140
|
+
if not isinstance(value, str) or not value:
|
|
141
|
+
return False
|
|
142
|
+
try:
|
|
143
|
+
parsed = uuid.UUID(value)
|
|
144
|
+
except (ValueError, AttributeError):
|
|
145
|
+
return False
|
|
146
|
+
if version is not None and parsed.version != version:
|
|
147
|
+
return False
|
|
148
|
+
return True
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def is_ip(value: Any, *, version: Optional[int] = None) -> bool:
|
|
152
|
+
"""Check whether ``value`` is a valid IPv4 or IPv6 address.
|
|
153
|
+
|
|
154
|
+
Args:
|
|
155
|
+
value (Any): The value to validate.
|
|
156
|
+
version (int, optional): Restrict to ``4`` (IPv4) or ``6`` (IPv6).
|
|
157
|
+
Default is None (accept either).
|
|
158
|
+
|
|
159
|
+
Returns:
|
|
160
|
+
bool: True if ``value`` is a valid IP address.
|
|
161
|
+
|
|
162
|
+
Raises:
|
|
163
|
+
ValueError: If ``version`` is not None, 4, or 6.
|
|
164
|
+
"""
|
|
165
|
+
if version is not None and version not in (4, 6):
|
|
166
|
+
raise ValueError("version must be 4 or 6")
|
|
167
|
+
if not isinstance(value, str) or not value:
|
|
168
|
+
return False
|
|
169
|
+
try:
|
|
170
|
+
addr = ipaddress.ip_address(value)
|
|
171
|
+
except ValueError:
|
|
172
|
+
return False
|
|
173
|
+
if version is not None and addr.version != version:
|
|
174
|
+
return False
|
|
175
|
+
return True
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def is_port(value: Any, *, allow_zero: bool = False) -> bool:
|
|
179
|
+
"""Check whether ``value`` is a valid TCP/UDP port number (1-65535).
|
|
180
|
+
|
|
181
|
+
Accepts both ``int`` and numeric ``str`` values (e.g. ``8080`` or ``"8080"``).
|
|
182
|
+
Booleans are rejected.
|
|
183
|
+
|
|
184
|
+
Args:
|
|
185
|
+
value (Any): The value to validate.
|
|
186
|
+
allow_zero (bool): Also allow port ``0``. Default is False.
|
|
187
|
+
|
|
188
|
+
Returns:
|
|
189
|
+
bool: True if ``value`` is a valid port number.
|
|
190
|
+
"""
|
|
191
|
+
if isinstance(value, bool):
|
|
192
|
+
return False
|
|
193
|
+
if isinstance(value, str):
|
|
194
|
+
if not value.isdigit():
|
|
195
|
+
return False
|
|
196
|
+
port = int(value)
|
|
197
|
+
elif isinstance(value, int):
|
|
198
|
+
port = value
|
|
199
|
+
else:
|
|
200
|
+
return False
|
|
201
|
+
|
|
202
|
+
minimum = 0 if allow_zero else 1
|
|
203
|
+
return minimum <= port <= 65535
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def is_hex(value: Any, *, allow_prefix: bool = False) -> bool:
|
|
207
|
+
"""Check whether ``value`` is a valid hexadecimal string.
|
|
208
|
+
|
|
209
|
+
Args:
|
|
210
|
+
value (Any): The value to validate.
|
|
211
|
+
allow_prefix (bool): Allow a leading ``0x`` or ``0X`` prefix. Default is False.
|
|
212
|
+
|
|
213
|
+
Returns:
|
|
214
|
+
bool: True if ``value`` contains only hexadecimal characters.
|
|
215
|
+
"""
|
|
216
|
+
if not isinstance(value, str) or not value:
|
|
217
|
+
return False
|
|
218
|
+
target = value
|
|
219
|
+
if allow_prefix and target.startswith(("0x", "0X")):
|
|
220
|
+
target = target[2:]
|
|
221
|
+
if not target:
|
|
222
|
+
return False
|
|
223
|
+
return bool(_HEX_RE.match(target))
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def is_mac_address(value: Any) -> bool:
|
|
227
|
+
"""Check whether ``value`` is a valid MAC address.
|
|
228
|
+
|
|
229
|
+
Supports colon-separated (``AA:BB:CC:DD:EE:FF``), hyphen-separated
|
|
230
|
+
(``AA-BB-CC-DD-EE-FF``), and dot-separated Cisco format (``AABB.CCDD.EEFF``).
|
|
231
|
+
|
|
232
|
+
Args:
|
|
233
|
+
value (Any): The value to validate.
|
|
234
|
+
|
|
235
|
+
Returns:
|
|
236
|
+
bool: True if ``value`` is a valid MAC address.
|
|
237
|
+
"""
|
|
238
|
+
if not isinstance(value, str) or not value:
|
|
239
|
+
return False
|
|
240
|
+
return bool(_MAC_RE.match(value))
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def is_json(value: Any) -> bool:
|
|
244
|
+
"""Check whether ``value`` is a valid JSON string.
|
|
245
|
+
|
|
246
|
+
Args:
|
|
247
|
+
value (Any): The value to validate.
|
|
248
|
+
|
|
249
|
+
Returns:
|
|
250
|
+
bool: True if ``value`` can be parsed as JSON.
|
|
251
|
+
"""
|
|
252
|
+
if not isinstance(value, str) or not value:
|
|
253
|
+
return False
|
|
254
|
+
try:
|
|
255
|
+
json.loads(value)
|
|
256
|
+
except ValueError:
|
|
257
|
+
return False
|
|
258
|
+
return True
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def is_slug(value: Any) -> bool:
|
|
262
|
+
"""Check whether ``value`` is a valid URL slug (lowercase letters, digits, single hyphens).
|
|
263
|
+
|
|
264
|
+
Args:
|
|
265
|
+
value (Any): The value to validate.
|
|
266
|
+
|
|
267
|
+
Returns:
|
|
268
|
+
bool: True if ``value`` is a valid slug.
|
|
269
|
+
"""
|
|
270
|
+
if not isinstance(value, str) or not value:
|
|
271
|
+
return False
|
|
272
|
+
return bool(_SLUG_RE.match(value))
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pyessential
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: A growing collection of Python utilities and decorators.
|
|
5
5
|
Home-page: https://github.com/Abhi39054/PyEssential
|
|
6
6
|
Author: Abhishek Kumar
|
|
@@ -221,6 +221,7 @@ Detailed guides with parameters and examples:
|
|
|
221
221
|
- [Generators](https://github.com/Abhi39054/PyEssential/blob/main/.docs/generators.md)
|
|
222
222
|
- [Logger](https://github.com/Abhi39054/PyEssential/blob/main/.docs/logger.md)
|
|
223
223
|
- [JSON utilities](https://github.com/Abhi39054/PyEssential/blob/main/.docs/json_utils.md)
|
|
224
|
+
- [Validator Utility](https://github.com/Abhi39054/PyEssential/blob/main/.docs/validators.md)
|
|
224
225
|
|
|
225
226
|
## Development
|
|
226
227
|
|
|
@@ -14,6 +14,7 @@ pyessential/generators.py
|
|
|
14
14
|
pyessential/json_utils.py
|
|
15
15
|
pyessential/logger.py
|
|
16
16
|
pyessential/py.typed
|
|
17
|
+
pyessential/validators.py
|
|
17
18
|
pyessential.egg-info/PKG-INFO
|
|
18
19
|
pyessential.egg-info/SOURCES.txt
|
|
19
20
|
pyessential.egg-info/dependency_links.txt
|
|
@@ -28,6 +29,7 @@ tests/test_folders.py
|
|
|
28
29
|
tests/test_generators.py
|
|
29
30
|
tests/test_json_utils.py
|
|
30
31
|
tests/test_logger.py
|
|
32
|
+
tests/test_validators.py
|
|
31
33
|
tests/decorators/test_deprecated.py
|
|
32
34
|
tests/decorators/test_retry.py
|
|
33
35
|
tests/decorators/test_timer.py
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import uuid
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
from pyessential.validators import (
|
|
5
|
+
is_domain,
|
|
6
|
+
is_email,
|
|
7
|
+
is_hex,
|
|
8
|
+
is_ip,
|
|
9
|
+
is_json,
|
|
10
|
+
is_mac_address,
|
|
11
|
+
is_port,
|
|
12
|
+
is_slug,
|
|
13
|
+
is_url,
|
|
14
|
+
is_uuid,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# ------------------------------------------------------------------ is_domain
|
|
19
|
+
@pytest.mark.parametrize(
|
|
20
|
+
"value",
|
|
21
|
+
[
|
|
22
|
+
"example.com",
|
|
23
|
+
"sub.domain.co.in",
|
|
24
|
+
"my-site123.org",
|
|
25
|
+
"xn--bcher-kva.example",
|
|
26
|
+
],
|
|
27
|
+
)
|
|
28
|
+
def test_is_domain_valid(value):
|
|
29
|
+
assert is_domain(value) is True
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@pytest.mark.parametrize(
|
|
33
|
+
"value",
|
|
34
|
+
[
|
|
35
|
+
"",
|
|
36
|
+
None,
|
|
37
|
+
123,
|
|
38
|
+
"localhost",
|
|
39
|
+
"-bad.com",
|
|
40
|
+
"bad-.com",
|
|
41
|
+
"example.c",
|
|
42
|
+
"example..com",
|
|
43
|
+
"example.com.",
|
|
44
|
+
"example.com\n",
|
|
45
|
+
"http://example.com",
|
|
46
|
+
],
|
|
47
|
+
)
|
|
48
|
+
def test_is_domain_invalid(value):
|
|
49
|
+
assert is_domain(value) is False
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
# ------------------------------------------------------------------- is_email
|
|
53
|
+
@pytest.mark.parametrize(
|
|
54
|
+
"value",
|
|
55
|
+
[
|
|
56
|
+
"user@example.com",
|
|
57
|
+
"first.last+tag@sub.domain.org",
|
|
58
|
+
"user_123@company.co.in",
|
|
59
|
+
],
|
|
60
|
+
)
|
|
61
|
+
def test_is_email_valid(value):
|
|
62
|
+
assert is_email(value) is True
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@pytest.mark.parametrize(
|
|
66
|
+
"value",
|
|
67
|
+
[
|
|
68
|
+
"",
|
|
69
|
+
None,
|
|
70
|
+
42,
|
|
71
|
+
"plainaddress",
|
|
72
|
+
"@example.com",
|
|
73
|
+
"user@",
|
|
74
|
+
"user@localhost",
|
|
75
|
+
".user@example.com",
|
|
76
|
+
"user.@example.com",
|
|
77
|
+
"user..name@example.com",
|
|
78
|
+
"user name@example.com",
|
|
79
|
+
"user@example.com\n",
|
|
80
|
+
"a" * 65 + "@example.com",
|
|
81
|
+
],
|
|
82
|
+
)
|
|
83
|
+
def test_is_email_invalid(value):
|
|
84
|
+
assert is_email(value) is False
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
# --------------------------------------------------------------------- is_url
|
|
88
|
+
@pytest.mark.parametrize(
|
|
89
|
+
"value",
|
|
90
|
+
[
|
|
91
|
+
"https://example.com",
|
|
92
|
+
"http://sub.example.com:8080/path?a=1&b=2#top",
|
|
93
|
+
"http://localhost:8000/docs",
|
|
94
|
+
"http://127.0.0.1:5000/api",
|
|
95
|
+
"https://[2001:db8::1]/resource",
|
|
96
|
+
],
|
|
97
|
+
)
|
|
98
|
+
def test_is_url_valid(value):
|
|
99
|
+
assert is_url(value) is True
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@pytest.mark.parametrize(
|
|
103
|
+
"value",
|
|
104
|
+
[
|
|
105
|
+
"",
|
|
106
|
+
None,
|
|
107
|
+
123,
|
|
108
|
+
"example.com",
|
|
109
|
+
"ftp://example.com",
|
|
110
|
+
"http://",
|
|
111
|
+
"https://bad host.com",
|
|
112
|
+
"https://example.com:99999",
|
|
113
|
+
"https://example.com\n",
|
|
114
|
+
],
|
|
115
|
+
)
|
|
116
|
+
def test_is_url_invalid(value):
|
|
117
|
+
assert is_url(value) is False
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def test_is_url_custom_schemes_and_localhost():
|
|
121
|
+
assert is_url("ftp://files.example.com/a.zip", schemes=("ftp",)) is True
|
|
122
|
+
assert is_url("http://localhost:8000", allow_localhost=False) is False
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
# -------------------------------------------------------------------- is_uuid
|
|
126
|
+
def test_is_uuid_valid_and_versions():
|
|
127
|
+
v1 = str(uuid.uuid1())
|
|
128
|
+
v4 = str(uuid.uuid4())
|
|
129
|
+
v5 = str(uuid.uuid5(uuid.NAMESPACE_DNS, "example.com"))
|
|
130
|
+
|
|
131
|
+
assert is_uuid(v1) is True
|
|
132
|
+
assert is_uuid(v4, version=4) is True
|
|
133
|
+
assert is_uuid(v4, version=1) is False
|
|
134
|
+
assert is_uuid(v5, version=5) is True
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
@pytest.mark.parametrize("value", ["", None, 123, "not-a-uuid", "12345678-1234-1234-1234-1234567890abZ"])
|
|
138
|
+
def test_is_uuid_invalid(value):
|
|
139
|
+
assert is_uuid(value) is False
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def test_is_uuid_bad_version_raises():
|
|
143
|
+
with pytest.raises(ValueError):
|
|
144
|
+
is_uuid(str(uuid.uuid4()), version=9)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
# ---------------------------------------------------------------------- is_ip
|
|
148
|
+
@pytest.mark.parametrize("value", ["127.0.0.1", "192.168.1.1", "0.0.0.0", "::1", "2001:db8::1"])
|
|
149
|
+
def test_is_ip_valid(value):
|
|
150
|
+
assert is_ip(value) is True
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def test_is_ip_version_filtering():
|
|
154
|
+
assert is_ip("192.168.0.1", version=4) is True
|
|
155
|
+
assert is_ip("192.168.0.1", version=6) is False
|
|
156
|
+
assert is_ip("2001:db8::1", version=6) is True
|
|
157
|
+
assert is_ip("2001:db8::1", version=4) is False
|
|
158
|
+
|
|
159
|
+
with pytest.raises(ValueError):
|
|
160
|
+
is_ip("127.0.0.1", version=5)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
@pytest.mark.parametrize("value", ["", None, 123, "256.0.0.1", "1.2.3", "example.com", "127.0.0.1\n"])
|
|
164
|
+
def test_is_ip_invalid(value):
|
|
165
|
+
assert is_ip(value) is False
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
# -------------------------------------------------------------------- is_port
|
|
169
|
+
@pytest.mark.parametrize("value", [1, 80, 443, 8080, 65535, "80", "65535"])
|
|
170
|
+
def test_is_port_valid(value):
|
|
171
|
+
assert is_port(value) is True
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
@pytest.mark.parametrize("value", [0, -1, 65536, "0", "-80", "8080a", "", None, True, False, 80.5])
|
|
175
|
+
def test_is_port_invalid(value):
|
|
176
|
+
assert is_port(value) is False
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def test_is_port_allow_zero():
|
|
180
|
+
assert is_port(0, allow_zero=True) is True
|
|
181
|
+
assert is_port("0", allow_zero=True) is True
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
# --------------------------------------------------------------------- is_hex
|
|
185
|
+
@pytest.mark.parametrize("value", ["0", "deadbeef", "DEADBEEF", "1a2B3c"])
|
|
186
|
+
def test_is_hex_valid(value):
|
|
187
|
+
assert is_hex(value) is True
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
@pytest.mark.parametrize("value", ["", None, 0xFF, "0x1A", "xyz", "12 34", "deadbeef\n"])
|
|
191
|
+
def test_is_hex_invalid(value):
|
|
192
|
+
assert is_hex(value) is False
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def test_is_hex_allow_prefix():
|
|
196
|
+
assert is_hex("0xdeadbeef", allow_prefix=True) is True
|
|
197
|
+
assert is_hex("0X1A2B", allow_prefix=True) is True
|
|
198
|
+
assert is_hex("0x", allow_prefix=True) is False
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
# ------------------------------------------------------------- is_mac_address
|
|
202
|
+
@pytest.mark.parametrize("value", ["AA:BB:CC:DD:EE:FF", "aa-bb-cc-dd-ee-ff", "aabb.ccdd.eeff"])
|
|
203
|
+
def test_is_mac_address_valid(value):
|
|
204
|
+
assert is_mac_address(value) is True
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
@pytest.mark.parametrize("value", ["", None, 123, "AA:BB:CC:DD:EE", "AA:BB-CC:DD:EE:FF", "GG:BB:CC:DD:EE:FF"])
|
|
208
|
+
def test_is_mac_address_invalid(value):
|
|
209
|
+
assert is_mac_address(value) is False
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
# -------------------------------------------------------------------- is_json
|
|
213
|
+
@pytest.mark.parametrize("value", ['{"a": 1}', "[1, 2, 3]", '"hello"', "true", "null", "42"])
|
|
214
|
+
def test_is_json_valid(value):
|
|
215
|
+
assert is_json(value) is True
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
@pytest.mark.parametrize("value", ["", None, {"a": 1}, "{bad}", "{'a': 1}"])
|
|
219
|
+
def test_is_json_invalid(value):
|
|
220
|
+
assert is_json(value) is False
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
# -------------------------------------------------------------------- is_slug
|
|
224
|
+
@pytest.mark.parametrize("value", ["hello", "hello-world", "pyessential-v2-release"])
|
|
225
|
+
def test_is_slug_valid(value):
|
|
226
|
+
assert is_slug(value) is True
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
@pytest.mark.parametrize("value", ["", None, 123, "Hello-World", "-leading", "trailing-", "double--hyphen", "has space"])
|
|
230
|
+
def test_is_slug_invalid(value):
|
|
231
|
+
assert is_slug(value) is False
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.1.9"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|