Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/validators/card.py: 100%
41 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:08 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:08 +0000
1"""Card."""
2# -*- coding: utf-8 -*-
4# standard
5import re
7# local
8from .utils import validator
11@validator
12def card_number(value: str, /):
13 """Return whether or not given value is a valid generic card number.
15 This validator is based on [Luhn's algorithm][1].
17 [1]: https://github.com/mmcloughlin/luhn
19 Examples:
20 >>> card_number('4242424242424242')
21 # Output: True
22 >>> card_number('4242424242424241')
23 # Output: ValidationFailure(func=card_number, args={'value': '4242424242424241'})
25 Args:
26 value:
27 Generic card number string to validate
29 Returns:
30 (Literal[True]):
31 If `value` is a valid generic card number.
32 (ValidationFailure):
33 If `value` is an invalid generic card number.
35 > *New in version 0.15.0*.
36 """
37 if not value:
38 return False
39 try:
40 digits = list(map(int, value))
41 odd_sum = sum(digits[-1::-2])
42 even_sum = sum(sum(divmod(2 * d, 10)) for d in digits[-2::-2])
43 return (odd_sum + even_sum) % 10 == 0
44 except ValueError:
45 return False
48@validator
49def visa(value: str, /):
50 """Return whether or not given value is a valid Visa card number.
52 Examples:
53 >>> visa('4242424242424242')
54 # Output: True
55 >>> visa('2223003122003222')
56 # Output: ValidationFailure(func=visa, args={'value': '2223003122003222'})
58 Args:
59 value:
60 Visa card number string to validate
62 Returns:
63 (Literal[True]):
64 If `value` is a valid Visa card number.
65 (ValidationFailure):
66 If `value` is an invalid Visa card number.
68 > *New in version 0.15.0*.
69 """
70 pattern = re.compile(r"^4")
71 return card_number(value) and len(value) == 16 and pattern.match(value)
74@validator
75def mastercard(value: str, /):
76 """Return whether or not given value is a valid Mastercard card number.
78 Examples:
79 >>> mastercard('5555555555554444')
80 # Output: True
81 >>> mastercard('4242424242424242')
82 # Output: ValidationFailure(func=mastercard, args={'value': '4242424242424242'})
84 Args:
85 value:
86 Mastercard card number string to validate
88 Returns:
89 (Literal[True]):
90 If `value` is a valid Mastercard card number.
91 (ValidationFailure):
92 If `value` is an invalid Mastercard card number.
94 > *New in version 0.15.0*.
95 """
96 pattern = re.compile(r"^(51|52|53|54|55|22|23|24|25|26|27)")
97 return card_number(value) and len(value) == 16 and pattern.match(value)
100@validator
101def amex(value: str, /):
102 """Return whether or not given value is a valid American Express card number.
104 Examples:
105 >>> amex('378282246310005')
106 # Output: True
107 >>> amex('4242424242424242')
108 # Output: ValidationFailure(func=amex, args={'value': '4242424242424242'})
110 Args:
111 value:
112 American Express card number string to validate
114 Returns:
115 (Literal[True]):
116 If `value` is a valid American Express card number.
117 (ValidationFailure):
118 If `value` is an invalid American Express card number.
120 > *New in version 0.15.0*.
121 """
122 pattern = re.compile(r"^(34|37)")
123 return card_number(value) and len(value) == 15 and pattern.match(value)
126@validator
127def unionpay(value: str, /):
128 """Return whether or not given value is a valid UnionPay card number.
130 Examples:
131 >>> unionpay('6200000000000005')
132 # Output: True
133 >>> unionpay('4242424242424242')
134 # Output: ValidationFailure(func=unionpay, args={'value': '4242424242424242'})
136 Args:
137 value:
138 UnionPay card number string to validate
140 Returns:
141 (Literal[True]):
142 If `value` is a valid UnionPay card number.
143 (ValidationFailure):
144 If `value` is an invalid UnionPay card number.
146 > *New in version 0.15.0*.
147 """
148 pattern = re.compile(r"^62")
149 return card_number(value) and len(value) == 16 and pattern.match(value)
152@validator
153def diners(value: str, /):
154 """Return whether or not given value is a valid Diners Club card number.
156 Examples:
157 >>> diners('3056930009020004')
158 # Output: True
159 >>> diners('4242424242424242')
160 # Output: ValidationFailure(func=diners, args={'value': '4242424242424242'})
162 Args:
163 value:
164 Diners Club card number string to validate
166 Returns:
167 (Literal[True]):
168 If `value` is a valid Diners Club card number.
169 (ValidationFailure):
170 If `value` is an invalid Diners Club card number.
172 > *New in version 0.15.0*.
173 """
174 pattern = re.compile(r"^(30|36|38|39)")
175 return card_number(value) and len(value) in {14, 16} and pattern.match(value)
178@validator
179def jcb(value: str, /):
180 """Return whether or not given value is a valid JCB card number.
182 Examples:
183 >>> jcb('3566002020360505')
184 # Output: True
185 >>> jcb('4242424242424242')
186 # Output: ValidationFailure(func=jcb, args={'value': '4242424242424242'})
188 Args:
189 value:
190 JCB card number string to validate
192 Returns:
193 (Literal[True]):
194 If `value` is a valid JCB card number.
195 (ValidationFailure):
196 If `value` is an invalid JCB card number.
198 > *New in version 0.15.0*.
199 """
200 pattern = re.compile(r"^35")
201 return card_number(value) and len(value) == 16 and pattern.match(value)
204@validator
205def discover(value: str, /):
206 """Return whether or not given value is a valid Discover card number.
208 Examples:
209 >>> discover('6011111111111117')
210 # Output: True
211 >>> discover('4242424242424242')
212 # Output: ValidationFailure(func=discover, args={'value': '4242424242424242'})
214 Args:
215 value:
216 Discover card number string to validate
218 Returns:
219 (Literal[True]):
220 If `value` is a valid Discover card number.
221 (ValidationFailure):
222 If `value` is an invalid Discover card number.
224 > *New in version 0.15.0*.
225 """
226 pattern = re.compile(r"^(60|64|65)")
227 return card_number(value) and len(value) == 16 and pattern.match(value)