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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: human-input-validator
3
- Version: 0.1.2
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 official ISO 3166-1 country name, given a
44
- name or code. Also accepts localized country names (e.g. "Deutschland",
45
- "España") via pycountry's bundled translations.
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 official ISO 3166-1 country name, given a
34
- name or code. Also accepts localized country names (e.g. "Deutschland",
35
- "España") via pycountry's bundled translations.
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
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "human-input-validator"
7
- version = "0.1.2"
7
+ version = "0.1.3"
8
8
  description = "Small validators for common human-entered data."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -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
- return common_name or name
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.2
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 official ISO 3166-1 country name, given a
44
- name or code. Also accepts localized country names (e.g. "Deutschland",
45
- "España") via pycountry's bundled translations.
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: