Using [EmailAddress]
in a controller-based API is sufficient to also include email validation errors (as far as I know), however doing the same in a minimal API doesn't have that feature. A colleague suggested that this is the case because minimal APIs don't have support yet for IModelValidator.
How would you address this issue?
So far I've tried using jsteadfast/EmailValidation, but it seems to fail me. I attempted to use [Email]
on top of a string I wanted to validate, however that doesn't seem to have the effect I wanted.
I have no idea what the 'minimal API project' you mentioned means. Assuming you want to validate an email address without any dependency but only system libraries.
If you want to know if a string is an Email address, simply call:
In case you want to validate a class which has Email address as a property, which is common in API projects:
You can try to add a static validator first:
Then build a model to be validated:
Now you can validate it directly in
Main()
or any controller action code.