FluentValidation - Transform optionals to underlying type?

Viewed 118

Say I have this midel

class Model
{
    public Optional<string> Str { get; set; }
}

Is it possible to map the optional field so that rules are ignored when the optional is empty and allied otherwise? E.g. when HasValue is true - apply all the chained validation, when HasValue is false - ignore all the chained validation.

class MyValidator : AbstractValidator<Model>
{
    MyValidator()
    {
        RuleForEx(x => x.Opt) // see below
            .NotNull(); // apply string rules, not Optional<string> rules
    }

    // Is it possible to implement something like this?
    private IRuleBuilderInitial<Model, string> RuleForEx(Expression<Func<T, Optional<string>>> expression)
    {
        return Transform(expression, x => x.HasValue : x.Value ? `[no idea what is here]`);
    }
}
0 Answers
Related