C# - Difficulty Handling Nullable Reference Types with Custom Attributes in ASP.NET Core
I'm working through a tutorial and I've looked through the documentation and I'm still confused about I'm currently working on an ASP.NET Core 6.0 application where I'm trying to implement nullable reference types along with custom attributes for validation. The question arises when I apply a custom validation attribute to a property that is defined as nullable. For instance, I have the following model: ```csharp public class User { [Required] public string? UserName { get; set; } [EmailAddress] public string? Email { get; set; } } ``` When I attempt to validate an instance of this model with both properties set to null, I expect to receive validation errors for `UserName` and `Email`, but I only get an behavior for `UserName`. The `Email` property does not trigger the required validation even though it is marked with `[EmailAddress]`. I expected that since `Email` is nullable, it would still be validated for the email format. I also noticed that enabling nullable contexts didnโt seem to affect the validation results. I tried using both the `IValidatableObject` interface and the `DataAnnotationsValidator`, but I'm still not getting the desired behavior. Hereโs how Iโm invoking validation: ```csharp var user = new User { UserName = null, Email = null }; var validationResults = new List<ValidationResult>(); var context = new ValidationContext(user); bool isValid = Validator.TryValidateObject(user, context, validationResults, true); ``` Iโm getting the following output in `validationResults`: ``` UserName: The UserName field is required. ``` No behavior is reported for `Email`, which leads me to believe that the attribute isnโt being evaluated correctly for nullable properties. Any insights into why this might be happening or how to achieve the desired validation behavior would be appreciated! My development environment is Ubuntu. How would you solve this? Has anyone dealt with something similar?