From ce081c6f48e49aa935bcac3724cc555aaa5613f1 Mon Sep 17 00:00:00 2001
From: Gustavo Leon <1261319+gusty@users.noreply.github.com>
Date: Wed, 8 Nov 2023 06:51:41 +0100
Subject: [PATCH] + Validation.ofOptionWith
---
src/FSharpPlus/Data/Validation.fs | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/src/FSharpPlus/Data/Validation.fs b/src/FSharpPlus/Data/Validation.fs
index ab2513d31..4cc176805 100644
--- a/src/FSharpPlus/Data/Validation.fs
+++ b/src/FSharpPlus/Data/Validation.fs
@@ -186,6 +186,18 @@ module Validation =
/// Creates a Validation<'Error,'T> from a Choice<'T,'Error>.
let ofChoice (x: Choice<'T,'Error>) = match x with Choice1Of2 a -> Success a | Choice2Of2 e -> Failure e
+ /// Converts an option to a Validation.
+ /// The error value to be used in case of None.
+ /// The option value.
+ /// The resulting Validation value.
+ let ofOptionWith (errorValue: 'Error) (source: 'T option) = match source with Some x -> Success x | None -> Failure errorValue
+
+ /// Converts a voption to a Validation.
+ /// The error value to be used in case of None.
+ /// The voption value.
+ /// The resulting Validation value.
+ let ofValueOptionWith (errorValue: 'Error) (source: 'T voption) = match source with ValueSome x -> Success x | ValueNone -> Failure errorValue
+
/// Extracts a value from either side of a Validation.
/// Function to be applied to source, if it contains a Success value.
/// Function to be applied to source, if it contains a Failure value.