Table of Contents

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

str string

The string to validate.

error Error

The 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

result Result<T?>

The result containing a potentially null value type.

error Error

The 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

T

The 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

result Result<T>

The result containing a potentially null value.

error Error

The 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

T

The 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

result Result<TValue>

The result to validate.

predicate Func<bool>

The predicate to test.

error Error

The 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

TValue

Type 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

result Result<TValue>

The result to validate.

predicate Func<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

TValue

Type 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

result Result<TValue>

The result to validate.

predicate Func<TValue, bool>

The predicate function to test the value.

errorPredicate Func<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

TValue

Type 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

result Result<TValue>

The result to validate.

predicate Func<TValue, bool>

The predicate function to test the value.

error Error

The 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

TValue

Type 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

result Result<TValue>

The result to validate.

predicate Func<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

TValue

Type of the result value.