© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
10 replies
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?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Fluent Validation
C#CC# / help
3y ago
Fluent validation
C#CC# / help
4y ago
Fluent Syntax - generic methods
C#CC# / help
2y ago
Fluent validation not working properly for me
C#CC# / help
4y ago