Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/validators/card.py: 96%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

46 statements  

1"""Card.""" 

2 

3# standard 

4import re 

5 

6# local 

7from .utils import validator 

8 

9 

10@validator 

11def card_number(value: str, /): 

12 """Return whether or not given value is a valid generic card number. 

13 

14 This validator is based on [Luhn's algorithm][1]. 

15 

16 [1]: https://github.com/mmcloughlin/luhn 

17 

18 Examples: 

19 >>> card_number('4242424242424242') 

20 True 

21 >>> card_number('4242424242424241') 

22 ValidationError(func=card_number, args={'value': '4242424242424241'}) 

23 

24 Args: 

25 value: 

26 Generic card number string to validate 

27 

28 Returns: 

29 (Literal[True]): If `value` is a valid generic card number. 

30 (ValidationError): If `value` is an invalid generic card number. 

31 """ 

32 if not value: 

33 return False 

34 try: 

35 digits = list(map(int, value)) 

36 odd_sum = sum(digits[-1::-2]) 

37 even_sum = sum(sum(divmod(2 * d, 10)) for d in digits[-2::-2]) 

38 return (odd_sum + even_sum) % 10 == 0 

39 except ValueError: 

40 return False 

41 

42 

43@validator 

44def visa(value: str, /): 

45 """Return whether or not given value is a valid Visa card number. 

46 

47 Examples: 

48 >>> visa('4242424242424242') 

49 True 

50 >>> visa('2223003122003222') 

51 ValidationError(func=visa, args={'value': '2223003122003222'}) 

52 

53 Args: 

54 value: 

55 Visa card number string to validate 

56 

57 Returns: 

58 (Literal[True]): If `value` is a valid Visa card number. 

59 (ValidationError): If `value` is an invalid Visa card number. 

60 """ 

61 pattern = re.compile(r"^4") 

62 return card_number(value) and len(value) == 16 and pattern.match(value) 

63 

64 

65@validator 

66def mastercard(value: str, /): 

67 """Return whether or not given value is a valid Mastercard card number. 

68 

69 Examples: 

70 >>> mastercard('5555555555554444') 

71 True 

72 >>> mastercard('4242424242424242') 

73 ValidationError(func=mastercard, args={'value': '4242424242424242'}) 

74 

75 Args: 

76 value: 

77 Mastercard card number string to validate 

78 

79 Returns: 

80 (Literal[True]): If `value` is a valid Mastercard card number. 

81 (ValidationError): If `value` is an invalid Mastercard card number. 

82 """ 

83 pattern = re.compile(r"^(51|52|53|54|55|22|23|24|25|26|27)") 

84 return card_number(value) and len(value) == 16 and pattern.match(value) 

85 

86 

87@validator 

88def amex(value: str, /): 

89 """Return whether or not given value is a valid American Express card number. 

90 

91 Examples: 

92 >>> amex('378282246310005') 

93 True 

94 >>> amex('4242424242424242') 

95 ValidationError(func=amex, args={'value': '4242424242424242'}) 

96 

97 Args: 

98 value: 

99 American Express card number string to validate 

100 

101 Returns: 

102 (Literal[True]): If `value` is a valid American Express card number. 

103 (ValidationError): If `value` is an invalid American Express card number. 

104 """ 

105 pattern = re.compile(r"^(34|37)") 

106 return card_number(value) and len(value) == 15 and pattern.match(value) 

107 

108 

109@validator 

110def unionpay(value: str, /): 

111 """Return whether or not given value is a valid UnionPay card number. 

112 

113 Examples: 

114 >>> unionpay('6200000000000005') 

115 True 

116 >>> unionpay('4242424242424242') 

117 ValidationError(func=unionpay, args={'value': '4242424242424242'}) 

118 

119 Args: 

120 value: 

121 UnionPay card number string to validate 

122 

123 Returns: 

124 (Literal[True]): If `value` is a valid UnionPay card number. 

125 (ValidationError): If `value` is an invalid UnionPay card number. 

126 """ 

127 pattern = re.compile(r"^62") 

128 return card_number(value) and len(value) == 16 and pattern.match(value) 

129 

130 

131@validator 

132def diners(value: str, /): 

133 """Return whether or not given value is a valid Diners Club card number. 

134 

135 Examples: 

136 >>> diners('3056930009020004') 

137 True 

138 >>> diners('4242424242424242') 

139 ValidationError(func=diners, args={'value': '4242424242424242'}) 

140 

141 Args: 

142 value: 

143 Diners Club card number string to validate 

144 

145 Returns: 

146 (Literal[True]): If `value` is a valid Diners Club card number. 

147 (ValidationError): If `value` is an invalid Diners Club card number. 

148 """ 

149 pattern = re.compile(r"^(30|36|38|39)") 

150 return card_number(value) and len(value) in {14, 16} and pattern.match(value) 

151 

152 

153@validator 

154def jcb(value: str, /): 

155 """Return whether or not given value is a valid JCB card number. 

156 

157 Examples: 

158 >>> jcb('3566002020360505') 

159 True 

160 >>> jcb('4242424242424242') 

161 ValidationError(func=jcb, args={'value': '4242424242424242'}) 

162 

163 Args: 

164 value: 

165 JCB card number string to validate 

166 

167 Returns: 

168 (Literal[True]): If `value` is a valid JCB card number. 

169 (ValidationError): If `value` is an invalid JCB card number. 

170 """ 

171 pattern = re.compile(r"^35") 

172 return card_number(value) and len(value) == 16 and pattern.match(value) 

173 

174 

175@validator 

176def discover(value: str, /): 

177 """Return whether or not given value is a valid Discover card number. 

178 

179 Examples: 

180 >>> discover('6011111111111117') 

181 True 

182 >>> discover('4242424242424242') 

183 ValidationError(func=discover, args={'value': '4242424242424242'}) 

184 

185 Args: 

186 value: 

187 Discover card number string to validate 

188 

189 Returns: 

190 (Literal[True]): If `value` is a valid Discover card number. 

191 (ValidationError): If `value` is an invalid Discover card number. 

192 """ 

193 pattern = re.compile(r"^(60|64|65)") 

194 return card_number(value) and len(value) == 16 and pattern.match(value) 

195 

196 

197@validator 

198def mir(value: str, /): 

199 """Return whether or not given value is a valid Mir card number. 

200 

201 Examples: 

202 >>> mir('2200123456789019') 

203 True 

204 >>> mir('4242424242424242') 

205 ValidationError(func=mir, args={'value': '4242424242424242'}) 

206 

207 Args: 

208 value: 

209 Mir card number string to validate. 

210 

211 Returns: 

212 (Literal[True]): If `value` is a valid Mir card number. 

213 (ValidationError): If `value` is an invalid Mir card number. 

214 """ 

215 pattern = re.compile(r"^(220[0-4])") 

216 return card_number(value) and len(value) == 16 and pattern.match(value)