Skip to content

Deprecate some functions in Validation #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/FSharpPlus/Validations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ module Validation=
| Success a -> f a

/// orElse v a returns 'a when v is Failure, and the a in Success a.
let orElse v (a: 'a) =
match v with
| Failure _ -> a
| Success x -> x
[<System.Obsolete("Use Validation.defaultValue instead.")>]
let orElse v (a: 'a) = match v with | Failure _ -> a | Success x -> x
/// defaultValue value source returns value when source is Failure, and the v in Success v.
let defaultValue (value:'a) (source:Validation<'err,'a>) :'a = match source with Success v -> v | _ -> value
/// defaultWith returns either x when the source is Success x, otherwise applies the function f on e if the source is Failure e.
let defaultWith (f:'err->'a) (source:Validation<'err,'a>) :'a = match source with | Success x -> x | Failure e -> f e

/// Return the 'a or run the given function over the 'e.
let valueOr ea (v: Validation<'e,'a>) =
match v with
| Failure e -> ea e
| Success a -> a
[<System.Obsolete("Use Validation.defaultWith instead.")>]
let valueOr ea (v: Validation<'e,'a>) = match v with | Failure e -> ea e | Success a -> a

/// 'liftResult' is useful for converting a 'Result' to an 'Validation'
/// when the 'Error' of the 'Result' needs to be lifted into a 'Semigroup'.
Expand All @@ -113,6 +113,7 @@ module Validation=
///
/// validate : 'e -> ('a -> bool) -> 'a -> Validation<'e, 'a>
///
[<System.Obsolete("This function will not be supported in future versions.")>]
let validate (e: 'e) (p: 'a -> bool) (a: 'a) : Validation<'e,'a> = if p a then Success a else Failure e

#if !FABLE_COMPILER
Expand All @@ -128,6 +129,7 @@ module Validation=
/// This can be thought of as having the less general type:
///
/// ensure : 'e -> ('a -> 'bool) -> Validation<'a,'e> -> Validation<'a,'e>
[<System.Obsolete("This function will not be supported in future versions.")>]
let ensure (e: 'e) (p: 'a-> bool) = function
| Failure x -> Failure x
| Success a -> validate e p a
Expand Down
10 changes: 5 additions & 5 deletions tests/FSharpPlus.Tests/Validations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,15 @@ let testEnsureRightTrue () =
areEqual (Success seven) subject

[<Test>]
let testOrElseRight () =
let v = Success seven
let subject = Validation.orElse v three
let testDefaultValueSuccess () =
let v = Success seven
let subject = Validation.defaultValue three v
areEqual seven subject

[<Test>]
let testOrElseLeft () =
let testDefaultValueFailure () =
let v = Failure seven
let subject = Validation.orElse v three
let subject = Validation.defaultValue three v
areEqual three subject

//testEnsureLeftFalse, testEnsureLeftTrue, testEnsureRightFalse, testEnsureRightTrue,
Expand Down