git.iko.soy/typeable/schematic
on master help sign in
Better ISO8601 regex
commit1da8887f8831d24de50a610e5b61c56dc813f485 authorDenis Redozubov <denis.redozubov@gmail.com> dateMon 2 Oct 2017, 15:05 (9 years ago) parentfced3b9
2 files changed+35−7
src/Data/Schematic/Helpers.hs+6−3 test/HelpersSpec.hs+29−4
src/Data/Schematic/Helpers.hs+6−3
@@ -17,11 +17,14 @@ type IsUUID = '[UUIDRegex]
1717-- ]
1818
1919-- components
20−type ISO8601Date = "[1-9][0-9]{3}-(0[1-9]|1[0-2])-[0-3][0-9]"
20+type ISO8601Date = "[1-9][0-9]{3}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])"
2121
22−type ISO8601Time = "(?:[0-2][0-9]:[0-2][0-9]:[0-6][0-9])?"
22+type ISO8601Time = "([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]"
2323
24−type ISO8601DateTime = AppendSymbol ISO8601Date (AppendSymbol "T" ISO8601Time)
24+type ISO8601DateTime =
25+ AppendSymbol
26+ ISO8601Date
27+ (AppendSymbol "(T" (AppendSymbol ISO8601Time ")?"))
2528
2629type ISO8601UTC = "(Z|\\+00:00)"
2730
test/HelpersSpec.hs+29−4
@@ -32,6 +32,19 @@ iso8601dates = [ "1985-04-12" ]
3232invalidIso8601dates :: [Text]
3333invalidIso8601dates = [ "1999-13-12" ]
3434
35+type ISO8601TimeSchema = 'SchemaObject '[ '("time", 'SchemaText IsTime) ]
36+
37+iso8601times :: [Text]
38+iso8601times =
39+ [ "19:23:00"
40+ , "00:00:00"
41+ , "23:47:12" ]
42+
43+invalidIso8601times :: [Text]
44+invalidIso8601times =
45+ [ "00:60:00"
46+ , "24:01:02" ]
47+
3548type ISO8601DateTimeSchema = 'SchemaObject '[ '("datetime", 'SchemaText IsDateTime) ]
3649
3750-- | Taken from ISO8601 RFC 3339
@@ -47,11 +60,10 @@ iso8601datetimes =
4760
4861invalidIso8601datetimes :: [Text]
4962invalidIso8601datetimes =
50− [ "1985-04-29T23:20:50.52Z"
51− , "1996-12-129T16:39:57-08:00"
63+ [ "1996-13-129T16:39:57-08:00"
5264 , "1990-2-31T23:59:60Z"
53− , "1990-12-31T15:59:70"
54− , "1937-01-01T12:00:27.87+24:20"
65+ , "1990-12-32T15:59:70"
66+ , "1937-01-1T12::27.87+24:20"
5567 ]
5668
5769spec :: Spec
@@ -82,6 +94,19 @@ spec = do
8294 (decodeAndValidateJson json :: ParseResult (JsonRepr ISO8601DateSchema))
8395 `shouldSatisfy` isValidationError
8496
97+ describe "ISO8601 time: " $ do
98+ for_ iso8601times $ \dt -> do
99+ it ("validates correct time - " <> dt ^. unpacked) $ do
100+ let json = "{\"time\": \"" <> (dt ^. unpacked . packedChars) <> "\" }"
101+ (decodeAndValidateJson json :: ParseResult (JsonRepr ISO8601TimeSchema))
102+ `shouldSatisfy` isValid
103+
104+ for_ invalidIso8601times $ \dt -> do
105+ it ("fails to validate incorrect time - " <> dt ^. unpacked) $ do
106+ let json = "{\"time\": \"" <> (dt ^. unpacked . packedChars) <> "\" }"
107+ (decodeAndValidateJson json :: ParseResult (JsonRepr ISO8601TimeSchema))
108+ `shouldSatisfy` isValidationError
109+
85110 describe "ISO8601 datetime: " $ do
86111 for_ iso8601datetimes $ \dt -> do
87112 it ("validates correct datetime - " <> dt ^. unpacked) $ do