on master help sign in
Accumulate NonEmpty errors
7bcf368Adam Bergmark
Data/Aeson/Types/FromJSON.hs+1−1
@@ -1414,7 +1414,7 @@ parseVersionText = go . readP_to_S parseVersion . unpack
1414
1414
1415instance FromJSON1 NonEmpty where
1415instance FromJSON1 NonEmpty where
1416 liftParseJSON p _ = withArray "NonEmpty a" $
1416 liftParseJSON p _ = withArray "NonEmpty a" $
1417− (>>= ne) . Tr.sequence . zipWith (parseIndexedJSON p) [0..] . V.toList
1417+ (>>= ne) . accSequence . zipWith (parseIndexedJSON p) [0..] . V.toList
1418 where
1418 where
1419 ne [] = fail "Expected a NonEmpty but got an empty list"
1419 ne [] = fail "Expected a NonEmpty but got an empty list"
1420 ne (x:xs) = pure (x :| xs)
1420 ne (x:xs) = pure (x :| xs)
tests/UnitTests/AccErrors.hs+9−2
@@ -21,8 +21,9 @@ import qualified Data.Sequence as Seq
21
21
22tests :: Test
22tests :: Test
23tests = testGroup "Error accumulation" [
23tests = testGroup "Error accumulation" [
24− testCase "seq" seq
24+ testCase "Seq" seq
25− , testCase "vector" vector
25+ , testCase "Vector" vector
26+ , testCase "NonEmpty" nonEmpty
26 ]
27 ]
27
28
28decoder :: FromJSON a
29decoder :: FromJSON a
@@ -41,3 +42,9 @@ vector = do
41 let res = decoder "[true, null]" :: Either (NonEmpty (JSONPath, String)) (Vector Int)
42 let res = decoder "[true, null]" :: Either (NonEmpty (JSONPath, String)) (Vector Int)
42 let message i s = ([Index i], "expected Int, encountered " <> s)
43 let message i s = ([Index i], "expected Int, encountered " <> s)
43 res @=? Left (NL.fromList [message 0 "Boolean", message 1 "Null"])
44 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"])