-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Support ICollection with [MaxLength] and [MinLength] data annotations #2650
Conversation
This change adds support for the use of the [MaxLength] and [MaxLength] attributes on properties whose type implements ICollection so that they can be used on more properties than just strings and arrays, such as List<T> and ICollection<T>.
Hi @martincostello, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution! TTYL, DNFBOT; |
@martincostello, Thanks for signing the contribution license agreement so quickly! Actual humans will now validate the agreement and then evaluate the PR. |
Jenkins build failure looks like an unrelated issue from a failing test in
|
Looks like #2655 is tracking that failure. |
@terrajobst, this adds new functionality via allowing attributes in places they weren't previously supported. Is this something to be reviewed in an upcoming design meeting? |
@weshaggard @KrzysztofCwalina I don't think we need to. This looks like a feature of data annotations. No public API changes, not did AttributeTargets change. Seems @krwq should take a look. |
cc @lajones as he is the owner for DataAnnotations |
@rowanmiller / @divega - what do you think - this has been discussed in the past but we rejected it because we didn't think it was the time to change the way these annotations work. Has the time come? |
The code changes look good and I agree with @terrajobst that this enables new nice behavior without API changes so from that perspective I would be happy with us taking the PR. I remember one of the concerns being that any changes to DataAnnotations would light up only on platforms in which you can actually get a newer version of DataAnnotations. E.g. desktop .NET is a separate codebase and won't automatically get this and AFAIR UWP would be a different train as well. |
I suspect what I said in my last paragraph about functionality getting out of sync across platforms applies to most things in this repo. I am not sure if you have general guidelines on how to deal with that, but if you guys are fine with it happening in this case and if @rowanmiller gives the ok, I would like you to merge it. |
I'm fine with it. Mostly because it's currently a negative case on all platforms, so it's not a subtle change in behavior... it straight up doesn't work anywhere and will light up on some platforms with this change. |
Sounds good. @weshaggard, however we track such things, we should track this to ensure the behavior gets ported to other platforms. |
Support ICollection with [MaxLength] and [MinLength] data annotations
Thanks for merging this change. |
@stephentoub We don't have a great way to track such things. The owner in this @lajones should track it accordingly for each of the platforms we want to bring this behavior to. |
Support ICollection with [MaxLength] and [MinLength] data annotations Commit migrated from dotnet/corefx@7f61495
This PR adds support for the use of the
[MaxLength]
and[MaxLength]
data annotation attributes on properties whose type implementsICollection
so that they can be used on more properties than just those which are strings and arrays, such asList<T>
andICollection<T>
.This would then allow models such as the one below to use the attributes successfully without an
InvalidCastException
being thrown:If the value is not of type
string
orICollection
then the existing code is executed so that the exception behavior from beforeICollection
support was added is retained.