ResOkIf(FuncBoolean, String) Method

Lazy counterpart of OkIf(Boolean, String) where condition is evaluated only if this is Ok.

Returns back the Err if this is Err. Otherwise, returns Ok if lazyOkCondition holds; Err if it does not hold. Especially useful in fluent input validation.
C#
static Res<User> Login(string username, string passwordHash)
{
    return OkIf(!string.IsNullOrEmpty(username))    // validate username
        .OkIf(!string.IsNullOrEmpty(passwordHash))  // validate password-hash
        .OkIf(() => userRepo.ContainsKey(username)) // further validate user; assume this is an expensive call, so we prefer the lazy variant
        .Map(() => GetUser(username, password));    // finally map into actual result;
                                                    // any Err in validation steps will directly be mapped to Err, avoiding GetUser call.
}

Definition

Namespace: Orx.Fun.Result
Assembly: Orx.Fun.Result (in Orx.Fun.Result.dll) Version: 1.3.0+344c8bdd6f720ccfb2d8db7c61b76cf02be18f5f
C#
public Res OkIf(
	Func<bool> lazyOkCondition,
	string strOkCondition = ""
)

Parameters

lazyOkCondition  FuncBoolean
Condition that should hold to get an Ok, rather than Err.
strOkCondition  String  (Optional)
Name of the condition; to be appended to the error message if it does not hold. Omitting the argument will automatically be filled with the condition's expression in the caller side.

Return Value

Res

See Also