You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The intent of check is to allow an Ok result value to be validated.
6
+
7
+
`check` takes a validation function of the form `'ok -> Result<unit, 'error>` and a result of the form `Result<'ok, 'error>`.
8
+
9
+
If the result is `Ok x` then the validation function is applied, and if the validation function returns an error, this new error is returned. Otherwise, the original `Ok x` result is returned. If the original result is an Error, the original result is returned.
The intent of check is to allow an Ok result value to be validated.
6
+
7
+
`check` takes a validation function of the form `'ok -> Task<Result<unit, 'error>>` and a result of the form `Task<Result<'ok, 'error>>`.
8
+
9
+
If the task-wrapped result is `Ok x` then the validation function is applied, and if the validation function returns an error, this new task-wrapped error is returned. Otherwise, the original task-wrapped `Ok x` result is returned. If the original task-wrapped result is an Error, the original task-wrapped result is returned.
Copy file name to clipboardExpand all lines: src/FsToolkit.ErrorHandling/Result.fs
+45Lines changed: 45 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -424,6 +424,28 @@ module Result =
424
424
| Some x -> Ok x
425
425
| None -> Error error
426
426
427
+
/// <summary>
428
+
/// If the input result is <c>Ok</c>, applies a predicate to the <c>Ok</c> value.
429
+
/// If the predicate returns true, then returns the original <c>Ok</c> Result.
430
+
/// Otherwise, returns a new <c>Error</c> result with the provided error.
431
+
///
432
+
/// If the input result is <c>Error</c>, just returns the input result.
433
+
///
434
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/requirefunctions#require</href>
435
+
/// </summary>
436
+
/// <param name="predicate">Predicate applied to the <c>Ok</c> value of the result. </param>
437
+
/// <param name="error">The error to return if the predicate returns false.</param>
/// <returns> The input <paramref name="result"/> if it is <c>Error</c> or if the provided <paramref name="predicate"/> returns true. Otherwise returns a new <c>Error</c> result with the value <paramref name="error"/>.</returns>
440
+
let inlinerequire
441
+
([<InlineIfLambda>]predicate:'ok ->bool)
442
+
(error:'error)
443
+
(result:Result<'ok,'error>)
444
+
:Result<'ok,'error>=
445
+
match result with
446
+
| Error err -> Error err
447
+
| Ok ok ->if predicate ok then Ok ok else Error(error)
448
+
427
449
/// <summary>
428
450
/// Replaces an error value with a custom error value.
429
451
///
@@ -656,3 +678,26 @@ module Result =
656
678
| Error x1res, Error x2res -> Error(x1res, x2res)
657
679
| Ok e,_-> Ok e
658
680
|_, Ok e -> Ok e
681
+
682
+
683
+
/// <summary>
684
+
/// When <paramref name="result"/> is <c>Ok</c>, applies <paramref name="checkFunc"/> to the <c>Ok</c> value.
685
+
/// If <paramref name="checkFunc"/> returns an <c>Ok</c> unit value, returns <paramref name="result"/>.
686
+
/// Otherwise, returns the <c>Error</c> value from the <paramref name="checkFunc"/> as a new result.
687
+
///
688
+
/// When <paramref name="result"/> is <c>Error</c>, returns <paramref name="result"/>.
689
+
///
690
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/check</href>
691
+
/// </summary>
692
+
/// <param name="checkFunc">The function that performs a check against the <c>Ok</c> value of <paramref name="result"/>. Returns <c>Error</c> value from the function if error.</param>
/// <returns>The input <paramref name="result"/> if <c>Error</c> or <paramref name="checkFunc"/> returns a <c>Ok</c> result. Returns the <c>Error</c> value from <paramref name="checkFunc"/> if it returns an <c>Error</c> result.</returns>
0 commit comments