Skip to content

Commit

Permalink
Filter out props for validation (#66)
Browse files Browse the repository at this point in the history
* Filter out props for validation
that don't have any validation attribute and when recurse is disabled

* Tweaks

* Update TryValidate.cs

---------

Co-authored-by: Damian Edwards <[email protected]>
  • Loading branch information
Kraviecc and DamianEdwards authored Dec 20, 2024
1 parent 08a416a commit 708584e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
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.1</VersionPrefix>
<VersionPrefix>0.9.2</VersionPrefix>
<!-- VersionSuffix used for local builds -->
<VersionSuffix>dev</VersionSuffix>
<!-- VersionSuffix to be used for CI builds -->
Expand Down
6 changes: 6 additions & 0 deletions src/MiniValidation/MiniValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,12 @@ private static async Task<bool> TryValidateImpl(

foreach (var property in typeProperties)
{
// Skip properties that don't have validation attributes if we're not recursing
if (!(property.HasValidationAttributes || recurse))
{
continue;
}

var propertyValue = property.GetValue(target);
var propertyValueType = propertyValue?.GetType();
var (properties, _) = _typeDetailsCache.Get(propertyValueType);
Expand Down
25 changes: 25 additions & 0 deletions tests/MiniValidation.UnitTests/Recursion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,4 +467,29 @@ public async Task Invalid_When_Polymorphic_AsyncValidatableOnlyChild_Is_Invalid(
Assert.Single(errors);
Assert.Equal($"{nameof(TestValidatableType.PocoChild)}.{nameof(TestAsyncValidatableChildType.TwentyOrMore)}", errors.Keys.First());
}

[Fact]
public async Task Throws_When_Validates_With_Recurse_And_Object_Has_Not_Implemented_Property()
{
var thingToValidate = new TestTypeWithNotImplementedProperty
{
PropertyToBeRequired = "test1"
};


await Assert.ThrowsAsync<Exception>(async () => await MiniValidator.TryValidateAsync(thingToValidate, recurse: true));
}

[Fact]
public async Task DoesntThrow_When_Validates_Without_Recurse_And_Object_Has_Not_Implemented_Property()
{
var thingToValidate = new TestTypeWithNotImplementedProperty
{
PropertyToBeRequired = "test1"
};

var (isValid, _) = await MiniValidator.TryValidateAsync(thingToValidate, recurse: false);

Assert.True(isValid);
}
}
10 changes: 9 additions & 1 deletion tests/MiniValidation.UnitTests/TestTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,12 @@ class TestTypeForTypeDescriptor

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

class TestTypeWithNotImplementedProperty
{
[Required]
public string? PropertyToBeRequired { get; set; }

public TestTypeForTypeDescriptor NotImplementedProperty => throw new Exception();
}
8 changes: 4 additions & 4 deletions tests/MiniValidation.UnitTests/TryValidate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,11 @@ public async Task TryValidateAsync_Enumerable_With_ServiceProvider()
[Fact]
public async Task TryValidateAsync_With_Attribute_Attached_Via_TypeDescriptor()
{
var thingToValidate = new TestTypeForTypeDescriptor();

typeof(TestTypeForTypeDescriptor).AttachAttribute(
nameof(TestTypeForTypeDescriptor.PropertyToBeRequired),
_ => new RequiredAttribute());
nameof(TestTypeForTypeDescriptor.PropertyToBeRequired),
_ => new RequiredAttribute());

var thingToValidate = new TestTypeForTypeDescriptor();

var (isValid, errors) = await MiniValidator.TryValidateAsync(thingToValidate);

Expand Down

0 comments on commit 708584e

Please sign in to comment.