Closed Tiberriver256 closed 7 months ago
StringLengthAttribute
is explicit about the type. It expects String, array, or collection type. TypeConverter
doesn't help, the attribute doesn't use it for type conversion.
'System.InvalidCastException' in System.ComponentModel.Annotations.dll: 'The field of type DotNext.Optional`1[System.String] must be a string, array or ICollection type.'
I see no way in .NET library to override this behavior.
The only way is to derive StringLengthAttribute
class and override bool IsValid (object? value);
method. Let's call this OptionalStringLengthAttribute
. If this approach works for you, I can prepare a few validation classes especially for Optional<T>
data type.
Ah thanks man! Yeah that sounds like a good way forward.
Please review the commit above. OptionalStringLengthAttribute
and ValueRequiredAttribute
classes are introduced. Counterparts of MinLengthAttribute
and MaxLengthAttribute
were not introduced because it makes no sense to use Optional<List<T>>
data types in data models.
Looks awesome man! Thank you!
Hello,
Is there any easy way to support making a class like this?
and then expecting it to work with normal validation?