Table of Contents

Class MaybeInvariant

Namespace
Trellis
Assembly
Trellis.Core.dll

Provides static methods for validating invariants across multiple Maybe<T> values. These methods express relationships between correlated optional values — such as "all or none", "if A then B", or "exactly one" — and return Result<TValue> with field-level Error.UnprocessableContent details when the invariant is violated.

public static class MaybeInvariant
Inheritance
MaybeInvariant
Inherited Members

Examples

// Winner and winner points must both be present or both absent
var result = MaybeInvariant.AllOrNone(winner, winnerPoints, "winner", "winnerPoints");

// A discount requires a coupon code
var result = MaybeInvariant.Requires(discount, couponCode, "discount", "couponCode");

// Payment can be by card or bank transfer, but not both
var result = MaybeInvariant.MutuallyExclusive(cardPayment, bankTransfer, "cardPayment", "bankTransfer");

Remarks

Use these helpers in aggregate factory methods, command validation, and domain methods where multiple optional values must satisfy a relationship constraint.

All methods return Result<TValue> with Unit. Chain with .Combine() to compose multiple invariant checks, or use as a step in a result pipeline.

Methods

AllOrNone<T1, T2>(Maybe<T1>, Maybe<T2>, string, string)

Validates that all values are present or all values are absent.

public static Result<Unit> AllOrNone<T1, T2>(Maybe<T1> first, Maybe<T2> second, string firstFieldName, string secondFieldName) where T1 : notnull where T2 : notnull

Parameters

first Maybe<T1>

The first optional value.

second Maybe<T2>

The second optional value.

firstFieldName string

Field name for the first value (used in validation errors).

secondFieldName string

Field name for the second value (used in validation errors).

Returns

Result<Unit>

Result<TValue> with Unit success if all values are present or all are absent; otherwise a Error.UnprocessableContent listing the fields that violate the invariant.

Type Parameters

T1

Type of the first optional value.

T2

Type of the second optional value.

AllOrNone<T1, T2, T3>(Maybe<T1>, Maybe<T2>, Maybe<T3>, string, string, string)

Validates that all three values are present or all three are absent.

public static Result<Unit> AllOrNone<T1, T2, T3>(Maybe<T1> first, Maybe<T2> second, Maybe<T3> third, string firstFieldName, string secondFieldName, string thirdFieldName) where T1 : notnull where T2 : notnull where T3 : notnull

Parameters

first Maybe<T1>

The first optional value.

second Maybe<T2>

The second optional value.

third Maybe<T3>

The third optional value.

firstFieldName string

Field name for the first value.

secondFieldName string

Field name for the second value.

thirdFieldName string

Field name for the third value.

Returns

Result<Unit>

Result<TValue> with Unit success if all values are present or all are absent; otherwise a Error.UnprocessableContent listing the fields that violate the invariant.

Type Parameters

T1

Type of the first optional value.

T2

Type of the second optional value.

T3

Type of the third optional value.

AllOrNone<T1, T2, T3, T4>(Maybe<T1>, Maybe<T2>, Maybe<T3>, Maybe<T4>, string, string, string, string)

Validates that all four values are present or all four are absent.

public static Result<Unit> AllOrNone<T1, T2, T3, T4>(Maybe<T1> first, Maybe<T2> second, Maybe<T3> third, Maybe<T4> fourth, string firstFieldName, string secondFieldName, string thirdFieldName, string fourthFieldName) where T1 : notnull where T2 : notnull where T3 : notnull where T4 : notnull

Parameters

first Maybe<T1>

The first optional value.

second Maybe<T2>

The second optional value.

third Maybe<T3>

The third optional value.

fourth Maybe<T4>

The fourth optional value.

firstFieldName string

Field name for the first value.

secondFieldName string

Field name for the second value.

thirdFieldName string

Field name for the third value.

fourthFieldName string

Field name for the fourth value.

Returns

Result<Unit>

Result<TValue> with Unit success if all values are present or all are absent; otherwise a Error.UnprocessableContent listing the fields that violate the invariant.

Type Parameters

T1

Type of the first optional value.

T2

Type of the second optional value.

T3

Type of the third optional value.

T4

Type of the fourth optional value.

AtLeastOne<T1, T2>(Maybe<T1>, Maybe<T2>, string, string)

Validates that at least one of the values is present.

public static Result<Unit> AtLeastOne<T1, T2>(Maybe<T1> first, Maybe<T2> second, string firstFieldName, string secondFieldName) where T1 : notnull where T2 : notnull

Parameters

first Maybe<T1>

The first optional value.

second Maybe<T2>

The second optional value.

firstFieldName string

Field name for the first value.

secondFieldName string

Field name for the second value.

Returns

Result<Unit>

Result<TValue> with Unit success if at least one value is present; otherwise a Error.UnprocessableContent listing all fields.

Type Parameters

T1

Type of the first optional value.

T2

Type of the second optional value.

AtLeastOne<T1, T2, T3>(Maybe<T1>, Maybe<T2>, Maybe<T3>, string, string, string)

Validates that at least one of the three values is present.

public static Result<Unit> AtLeastOne<T1, T2, T3>(Maybe<T1> first, Maybe<T2> second, Maybe<T3> third, string firstFieldName, string secondFieldName, string thirdFieldName) where T1 : notnull where T2 : notnull where T3 : notnull

Parameters

first Maybe<T1>

The first optional value.

second Maybe<T2>

The second optional value.

third Maybe<T3>

The third optional value.

firstFieldName string

Field name for the first value.

secondFieldName string

Field name for the second value.

thirdFieldName string

Field name for the third value.

Returns

Result<Unit>

Result<TValue> with Unit success if at least one value is present; otherwise a Error.UnprocessableContent listing all fields.

Type Parameters

T1

Type of the first optional value.

T2

Type of the second optional value.

T3

Type of the third optional value.

ExactlyOne<T1, T2>(Maybe<T1>, Maybe<T2>, string, string)

Validates that exactly one of the values is present.

public static Result<Unit> ExactlyOne<T1, T2>(Maybe<T1> first, Maybe<T2> second, string firstFieldName, string secondFieldName) where T1 : notnull where T2 : notnull

Parameters

first Maybe<T1>

The first optional value.

second Maybe<T2>

The second optional value.

firstFieldName string

Field name for the first value.

secondFieldName string

Field name for the second value.

Returns

Result<Unit>

Result<TValue> with Unit success if exactly one value is present; otherwise a Error.UnprocessableContent listing all fields.

Type Parameters

T1

Type of the first optional value.

T2

Type of the second optional value.

ExactlyOne<T1, T2, T3>(Maybe<T1>, Maybe<T2>, Maybe<T3>, string, string, string)

Validates that exactly one of the three values is present.

public static Result<Unit> ExactlyOne<T1, T2, T3>(Maybe<T1> first, Maybe<T2> second, Maybe<T3> third, string firstFieldName, string secondFieldName, string thirdFieldName) where T1 : notnull where T2 : notnull where T3 : notnull

Parameters

first Maybe<T1>

The first optional value.

second Maybe<T2>

The second optional value.

third Maybe<T3>

The third optional value.

firstFieldName string

Field name for the first value.

secondFieldName string

Field name for the second value.

thirdFieldName string

Field name for the third value.

Returns

Result<Unit>

Result<TValue> with Unit success if exactly one value is present; otherwise a Error.UnprocessableContent listing all fields.

Type Parameters

T1

Type of the first optional value.

T2

Type of the second optional value.

T3

Type of the third optional value.

MutuallyExclusive<T1, T2>(Maybe<T1>, Maybe<T2>, string, string)

Validates that at most one of the values is present.

public static Result<Unit> MutuallyExclusive<T1, T2>(Maybe<T1> first, Maybe<T2> second, string firstFieldName, string secondFieldName) where T1 : notnull where T2 : notnull

Parameters

first Maybe<T1>

The first optional value.

second Maybe<T2>

The second optional value.

firstFieldName string

Field name for the first value.

secondFieldName string

Field name for the second value.

Returns

Result<Unit>

Result<TValue> with Unit success if zero or one value is present; otherwise a Error.UnprocessableContent listing all present fields.

Type Parameters

T1

Type of the first optional value.

T2

Type of the second optional value.

MutuallyExclusive<T1, T2, T3>(Maybe<T1>, Maybe<T2>, Maybe<T3>, string, string, string)

Validates that at most one of the three values is present.

public static Result<Unit> MutuallyExclusive<T1, T2, T3>(Maybe<T1> first, Maybe<T2> second, Maybe<T3> third, string firstFieldName, string secondFieldName, string thirdFieldName) where T1 : notnull where T2 : notnull where T3 : notnull

Parameters

first Maybe<T1>

The first optional value.

second Maybe<T2>

The second optional value.

third Maybe<T3>

The third optional value.

firstFieldName string

Field name for the first value.

secondFieldName string

Field name for the second value.

thirdFieldName string

Field name for the third value.

Returns

Result<Unit>

Result<TValue> with Unit success if zero or one value is present; otherwise a Error.UnprocessableContent listing all present fields.

Type Parameters

T1

Type of the first optional value.

T2

Type of the second optional value.

T3

Type of the third optional value.

Requires<T1, T2>(Maybe<T1>, Maybe<T2>, string, string)

Validates that if source has a value, required must also have a value. If source has no value, the check passes regardless of required.

public static Result<Unit> Requires<T1, T2>(Maybe<T1> source, Maybe<T2> required, string sourceFieldName, string requiredFieldName) where T1 : notnull where T2 : notnull

Parameters

source Maybe<T1>

The optional value that triggers the requirement.

required Maybe<T2>

The optional value that must be present when source is present.

sourceFieldName string

Field name for the source value.

requiredFieldName string

Field name for the required value.

Returns

Result<Unit>

Result<TValue> with Unit success if source is absent or both are present; otherwise a Error.UnprocessableContent for the missing required field.

Type Parameters

T1

Type of the source optional value.

T2

Type of the required optional value.