Class EnsureExtensions
- Namespace
- FunctionalDdd
- Assembly
- FunctionalDdd.RailwayOrientedProgramming.dll
Provides extension methods for ensuring conditions on Result values. If conditions fail, the result is converted to a failure with the specified error.
public static class EnsureExtensions
- Inheritance
-
EnsureExtensions
- Inherited Members
Remarks
Ensure methods allow you to validate result values against conditions and convert successful results to failures if the validation fails. This is useful for enforcing business rules and constraints in a functional pipeline without breaking the railway pattern.
Methods
Ensure(bool, Error)
Returns a success result if the flag is true; otherwise returns a failure with the specified error.
public static Result<Unit> Ensure(bool flag, Error error)
Parameters
Returns
EnsureNotNullOrWhiteSpace(string?, Error)
Ensures a string is not null or whitespace.
public static Result<string> EnsureNotNullOrWhiteSpace(this string? str, Error error)
Parameters
strstringThe string to validate.
errorErrorThe error to return if the string is null or whitespace.
Returns
- Result<string>
A success result with the string if valid; otherwise a failure with the specified error.
Ensure<TValue>(Result<TValue>, Func<Result<TValue>>)
Returns a new failure result if the predicate result is a failure. Otherwise returns the starting result.
public static Result<TValue> Ensure<TValue>(this Result<TValue> result, Func<Result<TValue>> predicate)
Parameters
resultResult<TValue>The result to validate.
predicateFunc<Result<TValue>>The predicate function that returns a Result.
Returns
- Result<TValue>
The original result if both it and the predicate result are successes; otherwise a failure.
Type Parameters
TValueType of the result value.
Ensure<TValue>(Result<TValue>, Func<bool>, Error)
Returns a new failure result if the predicate is false. Otherwise returns the starting result.
public static Result<TValue> Ensure<TValue>(this Result<TValue> result, Func<bool> predicate, Error error)
Parameters
resultResult<TValue>The result to validate.
predicateFunc<bool>The predicate to test.
errorErrorThe error to return if the predicate is false.
Returns
- Result<TValue>
The original result if success and predicate is true; otherwise a failure with the specified error.
Type Parameters
TValueType of the result value.
Ensure<TValue>(Result<TValue>, Func<TValue, Result<TValue>>)
Returns a new failure result if the predicate result is a failure. Otherwise returns the starting result. The predicate receives the value.
public static Result<TValue> Ensure<TValue>(this Result<TValue> result, Func<TValue, Result<TValue>> predicate)
Parameters
resultResult<TValue>The result to validate.
predicateFunc<TValue, Result<TValue>>The predicate function that receives the value and returns a Result.
Returns
- Result<TValue>
The original result if both it and the predicate result are successes; otherwise a failure.
Type Parameters
TValueType of the result value.
Ensure<TValue>(Result<TValue>, Func<TValue, bool>, Error)
Returns a new failure result if the predicate is false. Otherwise returns the starting result.
public static Result<TValue> Ensure<TValue>(this Result<TValue> result, Func<TValue, bool> predicate, Error error)
Parameters
resultResult<TValue>The result to validate.
predicateFunc<TValue, bool>The predicate function to test the value.
errorErrorThe error to return if the predicate is false.
Returns
- Result<TValue>
The original result if success and predicate is true; otherwise a failure with the specified error.
Type Parameters
TValueType of the result value.
Ensure<TValue>(Result<TValue>, Func<TValue, bool>, Func<TValue, Error>)
Returns a new failure result if the predicate is false. Otherwise returns the starting result. The error is generated by a function that receives the value.
public static Result<TValue> Ensure<TValue>(this Result<TValue> result, Func<TValue, bool> predicate, Func<TValue, Error> errorPredicate)
Parameters
resultResult<TValue>The result to validate.
predicateFunc<TValue, bool>The predicate function to test the value.
errorPredicateFunc<TValue, Error>The function that generates an error from the value if the predicate is false.
Returns
- Result<TValue>
The original result if success and predicate is true; otherwise a failure with an error from the error function.
Type Parameters
TValueType of the result value.