C#C
C#3y ago
NeRooN

❔ Fluent validation custom method

Hello!

I am trying to do a generic method to validate all the mails in any DTO i have.

Since i have multiple DTO's containing email strings and the rules for all should be always the same, i wanted to do a generic method to keep it simplier to mantain in the long term.

I've read the official documentation and i was trying to follow it:

public static IRuleBuilderOptions<T, string?> ValidEmail<T, TElement>(this IRuleBuilder<T, string?> ruleBuilder)
{
}


the thing here is i have to check the email domains so they belong to specific domains and i'm achieving this with a .Must(....) and .EmailAddress().

The problem i'm facing is that i only need it to validate the must and the email address only when it's NOT null or empty, so it's optional. But unlike in the Must method, if i do (x =>...) X is from type T instead of string? so how can i do this?
Was this page helpful?