Skip to content

Commit 7f9b190

Browse files
authored
add support for collections (#79)
1 parent 741e05c commit 7f9b190

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

FluentValidation.AutoValidation.Shared/src/Extensions/TypeExtensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Linq;
45

@@ -32,6 +33,13 @@ public static bool IsCustomType(this Type? type)
3233
return false;
3334
}
3435

36+
if (typeof(IEnumerable).IsAssignableFrom(type))
37+
{
38+
var underlyingType = type.IsArray ? type.GetElementType() : type.GetGenericArguments().LastOrDefault();
39+
40+
return underlyingType != null && underlyingType.IsCustomType();
41+
}
42+
3543
return type.IsClass || type.IsValueType;
3644
}
3745

Tests/src/FluentValidation.AutoValidation.Shared/Extensions/TypeExtensionsTest.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// ReSharper disable InconsistentNaming
22

33
using System;
4+
using System.Collections.Generic;
45
using Microsoft.AspNetCore.Mvc;
56
using SharpGrip.FluentValidation.AutoValidation.Mvc.Attributes;
67
using SharpGrip.FluentValidation.AutoValidation.Shared.Extensions;
@@ -42,6 +43,87 @@ public void Test_IsCustomType()
4243
Assert.False(typeof(Uri).IsCustomType());
4344
}
4445

46+
[Fact]
47+
public void Test_IsCustomType_Collections()
48+
{
49+
Assert.True(typeof(ICollection<TestModelClass>).IsCustomType());
50+
Assert.True(typeof(ICollection<TestModelRecord>).IsCustomType());
51+
Assert.True(typeof(ICollection<TestModelStruct>).IsCustomType());
52+
Assert.False(typeof(ICollection<TestModelEnum>).IsCustomType());
53+
Assert.False(typeof(ICollection<Enum>).IsCustomType());
54+
Assert.False(typeof(ICollection<string>).IsCustomType());
55+
56+
Assert.True(typeof(IList<TestModelClass>).IsCustomType());
57+
Assert.True(typeof(IList<TestModelRecord>).IsCustomType());
58+
Assert.True(typeof(IList<TestModelStruct>).IsCustomType());
59+
Assert.False(typeof(IList<TestModelEnum>).IsCustomType());
60+
Assert.False(typeof(IList<Enum>).IsCustomType());
61+
Assert.False(typeof(IList<string>).IsCustomType());
62+
63+
Assert.True(typeof(List<TestModelClass>).IsCustomType());
64+
Assert.True(typeof(List<TestModelRecord>).IsCustomType());
65+
Assert.True(typeof(List<TestModelStruct>).IsCustomType());
66+
Assert.False(typeof(List<TestModelEnum>).IsCustomType());
67+
Assert.False(typeof(List<Enum>).IsCustomType());
68+
Assert.False(typeof(List<string>).IsCustomType());
69+
70+
Assert.True(typeof(TestModelClass[]).IsCustomType());
71+
Assert.True(typeof(TestModelRecord[]).IsCustomType());
72+
Assert.True(typeof(TestModelStruct[]).IsCustomType());
73+
Assert.False(typeof(TestModelEnum[]).IsCustomType());
74+
Assert.False(typeof(Enum[]).IsCustomType());
75+
Assert.False(typeof(string[]).IsCustomType());
76+
77+
Assert.True(typeof(Dictionary<string, TestModelClass>).IsCustomType());
78+
Assert.True(typeof(Dictionary<string, TestModelRecord>).IsCustomType());
79+
Assert.True(typeof(Dictionary<string, TestModelStruct>).IsCustomType());
80+
Assert.False(typeof(Dictionary<string, TestModelEnum>).IsCustomType());
81+
Assert.False(typeof(Dictionary<string, Enum>).IsCustomType());
82+
Assert.False(typeof(Dictionary<string, string>).IsCustomType());
83+
84+
Assert.True(typeof(HashSet<TestModelClass>).IsCustomType());
85+
Assert.True(typeof(HashSet<TestModelRecord>).IsCustomType());
86+
Assert.True(typeof(HashSet<TestModelStruct>).IsCustomType());
87+
Assert.False(typeof(HashSet<TestModelEnum>).IsCustomType());
88+
Assert.False(typeof(HashSet<Enum>).IsCustomType());
89+
Assert.False(typeof(HashSet<string>).IsCustomType());
90+
91+
Assert.True(typeof(IEnumerable<TestModelClass>).IsCustomType());
92+
Assert.True(typeof(IEnumerable<TestModelRecord>).IsCustomType());
93+
Assert.True(typeof(IEnumerable<TestModelStruct>).IsCustomType());
94+
Assert.False(typeof(IEnumerable<TestModelEnum>).IsCustomType());
95+
Assert.False(typeof(IEnumerable<Enum>).IsCustomType());
96+
Assert.False(typeof(IEnumerable<string>).IsCustomType());
97+
98+
Assert.True(typeof(IReadOnlyList<TestModelClass>).IsCustomType());
99+
Assert.True(typeof(IReadOnlyList<TestModelRecord>).IsCustomType());
100+
Assert.True(typeof(IReadOnlyList<TestModelStruct>).IsCustomType());
101+
Assert.False(typeof(IReadOnlyList<TestModelEnum>).IsCustomType());
102+
Assert.False(typeof(IReadOnlyList<Enum>).IsCustomType());
103+
Assert.False(typeof(IReadOnlyList<string>).IsCustomType());
104+
105+
Assert.True(typeof(IReadOnlyCollection<TestModelClass>).IsCustomType());
106+
Assert.True(typeof(IReadOnlyCollection<TestModelRecord>).IsCustomType());
107+
Assert.True(typeof(IReadOnlyCollection<TestModelStruct>).IsCustomType());
108+
Assert.False(typeof(IReadOnlyCollection<TestModelEnum>).IsCustomType());
109+
Assert.False(typeof(IReadOnlyCollection<Enum>).IsCustomType());
110+
Assert.False(typeof(IReadOnlyCollection<string>).IsCustomType());
111+
112+
Assert.True(typeof(ISet<TestModelClass>).IsCustomType());
113+
Assert.True(typeof(ISet<TestModelRecord>).IsCustomType());
114+
Assert.True(typeof(ISet<TestModelStruct>).IsCustomType());
115+
Assert.False(typeof(ISet<TestModelEnum>).IsCustomType());
116+
Assert.False(typeof(ISet<Enum>).IsCustomType());
117+
Assert.False(typeof(ISet<string>).IsCustomType());
118+
119+
Assert.False(typeof(IAsyncEnumerable<TestModelClass>).IsCustomType());
120+
Assert.False(typeof(IAsyncEnumerable<TestModelRecord>).IsCustomType());
121+
Assert.False(typeof(IAsyncEnumerable<TestModelStruct>).IsCustomType());
122+
Assert.False(typeof(IAsyncEnumerable<TestModelEnum>).IsCustomType());
123+
Assert.False(typeof(IAsyncEnumerable<Enum>).IsCustomType());
124+
Assert.False(typeof(IAsyncEnumerable<string>).IsCustomType());
125+
}
126+
45127
[Fact]
46128
public void Test_HasCustomAttribute()
47129
{

0 commit comments

Comments
 (0)