Skip to content

country

validators.country.calling_code(value)

Validates given calling code.

This performs country's calling code validation.

Examples:

>>> calling_code('+91')
# Output: True
>>> calling_code('-31')
# Output: ValidationError(func=calling_code, args={'value': '-31'})

Parameters:

Name Type Description Default
value str

Country's calling code string to validate.

required

Returns:

Type Description
Literal[True]

If value is a valid calling code.

ValidationError

If value is an invalid calling code.

Source code in src/validators/country.py
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
@validator
def calling_code(value: str, /):
    """Validates given calling code.

    This performs country's calling code validation.

    Examples:
        >>> calling_code('+91')
        # Output: True
        >>> calling_code('-31')
        # Output: ValidationError(func=calling_code, args={'value': '-31'})

    Args:
        value:
            Country's calling code string to validate.

    Returns:
        (Literal[True]): If `value` is a valid calling code.
        (ValidationError): If `value` is an invalid calling code.
    """
    if not value:
        return False

    return value in set(_calling_codes.values())

validators.country.country_code(value, /, *, iso_format='auto', ignore_case=False)

Validates given country code.

This performs a case-sensitive ISO 3166 country code validation.

Examples:

>>> country_code('GB', iso_format='alpha3')
# Output: False
>>> country_code('USA')
# Output: True
>>> country_code('840', iso_format='numeric')
# Output: True
>>> country_code('iN', iso_format='alpha2')
# Output: False
>>> country_code('ZWE', iso_format='alpha3')
# Output: True

Parameters:

Name Type Description Default
value str

Country code string to validate.

required
iso_format str

ISO format to be used. Available options are: auto, alpha2, alpha3 and numeric.

'auto'
ignore_case bool

Enable/Disable case-sensitive matching.

False

Returns:

Type Description
Literal[True]

If value is a valid country code.

ValidationError

If value is an invalid country code.

Source code in src/validators/country.py
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
@validator
def country_code(value: str, /, *, iso_format: str = "auto", ignore_case: bool = False):
    """Validates given country code.

    This performs a case-sensitive [ISO 3166][1] country code validation.

    [1]: https://www.iso.org/iso-3166-country-codes.html

    Examples:
        >>> country_code('GB', iso_format='alpha3')
        # Output: False
        >>> country_code('USA')
        # Output: True
        >>> country_code('840', iso_format='numeric')
        # Output: True
        >>> country_code('iN', iso_format='alpha2')
        # Output: False
        >>> country_code('ZWE', iso_format='alpha3')
        # Output: True

    Args:
        value:
            Country code string to validate.
        iso_format:
            ISO format to be used. Available options are:
            `auto`, `alpha2`, `alpha3` and `numeric`.
        ignore_case:
            Enable/Disable case-sensitive matching.

    Returns:
        (Literal[True]): If `value` is a valid country code.
        (ValidationError): If `value` is an invalid country code.
    """
    if not value:
        return False

    if not (1 < len(value) < 4):
        return False

    if iso_format == "auto" and (iso_format := _get_code_type(value)) == "invalid":
        return False

    if iso_format == "alpha2":
        return (
            value.upper() in set(_alpha3_to_alpha2.values())
            if ignore_case
            else value in set(_alpha3_to_alpha2.values())
        )
    if iso_format == "alpha3":
        return value.upper() in _alpha3_to_alpha2 if ignore_case else value in _alpha3_to_alpha2

    return value in _numeric if iso_format == "numeric" else False

validators.country.currency(value, /, *, skip_symbols=True, ignore_case=False)

Validates given currency code.

This performs ISO 4217 currency code/symbol validation.

Examples:

>>> currency('USD')
# Output: True
>>> currency('ZWX')
# Output: ValidationError(func=currency, args={'value': 'ZWX'})

Parameters:

Name Type Description Default
value str

Currency code/symbol string to validate.

required
skip_symbols bool

Skip currency symbol validation.

True
ignore_case bool

Enable/Disable case-sensitive matching.

False

Returns:

Type Description
Literal[True]

If value is a valid currency code.

ValidationError

If value is an invalid currency code.

Source code in src/validators/country.py
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
@validator
def currency(value: str, /, *, skip_symbols: bool = True, ignore_case: bool = False):
    """Validates given currency code.

    This performs [ISO 4217][1] currency code/symbol validation.

    [1]: https://www.iso.org/iso-4217-currency-codes.html

    Examples:
        >>> currency('USD')
        # Output: True
        >>> currency('ZWX')
        # Output: ValidationError(func=currency, args={'value': 'ZWX'})

    Args:
        value:
            Currency code/symbol string to validate.
        skip_symbols:
            Skip currency symbol validation.
        ignore_case:
            Enable/Disable case-sensitive matching.

    Returns:
        (Literal[True]): If `value` is a valid currency code.
        (ValidationError): If `value` is an invalid currency code.
    """
    if not value:
        return False

    if not skip_symbols and value in _currency_symbols:
        return True

    if len(value) != 3:
        return False

    return value.upper() in _currency_iso4217 if ignore_case else value in _currency_iso4217