Class EnsureExtensions
- Namespace
- Trellis
- Assembly
- Trellis.Core.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
Examples
// Non-generic guard at the top of a handler:
var guard = Result.Ensure(order.IsOpen, new Error.Conflict(null, "order_closed"));
// Value-threaded guard inside a pipeline:
var validated = parseAmount
.Ensure(amt => amt > 0, Error.UnprocessableContent.ForField("amount", "must_be_positive"));
Remarks
Ensure is the canonical guard primitive in Trellis. Prefer it over hand-written
if (!cond) return Result.Fail(error); blocks — Ensure participates in tracing, composes
inside railway pipelines, and threads through both Result and Result<TValue>.
For non-generic guards (no value to thread), call Ensure(bool, Error) as a
static factory: Result.Ensure(condition, error) — it returns Result.Ok() when
condition is true and Result.Fail(error) otherwise.
For value-threaded guards, call result.Ensure(predicate, error) as an extension on
Result<TValue>. The extension shape short-circuits on failure: if the receiver is
already a failure, the predicate is not evaluated and the original error is preserved.
Methods
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.
EnsureNotNull<T>(Result<T?>, Error)
Ensures the value inside the result is not null for nullable value types,
narrowing from Result<T?> to Result<T>.
Returns a failure if the value is null.
public static Result<T> EnsureNotNull<T>(this Result<T?> result, Error error) where T : struct
Parameters
resultResult<T?>The result containing a potentially null value type.
errorErrorThe error to return if the value is null.
Returns
- Result<T>
A success result with the unwrapped value if present; otherwise a failure with the specified error.
Type Parameters
TThe value type of the value.
EnsureNotNull<T>(Result<T?>, Error)
Ensures the value inside the result is not null, narrowing from Result<T?> to Result<T>.
Returns a failure if the value is null.
public static Result<T> EnsureNotNull<T>(this Result<T?> result, Error error) where T : class
Parameters
resultResult<T>The result containing a potentially null value.
errorErrorThe error to return if the value is null.
Returns
- Result<T>
A success result with the non-null value if present; otherwise a failure with the specified error.
Type Parameters
TThe reference type of the 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<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<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.
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, 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.