on master help sign in
Accumulate NonEmpty errors
commit7bcf368af913fd03199293a026665d74e7ebed95 authorAdam Bergmark <adam@bergmark.nl> dateMon 25 Dec 2017, 18:53 (9 years ago) parent9eef7e7
2 files changed+10−3
Data/Aeson/Types/FromJSON.hs+1−1 tests/UnitTests/AccErrors.hs+9−2
Data/Aeson/Types/FromJSON.hs+1−1
@@ -1414,7 +1414,7 @@ parseVersionText = go . readP_to_S parseVersion . unpack
14141414
14151415instance FromJSON1 NonEmpty where
14161416 liftParseJSON p _ = withArray "NonEmpty a" $
1417− (>>= ne) . Tr.sequence . zipWith (parseIndexedJSON p) [0..] . V.toList
1417+ (>>= ne) . accSequence . zipWith (parseIndexedJSON p) [0..] . V.toList
14181418 where
14191419 ne [] = fail "Expected a NonEmpty but got an empty list"
14201420 ne (x:xs) = pure (x :| xs)
tests/UnitTests/AccErrors.hs+9−2
@@ -21,8 +21,9 @@ import qualified Data.Sequence as Seq
2121
2222tests :: Test
2323tests = testGroup "Error accumulation" [
24− testCase "seq" seq
25− , testCase "vector" vector
24+ testCase "Seq" seq
25+ , testCase "Vector" vector
26+ , testCase "NonEmpty" nonEmpty
2627 ]
2728
2829decoder :: FromJSON a
@@ -41,3 +42,9 @@ vector = do
4142 let res = decoder "[true, null]" :: Either (NonEmpty (JSONPath, String)) (Vector Int)
4243 let message i s = ([Index i], "expected Int, encountered " <> s)
4344 res @=? Left (NL.fromList [message 0 "Boolean", message 1 "Null"])
45+
46+nonEmpty :: Assertion
47+nonEmpty = do
48+ let res = decoder "[true, null]" :: Either (NonEmpty (JSONPath, String)) (NL.NonEmpty Int)
49+ let message i s = ([Index i], "expected Int, encountered " <> s)
50+ res @=? Left (NL.fromList [message 0 "Boolean", message 1 "Null"])