Skip to content

Commit

Permalink
Don’t ignore mappend failures in validation
Browse files Browse the repository at this point in the history
Previously, a <> b would give success if either a or b were a success!
This should not happen when doing validation. We want any errors to be
propagated.

Fixes #35
  • Loading branch information
matthewbauer committed Jun 11, 2019
1 parent fa76f02 commit be86a2b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Data/Validation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ appValidation ::
-> Validation err a
appValidation m (Failure e1) (Failure e2) =
Failure (e1 `m` e2)
appValidation _ (Failure _) (Success a2) =
Success a2
appValidation _ (Success a1) (Failure _) =
Success a1
appValidation _ (Failure e1) (Success _) =
Failure e1
appValidation _ (Success _) (Failure e2) =
Failure e2
appValidation _ (Success a1) (Success _) =
Success a1
{-# INLINE appValidation #-}
Expand Down
36 changes: 36 additions & 0 deletions test/hunit_tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,38 @@ testValidateNothing =
option = Nothing :: Maybe Int
in TestCase (assertEqual "testValidateFalse" subject expected)

testMappendNY :: Test
testMappendNY =
let v1 = _Failure # [three]
v2 = _Success # [seven]
subject = v1 <> v2
expected = Failure [three]
in TestCase (assertEqual "Failure <> Success" subject expected)

testMappendYN :: Test
testMappendYN =
let v1 = _Success # [three]
v2 = _Failure # [seven]
subject = v1 <> v2
expected = Failure [seven]
in TestCase (assertEqual "Success <> Failure" subject expected)

testMappendYY :: Test
testMappendYY =
let v1 = _Success # [three]
v2 = _Success # [seven]
subject = v1 <> v2 :: Validation [Int] [Int]
expected = Success [three]
in TestCase (assertEqual "Success <> Success" subject expected)

testMappendNN :: Test
testMappendNN =
let v1 = _Failure # [three]
v2 = _Failure # [seven]
subject = v1 <> v2 :: Validation [Int] [Int]
expected = Failure [three, seven]
in TestCase (assertEqual "Failure <> Failure" subject expected)

tests :: Test
tests =
let eitherP :: Proxy Either
Expand Down Expand Up @@ -138,6 +170,10 @@ tests =
, testValidateNothing
, testValidateJust
, testValidateJust'
, testMappendNY
, testMappendYY
, testMappendNN
, testMappendYN
] ++ eithers ++ validations
where

Expand Down

0 comments on commit be86a2b

Please sign in to comment.