FsToolkit.ErrorHandling
  • README
  • FsToolkit.ErrorHandling
    • Result
      • apply
      • bind
      • check
      • Computation Expression
      • either Functions
      • fold
      • ignore
      • map
      • map2
      • map3
      • mapError
      • Operators
      • orElse Functions
      • Other Functions
      • require Functions
      • tee Functions
      • tryCreate
      • zip
      • zipError
      • Lists
        • traverseResultM
        • sequenceResultM
        • traverseResultA
        • sequenceResultA
      • Sequences
        • traverseResultM
        • sequenceResultM
        • traverseResultA
        • sequenceResultA
      • Transforms
        • ofChoice
    • Option
      • bind
      • bindNull
      • Computation Expression
      • either
      • map
      • map2
      • map3
      • sequenceAsync
      • sequenceResult
      • sequenceTask
      • sequenceTaskResult
      • tee Functions
      • traverseAsync
      • traverseResult
      • traverseTask
      • traverseTaskResult
      • zip
      • Lists
        • traverseOptionM
        • sequenceOptionM
        • traverseVOptionM
        • sequenceVOptionM
      • Sequences
        • traverseOptionM
        • sequenceOptionM
        • traverseVOptionM
        • sequenceVOptionM
      • Transforms
        • ofNull
        • ofPair
        • ofResult
        • ofValueOption
        • toValueOption
    • ResultOption
      • apply
      • bind
      • Computation Expression
      • ignore
      • map
      • map2
      • map3
      • mapError
      • Operators
      • zip
      • zipError
      • Transforms
        • ofChoice
        • ofOption
        • ofResult
    • AsyncResult
      • apply
      • bind
      • check
      • Computation Expression
      • error
      • foldResult
      • ignore
      • map
      • map2
      • map3
      • mapError
      • Operators
      • Other Functions
      • zip
      • zipError
      • List
        • traverseAsyncResultM
        • sequenceAsyncResultM
        • traverseAsyncResultA
        • sequenceAsyncResultA
      • Sequences
        • traverseAsyncResultM
        • sequenceAsyncResultM
        • traverseAsyncResultA
        • sequenceAsyncResultA
      • Transforms
        • ofAsync
        • ofResult
        • ofTask
        • ofTaskAction
    • AsyncOption
      • apply
      • bind
      • Computation Expression
      • either
      • map
      • orElse Functions
      • Other Functions
    • AsyncResultOption
      • apply
      • bind
      • Computation Expression
      • ignore
      • map
      • map2
      • map3
      • Operators
      • Transforms
        • ofAsyncOption
        • ofAsyncResult
        • ofOption
        • ofResult
    • Task
      • apply
      • bind
      • bindV
      • catch
      • Computation Expression
      • ignore
      • map
      • mapV
      • map2
      • map3
      • zip
      • Transforms
        • ofUnit
    • TaskOption
      • apply
      • bind
      • Computation Expression
      • either
      • map
      • Other Functions
      • zip
    • TaskResult
      • apply
      • bind
      • check
      • catch
      • Computation Expression
      • ignore
      • map
      • map2
      • map3
      • mapError
      • Operators
      • Other Functions
      • error
      • zip
      • zipError
      • Lists
        • traverseTaskResultM
        • sequenceTaskResultM
        • traverseTaskResultA
        • sequenceTaskResultA
      • Transforms
        • ofAsync
        • ofResult
        • ofTask
    • TaskResultOption
      • apply
      • bind
      • Computation Expression
      • ignore
      • map
      • map2
      • map3
      • Operators
    • Validation
      • apply
      • Computation Expression
      • error
      • map
      • map2
      • map3
      • mapError
      • mapErrors
      • Operators
      • zip
      • Transforms
        • ofChoice
        • ofResult
    • AsyncValidation
      • apply
      • Computation Expression
      • error
      • map
      • map2
      • map3
      • mapError
      • mapErrors
      • Operators
      • zip
      • Transforms
        • ofChoice
        • ofResult
    • TaskValidation
      • apply
      • Computation Expression
      • error
      • map
      • map2
      • map3
      • mapError
      • mapErrors
      • Operators
      • zip
      • Transforms
        • ofChoice
        • ofResult
  • FsToolkit.ErrorHandling.AsyncSeq
    • AsyncSeq
      • Computation Expression
  • FsToolkit.ErrorHandling.IcedTasks
    • CancellableTaskResult
      • apply
      • bind
      • Computation Expression
      • getCancellationToken
      • map
      • Operators
      • Other Functions
      • zip
      • parallelZip
    • CancellableTaskValidation
      • apply
      • bind
      • Computation Expression
      • error
      • getCancellationToken
      • map
      • map2
      • map3
      • mapError
      • mapErrors
      • Operators
      • zip
      • parallelZip
      • Transforms
        • ofChoice
        • ofResult
  • FsToolkit.ErrorHandling.JobResult
    • Job
      • apply
      • map2
      • map3
      • singleton
      • zip
    • JobOption
      • apply
      • bind
      • ce
      • either
      • map
    • JobResult
      • apply
      • bind
      • catch
      • Computation Expression
      • error
      • fromTask
      • fromUnitTask
      • ignore
      • map
      • map2
      • map3
      • mapError
      • Operators
      • Other Functions
      • zip
      • zipError
      • Lists
        • traverseJobResultM
        • sequenceJobResultM
        • traverseJobResultA
        • sequenceJobResultA
      • Transforms
        • ofAsync
        • ofJob
        • ofResult
        • fromTask
        • fromUnitTask
    • JobResultOption
      • apply
      • bind
      • Computation Expression
      • ignore
      • map
      • map2
      • map3
  • General Docs
    • Bind Mappings
Powered by GitBook
On this page
  • AsyncValidation Infix Operators
  • Examples
  1. FsToolkit.ErrorHandling
  2. AsyncValidation

Operators

AsyncValidation Infix Operators

Namespace: FsToolkit.ErrorHandling.Operator.AsyncValidation

FsToolkit.ErrorHandling provides the standard infix operators for map (<!>), apply (<*>), and bind (>>=) to work with Result<'a, 'b list>.

There are also variants of the map and apply operators (<!^> and <*^>) that accept Result<'a, 'b> (non-list) as the right-hand argument.

Examples

Example 1

Assume that we have following types and functions:

type Latitude = private Latitude of float with
  // float -> Async<Result<Latitude, string list>>
  static member TryCreate (lat : float) =
    // ...

type Longitude = private Longitude of float with
  // float -> Async<Result<Longitude, string list>>
  static member TryCreate (lng : float) =
    // ...

type Tweet = private Tweet of string with
  // string -> Async<Result<Tweet, string list>>
  static member TryCreate (tweet : string) =
    // ...

// Latitude -> Longitude -> Tweet -> CreatePostRequest
let createPostRequest lat long tweet =
  // ...

We can make use of the standard operators in the AsyncValidation Operators module to perform the asyncValidation of the incoming request and capture all the errors as shown below:

open FsToolkit.ErrorHandling.Operator.AsyncValidation

// float -> float -> string -> Async<Result<CreatePostRequest, string list>>
let validateCreatePostRequest lat lng tweet = 
  createPostRequest
  <!> Latitude.TryCreate lat
  <*> Longitude.TryCreate lng
  <*> Tweet.TryCreate tweet

By using the AsyncValidation operators instead of the Result operators, we collect all the errors:

validateCreatePostRequest 300. 400. ""
// Error
     ["300.0 is a invalid latitude value"
      "400.0 is a invalid longitude value"
      "Tweet shouldn't be empty"]

Example 2

In the above example, all the TryCreate functions return a string list as the error type (Async<Result<'a, string list>>). If these functions instead returned Async<Result<'a, string>> (only a single error), we can use <*^> and <!^> to get the same result:

open FsToolkit.ErrorHandling.Operator.AsyncValidation

// float -> float -> string -> Async<Result<CreatePostRequest, string list>>
let validateCreatePostRequest lat lng tweet = 
  createPostRequest
  <!^> Latitude.TryCreate lat
  <*^> Longitude.TryCreate lng
  <*^> Tweet.TryCreate tweet
PreviousmapErrorsNextzip

Last updated 1 year ago