Skip to content
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

Correct IsCustomType extension method, add more tests to TypeExtensionsTest.cs #32

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Linq;

namespace SharpGrip.FluentValidation.AutoValidation.Shared.Extensions
Expand All @@ -7,18 +8,11 @@ public static class TypeExtensions
{
public static bool IsCustomType(this Type? type)
{
var builtInTypes = new[]
{
typeof(string),
typeof(decimal),
typeof(DateTime),
typeof(DateTimeOffset),
typeof(TimeSpan),
typeof(Guid),
typeof(Enum)
};

return type != null && type.IsClass && !type.IsEnum && !type.IsValueType && !type.IsPrimitive && !builtInTypes.Contains(type);
return type != null && type.IsClass
&& !type.IsEnum && !type.IsValueType
&& !type.IsPrimitive
&& type != typeof(string)
&& !type.GetInterfaces().Contains(typeof(IEnumerable));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that should be considered as breaking change. If someone is right now using e.g. List as Body parameter, after this change validation will stop working for this parameter.

Copy link
Author

@Tihomirov-Vasiliy Tihomirov-Vasiliy Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you're right. You can use any collection properties with custom type and add FluentValidation's validators for them, thank you for mention it in your issue
I relaxed checking by deleting the IEnumerable check

}

public static bool HasCustomAttribute<TAttribute>(this Type type) where TAttribute : Attribute
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,52 @@
using System;
using SharpGrip.FluentValidation.AutoValidation.Mvc.Attributes;
using SharpGrip.FluentValidation.AutoValidation.Mvc.Attributes;
using SharpGrip.FluentValidation.AutoValidation.Shared.Extensions;
using System;
using System.Collections.Generic;
using Xunit;

namespace SharpGrip.FluentValidation.AutoValidation.Tests.FluentValidation.AutoValidation.Shared.Extensions;

public class TypeExtensionsTest
{
[Fact]
public void Test_IsCustomType()
[Theory]
[InlineData(typeof(TestModelEnum))]
[InlineData(typeof(string))]
[InlineData(typeof(char))]
[InlineData(typeof(short))]
[InlineData(typeof(ushort))]
[InlineData(typeof(int))]
[InlineData(typeof(uint))]
[InlineData(typeof(nint))]
[InlineData(typeof(nuint))]
[InlineData(typeof(long))]
[InlineData(typeof(ulong))]
[InlineData(typeof(double))]
[InlineData(typeof(float))]
[InlineData(typeof(decimal))]
[InlineData(typeof(byte))]
[InlineData(typeof(sbyte))]
[InlineData(typeof(DateTime))]
[InlineData(typeof(DateTimeOffset))]
[InlineData(typeof(TimeSpan))]
[InlineData(typeof(Guid))]
[InlineData(typeof(DateOnly))]
[InlineData(typeof(TimeOnly))]
[InlineData(typeof(int[]))]
[InlineData(typeof(List<int>))]
[InlineData(typeof(Dictionary<int, int>))]
[InlineData(typeof(Array))]
[InlineData(null)]
public void Test_IsCustomType_Negative(Type? type)
{
Assert.False(type.IsCustomType(), $"Type {type?.Name} was considered as custom type");
}

[Theory]
[InlineData(typeof(TestModelClass))]
[InlineData(typeof(TestModelRecord))]
public void Test_IsCustomType_Positive(Type? type)
{
Assert.True(typeof(TestModelClass).IsCustomType());
Assert.True(typeof(TestModelRecord).IsCustomType());
Assert.False(typeof(TestModelEnum).IsCustomType());
Assert.False(typeof(Enum).IsCustomType());
Assert.False(typeof(string).IsCustomType());
Assert.False(typeof(char).IsCustomType());
Assert.False(typeof(short).IsCustomType());
Assert.False(typeof(ushort).IsCustomType());
Assert.False(typeof(int).IsCustomType());
Assert.False(typeof(uint).IsCustomType());
Assert.False(typeof(nint).IsCustomType());
Assert.False(typeof(nuint).IsCustomType());
Assert.False(typeof(long).IsCustomType());
Assert.False(typeof(ulong).IsCustomType());
Assert.False(typeof(double).IsCustomType());
Assert.False(typeof(float).IsCustomType());
Assert.False(typeof(decimal).IsCustomType());
Assert.False(typeof(byte).IsCustomType());
Assert.False(typeof(sbyte).IsCustomType());
Assert.False(typeof(DateTime).IsCustomType());
Assert.False(typeof(DateTimeOffset).IsCustomType());
Assert.False(typeof(TimeSpan).IsCustomType());
Assert.False(typeof(Guid).IsCustomType());
Assert.False(typeof(DateOnly).IsCustomType());
Assert.False(typeof(TimeOnly).IsCustomType());
Assert.True(type.IsCustomType(), $"Type {type?.Name} was not considered as custom type");
}

[Fact]
Expand Down