FluentValidation.AutoValidation icon indicating copy to clipboard operation
FluentValidation.AutoValidation copied to clipboard

Simplifying TypeExtensions.cs IsCustomType method

Open Tihomirov-Vasiliy opened this issue 1 year ago • 3 comments

Hi, I think the code in TypeExtensions.cs

public static bool IsCustomType(this Type? type)
{
    var builtInTypes = new[]
    {
            typeof(string),
            typeof(decimal),
            typeof(DateTime),
            typeof(DateTimeOffset),
            typeof(TimeSpan),
            typeof(Guid)
    };

    return type != null && type.IsClass && !type.IsEnum && !type.IsValueType && !type.IsPrimitive && !builtInTypes.Contains(type);
}

can be simplified by changing the method to:

public static bool IsCustomType(this Type? type)
{
    return type != null && type.IsClass
        && !type.IsEnum && !type.IsValueType
        && !type.IsPrimitive && type != typeof(string);
}

you can check it just with unit-test below (i was using NUnit):

[TestCase(typeof(DateTime))]
[TestCase(typeof(DateTimeOffset))]
[TestCase(typeof(TimeSpan))]
[TestCase(typeof(Guid))]
[TestCase(typeof(decimal))]
[TestCase(typeof(string))]
[TestCase(null)]
public void TypeIsCustom(Type? type)
{
    Assert.IsFalse(
        type != null && type.IsClass 
        && !type.IsEnum && !type.IsValueType 
        && !type.IsPrimitive && type != typeof(string));
}

image

Tihomirov-Vasiliy avatar Dec 23 '23 15:12 Tihomirov-Vasiliy

@Tihomirov-Vasiliy interesting, I just added some tests to test the IsCustomType extension method. Feel free to open a PR!

mvdgun avatar Dec 24 '23 02:12 mvdgun

@mvdgun I've just created the pull request https://github.com/SharpGrip/FluentValidation.AutoValidation/pull/32, feel free to review!

Tihomirov-Vasiliy avatar Dec 24 '23 05:12 Tihomirov-Vasiliy

Moved this issue to the v2.0 milestone

mvdgun avatar Dec 04 '24 20:12 mvdgun