ResOkIf Method

Overload List

OkIf(Boolean, String) Returns back the Err if this is Err. Otherwise, returns Ok if okCondition 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
        .Map(() => GetUser(username, password));    // finally map into actual result;
                                                    // any Err in validation steps will directly be mapped to Err, avoiding GetUser call.
}
OkIf(FuncBoolean, String)

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.
}

See Also