Open Timo-Weike opened 3 years ago
Tagging subscribers to this area: @ajcvickers See info in area-owners.md if you want to be subscribed.
Author: | Timo-Weike |
---|---|
Assignees: | - |
Labels: | `area-System.ComponentModel.DataAnnotations`, `untriaged` |
Milestone: | - |
Notes from triage: We agree that fixing the documentation would be useful. We are unlikely to change the exception type, since exceptions are commonly caught in validation code and so this would be a breaking change for existing applications.
Description
If you run a program like
You get an unexpected/undocumented
InvalidCastException
from theMaxLength
attribute on a property which is not supported. However the documentation for theValidator.TryValidateObject
does not state that exceptions produced by theValidate
calls of the attributes get bubbled up, nor does the documentation for theMaxLength
state that this exception will be produced if used on a property that is not of an array-like type.Also the fact that the exception does not get wrapped up in a
ValidationResult
does not help in finding the root cause of the problem if a validation attribute is not setup correctly.I would suggest wrapping any Exception coming from any validation attribute to be wrapped in a
ValidationResult
with an added propertyOriginalException
set to the caught exception.Configuration
Just run the above program in e.g. dotnetfiddle with .Net 5