Skip to content

Commit

Permalink
Swallow exceptions when getting property values during validation
Browse files Browse the repository at this point in the history
It's impossible to know if a given property getter will throw during validation due to the state of the object, so just swallow any exception from getting the value.

Fixes #54
  • Loading branch information
DamianEdwards committed Oct 21, 2023
1 parent ce56e98 commit da9357b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
<PackageVersion Include="coverlet.collector" Version="3.2.0" />
<PackageVersion Include="BenchmarkDotNet" Version="0.13.2" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>
7 changes: 6 additions & 1 deletion src/MiniValidation/MiniValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,12 @@ private static async Task<(bool IsValid, IDictionary<string, string[]> Errors)>

foreach (var property in typeProperties)
{
var propertyValue = property.GetValue(target);
object? propertyValue = null;
try
{
propertyValue = property.GetValue(target);
}
catch (Exception) { }
var propertyValueType = propertyValue?.GetType();
var (properties, _) = _typeDetailsCache.Get(propertyValueType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks Condition=" $([MSBuild]::IsOsPlatform('Windows')) ">net471;net6.0;net7.0</TargetFrameworks>
<LangVersion>10.0</LangVersion>
<LangVersion>11.0</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand All @@ -27,6 +27,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Newtonsoft.Json" />
</ItemGroup>

</Project>
20 changes: 19 additions & 1 deletion tests/MiniValidation.UnitTests/TestTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,22 @@ class TestTypeForTypeDescriptor

[MaxLength(1)]
public string? AnotherProperty { get; set; } = "Test";
}
}

class ClassWithJTokenProperty
{
public ClassWithJTokenProperty()
{
SomeJsonToken = Newtonsoft.Json.Linq.JToken.Parse("""
{
"prop1": 123,
"array1": [1, 2, 3],
"obj1": {
"prop2": "abc"
}
}
""");
}

public Newtonsoft.Json.Linq.JToken SomeJsonToken { get; }
}
11 changes: 11 additions & 0 deletions tests/MiniValidation.UnitTests/TryValidate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,15 @@ public async Task TryValidateAsync_With_Attribute_Attached_Via_TypeDescriptor()
Assert.Single(errors["PropertyToBeRequired"]);
Assert.Single(errors["AnotherProperty"]);
}

[Fact]
public void CanValidateClassWithJTokenProperty()
{
var thingToValidate = new ClassWithJTokenProperty();

var result = MiniValidator.TryValidate(thingToValidate, out var errors);

Assert.True(result);
Assert.Equal(0, errors.Count);
}
}

0 comments on commit da9357b

Please sign in to comment.