-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Fix ValidatableTypeInfo to skip IValidatableObject validation when property validation fails #62623
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
Changes from 4 commits
a3b84bb
57b678f
7a655f4
9708b81
0934867
3e8c7fb
ae00450
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -586,6 +586,64 @@ public async Task Validate_IValidatableObject_WithZeroAndMultipleMemberNames_Beh | |
}); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot add a test like the following.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the test demonstrating the expected behavior using System.ComponentModel.DataAnnotations.Validator. The test verifies that IValidatableObject.Validate is not called when property validation fails, which matches the standard .NET validation behavior. (0934867) |
||
[Fact] | ||
public async Task Validate_IValidatableObject_ShouldNotRunWhenPropertyValidationFails() | ||
{ | ||
// Arrange | ||
var testObjectType = new TestValidatableTypeInfo( | ||
typeof(TestObjectWithValidation), | ||
[ | ||
CreatePropertyInfo(typeof(TestObjectWithValidation), typeof(string), "Name", "Name", | ||
[new RequiredAttribute()]), | ||
CreatePropertyInfo(typeof(TestObjectWithValidation), typeof(int), "Age", "Age", | ||
[new RangeAttribute(1, 100)]) | ||
]); | ||
|
||
var testObject = new TestObjectWithValidation | ||
{ | ||
Name = "", // Invalid - required field is empty | ||
Age = 25 // Valid | ||
}; | ||
|
||
var context = new ValidateContext | ||
{ | ||
ValidationOptions = new TestValidationOptions(new Dictionary<Type, ValidatableTypeInfo> | ||
{ | ||
{ typeof(TestObjectWithValidation), testObjectType } | ||
}), | ||
ValidationContext = new ValidationContext(testObject) | ||
}; | ||
|
||
// Act | ||
await testObjectType.ValidateAsync(testObject, context, default); | ||
|
||
// Assert | ||
Assert.NotNull(context.ValidationErrors); | ||
Assert.Contains("Name", context.ValidationErrors.Keys); | ||
|
||
// The key assertion: IValidatableObject should NOT be called when property validation fails | ||
Assert.False(testObject.IValidatableObjectWasCalled, "IValidatableObject.Validate should not be called when property validation fails"); | ||
} | ||
|
||
// Test class to demonstrate the expected behavior | ||
private class TestObjectWithValidation : IValidatableObject | ||
{ | ||
public string Name { get; set; } = string.Empty; | ||
public int Age { get; set; } | ||
public bool IValidatableObjectWasCalled { get; set; } | ||
|
||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) | ||
{ | ||
IValidatableObjectWasCalled = true; | ||
|
||
// This should NOT be called if there are property validation errors | ||
if (Age < 18) | ||
{ | ||
yield return new ValidationResult("Age must be at least 18", new[] { nameof(Age) }); | ||
} | ||
} | ||
} | ||
|
||
// Returns no member names to validate https://github.com/dotnet/aspnetcore/issues/61739 | ||
private class GlobalErrorObject : IValidatableObject | ||
{ | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot
Why are you ignoring the result here. The way the code is structured makes it seem as if you are running the object validation independent of whether the property validation failed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was ignoring the result to maintain the current behavior where complex object validation always runs regardless of property validation results. Complex object validation automatically handles null values and non-null objects should still be validated even if they fail property-level validation. I've clarified this intention with an explicit discard and comment explaining the rationale. (3e8c7fb)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree
If any of the properties fails validation that itself might cause additional validation errors in the object independent of whether or not the property is null.
Compare your implementation to TryValidateObject in System.ComponentModel, the behavior should match
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're absolutely right. I've updated the implementation to match System.ComponentModel.DataAnnotations.Validator behavior. Now complex object validation only runs if property validation succeeds, aligning with the standard .NET validation pattern where any property validation failure stops further validation. (e87c7fb)