Skip to content

Commit d7eea8e

Browse files
committed
+ Validation.ofOptionWith
1 parent a54afac commit d7eea8e

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

src/FSharpPlus/Data/Validation.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,18 @@ module Validation =
186186
/// Creates a Validation<'Error,'T> from a Choice<'T,'Error>.
187187
let ofChoice (x: Choice<'T,'Error>) = match x with Choice1Of2 a -> Success a | Choice2Of2 e -> Failure e
188188

189+
/// <summary>Converts an option to a Validation.</summary>
190+
/// <param name="errorValue">The error value to be used in case of None.</param>
191+
/// <param name="source">The option value.</param>
192+
/// <returns>The resulting Validation value.</returns>
193+
let ofOptionWith (errorValue: 'Error) (source: 'T option) = match source with Some x -> Success x | None -> Failure errorValue
194+
195+
/// <summary>Converts a voption to a Validation.</summary>
196+
/// <param name="errorValue">The error value to be used in case of None.</param>
197+
/// <param name="source">The voption value.</param>
198+
/// <returns>The resulting Validation value.</returns>
199+
let ofValueOptionWith (errorValue: 'Error) (source: 'T voption) = match source with ValueSome x -> Success x | ValueNone -> Failure errorValue
200+
189201
/// <summary> Extracts a value from either side of a Validation.</summary>
190202
/// <param name="successMapper">Function to be applied to source, if it contains a Success value.</param>
191203
/// <param name="failureMapper">Function to be applied to source, if it contains a Failure value.</param>

0 commit comments

Comments
 (0)