Skip to content

Commit ae0e031

Browse files
committed
Add overload for plain Result case; add test handling multiple errors case
1 parent 73fc89e commit ae0e031

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/FsToolkit.ErrorHandling/TaskValidationCE.fs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace FsToolkit.ErrorHandling
22

3+
open System
34
open System.Threading.Tasks
45
open System.Runtime.CompilerServices
56
open System.Threading
@@ -554,6 +555,19 @@ module TaskValidationCEExtensionsMediumPriority =
554555
|> Async.map Ok
555556
|> Async.StartImmediateAsTask
556557

558+
member inline _.Source(s: Async<Result<'ok, 'error>>) : TaskValidation<'ok, 'error> =
559+
s
560+
|> AsyncResult.mapError List.singleton
561+
|> Async.StartImmediateAsTask
562+
563+
member inline _.Source(s: Task<Result<'ok, 'error>>) : TaskValidation<'ok, 'error> =
564+
TaskResult.mapError List.singleton s
565+
566+
member inline _.Source(result: Result<_, _>) : Task<Validation<_, _>> =
567+
result
568+
|> Validation.ofResult
569+
|> Task.singleton
570+
557571
[<AutoOpen>]
558572
module TaskValidationCEExtensionsHighPriority2 =
559573

tests/FsToolkit.ErrorHandling.TaskResult.Tests/TaskValidationCE.fs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,31 @@ let ``TaskValidationCE applicative tests`` =
674674
Expect.equal actual (Validation.ok 6) "Should be ok"
675675
}
676676

677+
testCaseTask "Fail Path Result"
678+
<| fun () ->
679+
task {
680+
let expected =
681+
Error [
682+
"Error 1"
683+
"Error 2"
684+
]
685+
686+
let! actual =
687+
taskValidation {
688+
let! a = Ok 3
689+
and! b = Ok 2
690+
and! c = Error "Error 1"
691+
and! d = Error "Error 2"
692+
693+
return
694+
a + b
695+
- c
696+
- d
697+
}
698+
699+
Expect.equal actual expected "Should be Error"
700+
}
701+
677702
testCaseTask "Fail Path Validation"
678703
<| fun () ->
679704
task {

0 commit comments

Comments
 (0)