Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
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>
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<VersionPrefix>0.9.0</VersionPrefix>
<VersionPrefix>0.9.1</VersionPrefix>
<!-- VersionSuffix used for local builds -->
<VersionSuffix>dev</VersionSuffix>
<!-- VersionSuffix to be used for CI builds -->
Expand Down
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> TryValidateImpl(

foreach (var property in typeProperties)
{
var propertyValue = property.GetValue(target);
object? propertyValue = null;
try
{
propertyValue = property.GetValue(target);
}
catch (Exception) { }
var propertyValueType = propertyValue?.GetType();
Comment on lines +384 to 389

Choose a reason for hiding this comment

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

Shouldn't this fail validation?

Copy link
Owner Author

Choose a reason for hiding this comment

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

I don't think so? Certainly the bug report indicates they want the validation to continue, the issue is that for anything that has a getter that throws when in certain state, the walk will cause it to throw. I still feel like I'm missing something here. I might have to try out this scenario in MVC and step through what it does.

Choose a reason for hiding this comment

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

Should be an opt in. I think blindly swallowing exceptions is bad. Maybe instead have a way to skip a property? (attribute?)

Copy link
Owner Author

Choose a reason for hiding this comment

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

Already have a way to opt-out of recursing at the property level: https://github.com/DamianEdwards/MiniValidation/blob/main/src/MiniValidation/SkipRecursionAttribute.cs

Copy link

Choose a reason for hiding this comment

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

What about adding an ValidationResult stating that a certain value could not be retrieved?

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);
}
}