human-input-validator 0.1.2__tar.gz → 0.1.3__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.
- {human_input_validator-0.1.2 → human_input_validator-0.1.3}/PKG-INFO +8 -4
- {human_input_validator-0.1.2 → human_input_validator-0.1.3}/README.md +7 -3
- {human_input_validator-0.1.2 → human_input_validator-0.1.3}/pyproject.toml +1 -1
- {human_input_validator-0.1.2 → human_input_validator-0.1.3}/src/human_input_validator/validators.py +14 -6
- {human_input_validator-0.1.2 → human_input_validator-0.1.3}/src/human_input_validator.egg-info/PKG-INFO +8 -4
- {human_input_validator-0.1.2 → human_input_validator-0.1.3}/tests/test_validators.py +14 -0
- {human_input_validator-0.1.2 → human_input_validator-0.1.3}/setup.cfg +0 -0
- {human_input_validator-0.1.2 → human_input_validator-0.1.3}/src/human_input_validator/__init__.py +0 -0
- {human_input_validator-0.1.2 → human_input_validator-0.1.3}/src/human_input_validator/py.typed +0 -0
- {human_input_validator-0.1.2 → human_input_validator-0.1.3}/src/human_input_validator.egg-info/SOURCES.txt +0 -0
- {human_input_validator-0.1.2 → human_input_validator-0.1.3}/src/human_input_validator.egg-info/dependency_links.txt +0 -0
- {human_input_validator-0.1.2 → human_input_validator-0.1.3}/src/human_input_validator.egg-info/requires.txt +0 -0
- {human_input_validator-0.1.2 → human_input_validator-0.1.3}/src/human_input_validator.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: human-input-validator
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.3
|
|
4
4
|
Summary: Small validators for common human-entered data.
|
|
5
5
|
Author: Snorlena
|
|
6
6
|
License-Expression: MIT
|
|
@@ -40,9 +40,13 @@ Each function returns a normalized value or raises `ValidationError` for
|
|
|
40
40
|
invalid input.
|
|
41
41
|
|
|
42
42
|
- `email(value)` — returns a trimmed, lower-cased email address.
|
|
43
|
-
- `country(value)` — returns the
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
- `country(value, short=False)` — returns the common ISO 3166-1 country name
|
|
44
|
+
(e.g. "Tanzania" instead of "Tanzania, United Republic of"), given a name
|
|
45
|
+
or code. Also accepts localized country names (e.g. "Deutschland",
|
|
46
|
+
"España") via pycountry's bundled translations. Some countries have no
|
|
47
|
+
common name in pycountry's data (e.g. "Congo, The Democratic Republic of
|
|
48
|
+
the"); pass `short=True` to trim those at the first comma instead of
|
|
49
|
+
returning the full formal name.
|
|
46
50
|
- `username(value, max_length=12)` — returns a lower-cased username, checking
|
|
47
51
|
length and allowed characters.
|
|
48
52
|
- `phonenumber(value, pattern)` — checks a phone number against one or more
|
|
@@ -30,9 +30,13 @@ Each function returns a normalized value or raises `ValidationError` for
|
|
|
30
30
|
invalid input.
|
|
31
31
|
|
|
32
32
|
- `email(value)` — returns a trimmed, lower-cased email address.
|
|
33
|
-
- `country(value)` — returns the
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
- `country(value, short=False)` — returns the common ISO 3166-1 country name
|
|
34
|
+
(e.g. "Tanzania" instead of "Tanzania, United Republic of"), given a name
|
|
35
|
+
or code. Also accepts localized country names (e.g. "Deutschland",
|
|
36
|
+
"España") via pycountry's bundled translations. Some countries have no
|
|
37
|
+
common name in pycountry's data (e.g. "Congo, The Democratic Republic of
|
|
38
|
+
the"); pass `short=True` to trim those at the first comma instead of
|
|
39
|
+
returning the full formal name.
|
|
36
40
|
- `username(value, max_length=12)` — returns a lower-cased username, checking
|
|
37
41
|
length and allowed characters.
|
|
38
42
|
- `phonenumber(value, pattern)` — checks a phone number against one or more
|
{human_input_validator-0.1.2 → human_input_validator-0.1.3}/src/human_input_validator/validators.py
RENAMED
|
@@ -54,19 +54,27 @@ def email(value: str) -> str:
|
|
|
54
54
|
_FUZZY_MATCH_CUTOFF = 0.8
|
|
55
55
|
|
|
56
56
|
|
|
57
|
-
def _display_name(entry: object) -> str:
|
|
57
|
+
def _display_name(entry: object, short: bool = False) -> str:
|
|
58
58
|
"""Prefer the short common name (e.g. 'Tanzania') over the formal ISO name."""
|
|
59
59
|
common_name: str | None = getattr(entry, "common_name", None)
|
|
60
60
|
name: str = getattr(entry, "name")
|
|
61
|
-
|
|
61
|
+
if common_name:
|
|
62
|
+
return common_name
|
|
63
|
+
# No common_name available (e.g. 'Congo, The Democratic Republic of the');
|
|
64
|
+
# trim at the comma as a best-effort shortening.
|
|
65
|
+
return name.split(",", 1)[0] if short else name
|
|
62
66
|
|
|
63
67
|
|
|
64
|
-
def country(value: str) -> str:
|
|
65
|
-
"""Return the common name of an ISO 3166-1 country, given its name or code in any supported language.
|
|
68
|
+
def country(value: str, short: bool = False) -> str:
|
|
69
|
+
"""Return the common name of an ISO 3166-1 country, given its name or code in any supported language.
|
|
70
|
+
|
|
71
|
+
If ``short`` is True, names without a common_name (e.g. 'Congo, The Democratic
|
|
72
|
+
Republic of the') are trimmed at the first comma instead of returned in full.
|
|
73
|
+
"""
|
|
66
74
|
normalized = value.strip()
|
|
67
75
|
try:
|
|
68
76
|
result = pycountry.countries.lookup(normalized)
|
|
69
|
-
return _display_name(result)
|
|
77
|
+
return _display_name(result, short)
|
|
70
78
|
except LookupError:
|
|
71
79
|
pass
|
|
72
80
|
|
|
@@ -84,7 +92,7 @@ def country(value: str) -> str:
|
|
|
84
92
|
result = pycountry.countries.get(alpha_2=alpha_2)
|
|
85
93
|
if result is None:
|
|
86
94
|
raise ValidationError("Please enter a valid country")
|
|
87
|
-
return _display_name(result)
|
|
95
|
+
return _display_name(result, short)
|
|
88
96
|
|
|
89
97
|
|
|
90
98
|
def username(value: str, max_length: int = 12) -> str:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: human-input-validator
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.3
|
|
4
4
|
Summary: Small validators for common human-entered data.
|
|
5
5
|
Author: Snorlena
|
|
6
6
|
License-Expression: MIT
|
|
@@ -40,9 +40,13 @@ Each function returns a normalized value or raises `ValidationError` for
|
|
|
40
40
|
invalid input.
|
|
41
41
|
|
|
42
42
|
- `email(value)` — returns a trimmed, lower-cased email address.
|
|
43
|
-
- `country(value)` — returns the
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
- `country(value, short=False)` — returns the common ISO 3166-1 country name
|
|
44
|
+
(e.g. "Tanzania" instead of "Tanzania, United Republic of"), given a name
|
|
45
|
+
or code. Also accepts localized country names (e.g. "Deutschland",
|
|
46
|
+
"España") via pycountry's bundled translations. Some countries have no
|
|
47
|
+
common name in pycountry's data (e.g. "Congo, The Democratic Republic of
|
|
48
|
+
the"); pass `short=True` to trim those at the first comma instead of
|
|
49
|
+
returning the full formal name.
|
|
46
50
|
- `username(value, max_length=12)` — returns a lower-cased username, checking
|
|
47
51
|
length and allowed characters.
|
|
48
52
|
- `phonenumber(value, pattern)` — checks a phone number against one or more
|
|
@@ -35,6 +35,20 @@ class CountryTests(unittest.TestCase):
|
|
|
35
35
|
with self.assertRaises(ValidationError):
|
|
36
36
|
country("Kolbäck")
|
|
37
37
|
|
|
38
|
+
def test_full_name_without_common_name_is_unchanged(self) -> None:
|
|
39
|
+
self.assertEqual(
|
|
40
|
+
country("Congo, The Democratic Republic of the"),
|
|
41
|
+
"Congo, The Democratic Republic of the",
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
def test_short_trims_name_without_common_name(self) -> None:
|
|
45
|
+
self.assertEqual(
|
|
46
|
+
country("Congo, The Democratic Republic of the", short=True), "Congo"
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
def test_short_does_not_affect_common_name(self) -> None:
|
|
50
|
+
self.assertEqual(country("Tanzania", short=True), "Tanzania")
|
|
51
|
+
|
|
38
52
|
|
|
39
53
|
class UsernameTests(unittest.TestCase):
|
|
40
54
|
def test_normalizes_username(self) -> None:
|
|
File without changes
|
{human_input_validator-0.1.2 → human_input_validator-0.1.3}/src/human_input_validator/__init__.py
RENAMED
|
File without changes
|
{human_input_validator-0.1.2 → human_input_validator-0.1.3}/src/human_input_validator/py.typed
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|