-
Notifications
You must be signed in to change notification settings - Fork 5k
DisallowAllDefaultValues behaves unexpectedly for null #83797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Tagging subscribers to this area: @jeffhandley Issue Details
We're introducing extensions to the built-in validation attributes in System.ComponentModel.DataAnnotations:
|
Author: | Varorbc |
---|---|
Assignees: | - |
Labels: |
|
Milestone: | - |
Hi @Varorbc, to make sure your issue is understood, can you confirm that the scenario you're concerned about is the following? public class Entity_DisallowDefault
{
[Required(DisallowAllDefaultValues = true)]
public Guid? EntityId { get; set; }
}
[Fact]
public void DisallowDefault_Null()
{
Entity_DisallowDefault entity = new();
List<ValidationResult> results = new();
ValidationContext context = new(entity);
bool isValid = Validator.TryValidateObject(entity, context, results);
Assert.False(isValid);
}
[Fact]
public void DisallowDefault_Empty()
{
Entity_DisallowDefault entity = new() { EntityId = Guid.Empty };
List<ValidationResult> results = new();
ValidationContext context = new(entity);
bool isValid = Validator.TryValidateObject(entity, context, results);
Assert.True(isValid); // YOU ARE EXPECTING THIS TO BE INVALID, BUT IT IS VALID
} |
@jeffhandley Yes, I expect this to be invalid |
I can sort of see both sides of this. Compare it to strings with Of course nullable structs are legitimate types unlikes then difference between If this is being used in MVC, a On the flip side, keeping the property as nullable could have some value depending on your use case. For example, TLDR: I think the attribute is doing the "right"--or least surprising--thing for the situation. Perhaps the current binary nature of the property needs to be changed to an enum with various knobs? Otherwise a custom validation attribute could help |
I can see why you might expect this behaviour, but I think it's important to consider what As @pinkfloydx33 already points out, the |
The case you mentioned is for non-nullable property, and I think there is no problem. But if it is an nullable property and this is being used in MVC,if I send
Because if I mark it as a non-nullable property, it won't work very well if I send |
I'm going to reopen this. I think we should reconsider this design given the immediate feedback we've received (now from multiple people). |
I'm conflicted about this. If we did change the behaviour to include
cc @geeknoid |
Another approach on this could be to remove this functionality from |
Uh oh!
There was an error while loading. Please reload this page.
If the property is null, it does not work well. Because the default value of the nullable property is null, and the
RequiredAttribute
itself is validation null,DisallowAllDefaultValues
will meaningless. I expect validation to be successful when the value is Guid.Empty.Originally posted by @eiriktsarpalis in dotnet/core#8134 (comment)
The text was updated successfully, but these errors were encountered: