git.iko.soy/typeable/credit-card
on master help sign in
fix cvc validation
commit6ce74cab3ebbab63eddac37daddb2c5bf83d6a76 authornikola <nikolayr2001@gmai.com> dateFri 8 Jul 2022, 13:27 (4 years ago) parenta505cb6
4 files changed+75−2
.gitignore+2 spec/CreditCardSpec.hs+27 src/Data/CreditCard.hs+1−2 stack.yaml.lock+45
.gitignorenew file+2
@@ -0,0 +1,2 @@
1+.stack-work
2+.hie-yaml
spec/CreditCardSpec.hs+27
@@ -72,6 +72,29 @@ wrongNumbers =
7272 , "353011133330000"
7373 , "3566002028360505" ]
7474
75+correctCvc :: [Text]
76+correctCvc =
77+ [ "311"
78+ , "3211"
79+ , "9999"
80+ , "0000"
81+ , "000"
82+ , "1234"
83+ , "111"
84+ ]
85+
86+wrongCvc :: [Text]
87+wrongCvc =
88+ [ "31111"
89+ , "a31"
90+ , "zzz"
91+ , "00O"
92+ , "000O"
93+ , "OOO"
94+ , "11111"
95+ , "11"
96+ ]
97+
7598checkNumbers :: [Text] -> CreditCardType -> Spec
7699checkNumbers ns t = describe (show t) $ do
77100 for_ ns $ \n -> it (T.unpack n) $ do
@@ -94,3 +117,7 @@ spec = describe "Credit card number validation" $ do
94117 (parseExpirationDate . formatExpiryDate) d == Just d
95118 prop "JSON instances isomorphism" $ \(cc :: CreditCard) ->
96119 (decode . encode) cc == Just cc
120+ it "Wrong CVC" $ for_ wrongCvc $ \cvc -> do
121+ parseCreditCardSecurity cvc `shouldBe` Nothing
122+ it "Correct CVC" $ for_ correctCvc $ \cvc -> do
123+ parseCreditCardSecurity cvc `shouldBe` Just (CreditCardSecurity cvc)
src/Data/CreditCard.hs+1−2
@@ -54,7 +54,6 @@ import Control.Lens hiding (elements)
5454import Control.Monad (guard)
5555import Data.Aeson as Aeson
5656import Data.Aeson.Inflections (defaultFieldLabelModifier)
57−import Data.Aeson.Types (withText)
5857import Data.Char (isDigit)
5958import Data.Digit
6059import Data.Either (isRight)
@@ -245,7 +244,7 @@ parseCreditCardSecurity txt
245244 | otherwise = Nothing
246245 where
247246 isValid = and
248− [ T.length txt == 3
247+ [ (3, 4) `inRange` T.length txt
249248 , T.all isDigit txt ]
250249
251250data CreditCard = CreditCard
stack.yaml.locknew file+45
@@ -0,0 +1,45 @@
1+# This file was autogenerated by Stack.
2+# You should not edit this file by hand.
3+# For more information, please see the documentation at:
4+# https://docs.haskellstack.org/en/stable/lock_files
5+
6+packages:
7+- completed:
8+ name: aeson-inflections
9+ version: 0.1.0.0
10+ git: https://github.com/typeable/aeson-inflections
11+ pantry-tree:
12+ size: 305
13+ sha256: 306e91dd974dae0170d0b7cf7b74769782b4d4342575a2e39d82224ac69d6dad
14+ commit: 66ac49cc1e468f36bf9d6c78b73bdabcfca0855d
15+ original:
16+ git: https://github.com/typeable/aeson-inflections
17+ commit: 66ac49cc1e468f36bf9d6c78b73bdabcfca0855d
18+- completed:
19+ name: digit
20+ version: 0.1.0.0
21+ git: https://github.com/typeable/digit.git
22+ pantry-tree:
23+ size: 314
24+ sha256: 41a3214a12218a1cf85baa3dc33292491b662c32218ba88ce0f0e27478e1d78c
25+ commit: e03f087c90c9228a9ae52a0e516eba22589eef94
26+ original:
27+ git: https://github.com/typeable/digit.git
28+ commit: e03f087c90c9228a9ae52a0e516eba22589eef94
29+- completed:
30+ name: missing-lens
31+ version: 0.1.0.0
32+ git: https://github.com/typeable/missing-lens.git
33+ pantry-tree:
34+ size: 221
35+ sha256: ab572e39921e6f367dbc4b0a8df29fa170dc4ab015e407c4dfe03d84d07665ec
36+ commit: 328659fdda8fd7ec509abfab6298775062fc2926
37+ original:
38+ git: https://github.com/typeable/missing-lens.git
39+ commit: 328659fdda8fd7ec509abfab6298775062fc2926
40+snapshots:
41+- completed:
42+ size: 616897
43+ url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/19/0.yaml
44+ sha256: bbf2be02f17940bac1f87cb462d4fb0c3355de6dcfc53d84f4f9ad3ee2164f65
45+ original: lts-19.0