Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions tests/test_currency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
import pytest

from validators import iso_4217, ValidationFailure


@pytest.mark.parametrize('value, case_sensitive', [
('USD', True),
('usd', False),
])
def test_returns_true_on_valid_iso_4217(value, case_sensitive):
assert iso_4217(value, case_sensitive)


@pytest.mark.parametrize('value, case_sensitive', [
('XTB', True),
('usd', True),
])
def test_returns_failed_on_valid_iso_4217(value, case_sensitive):
assert isinstance(iso_4217(value, case_sensitive), ValidationFailure)
3 changes: 2 additions & 1 deletion validators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
unionpay,
visa
)
from .currency import iso_4217
from .domain import domain
from .email import email
from .extremes import Max, Min
Expand All @@ -29,6 +30,6 @@
'ipv4_cidr', 'ipv6', 'ipv6_cidr', 'length', 'mac_address', 'slug',
'truthy', 'url', 'ValidationFailure', 'validator', 'uuid',
'card_number', 'visa', 'mastercard', 'amex', 'unionpay', 'diners',
'jcb', 'discover')
'jcb', 'discover', 'iso_4217')

__version__ = '0.15.0'
216 changes: 216 additions & 0 deletions validators/currency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
from .utils import validator


@validator
def iso_4217(value, case_sensitive=True):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we'd just call this validator currency_code instead of iso_4217. We could keep the comment block as it is describing that the currency code validator is based on ISO 4217

"""
Return whether or not given value is a valid ISO 4217 currency code.

This validator is based on ISO 4217.

Examples::

>>> iso_4217('USD')
True

>>> iso_4217('usd')
ValidationFailure(func=iso_4217, ...)

>>> iso_4217('usd', case_sensitive=False)
True

>>> iso_4217('XBT')
ValidationFailure(func=iso_4217, ...)

.. versionadded:: 0.16.0

:param value: ISO 4217 currency code string to validate
"""

if not case_sensitive:
value = value.upper()

return value in [
'AED',
'AFN',
'ALL',
'AMD',
'ANG',
'AOA',
'ARS',
'AUD',
'AWG',
'AZN',
'BAM',
'BBD',
'BDT',
'BGN',
'BHD',
'BIF',
'BMD',
'BND',
'BOB',
'BOV',
'BRL',
'BSD',
'BTN',
'BWP',
'BYR',
'BZD',
'CAD',
'CDF',
'CHE',
'CHF',
'CHW',
'CLF',
'CLP',
'CNY',
'COP',
'COU',
'CRC',
'CUC',
'CUP',
'CVE',
'CZK',
'DJF',
'DKK',
'DOP',
'DZD',
'EGP',
'ERN',
'ETB',
'EUR',
'FJD',
'FKP',
'GBP',
'GEL',
'GHS',
'GIP',
'GMD',
'GNF',
'GTQ',
'GYD',
'HKD',
'HNL',
'HRK',
'HTG',
'HUF',
'IDR',
'ILS',
'INR',
'IQD',
'IRR',
'ISK',
'JMD',
'JOD',
'JPY',
'KES',
'KGS',
'KHR',
'KMF',
'KPW',
'KRW',
'KWD',
'KYD',
'KZT',
'LAK',
'LBP',
'LKR',
'LRD',
'LSL',
'LTL',
'LVL',
'LYD',
'MAD',
'MDL',
'MGA',
'MKD',
'MMK',
'MNT',
'MOP',
'MRO',
'MUR',
'MVR',
'MWK',
'MXN',
'MXV',
'MYR',
'MZN',
'NAD',
'NGN',
'NIO',
'NOK',
'NPR',
'NZD',
'OMR',
'PAB',
'PEN',
'PGK',
'PHP',
'PKR',
'PLN',
'PYG',
'QAR',
'RON',
'RSD',
'RUB',
'RWF',
'SAR',
'SBD',
'SCR',
'SDG',
'SEK',
'SGD',
'SHP',
'SLL',
'SOS',
'SRD',
'SSP',
'STD',
'SVC',
'SYP',
'SZL',
'THB',
'TJS',
'TMT',
'TND',
'TOP',
'TRY',
'TTD',
'TWD',
'TZS',
'UAH',
'UGX',
'USD',
'USN',
'USS',
'UYI',
'UYU',
'UZS',
'VEF',
'VND',
'VUV',
'WST',
'XAF',
'XAG',
'XAU',
'XBA',
'XBB',
'XBC',
'XBD',
'XCD',
'XDR',
'XFU',
'XOF',
'XPD',
'XPF',
'XPT',
'XSU',
'XTS',
'XUA',
'XXX',
'YER',
'ZAR',
'ZMW',
'ZWL',
]