Skip to content

Commit 708584e

Browse files
Filter out props for validation (#66)
* 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]>
1 parent 08a416a commit 708584e

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22

33
<PropertyGroup>
4-
<VersionPrefix>0.9.1</VersionPrefix>
4+
<VersionPrefix>0.9.2</VersionPrefix>
55
<!-- VersionSuffix used for local builds -->
66
<VersionSuffix>dev</VersionSuffix>
77
<!-- VersionSuffix to be used for CI builds -->

src/MiniValidation/MiniValidator.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,12 @@ private static async Task<bool> TryValidateImpl(
393393

394394
foreach (var property in typeProperties)
395395
{
396+
// Skip properties that don't have validation attributes if we're not recursing
397+
if (!(property.HasValidationAttributes || recurse))
398+
{
399+
continue;
400+
}
401+
396402
var propertyValue = property.GetValue(target);
397403
var propertyValueType = propertyValue?.GetType();
398404
var (properties, _) = _typeDetailsCache.Get(propertyValueType);

tests/MiniValidation.UnitTests/Recursion.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,4 +467,29 @@ public async Task Invalid_When_Polymorphic_AsyncValidatableOnlyChild_Is_Invalid(
467467
Assert.Single(errors);
468468
Assert.Equal($"{nameof(TestValidatableType.PocoChild)}.{nameof(TestAsyncValidatableChildType.TwentyOrMore)}", errors.Keys.First());
469469
}
470+
471+
[Fact]
472+
public async Task Throws_When_Validates_With_Recurse_And_Object_Has_Not_Implemented_Property()
473+
{
474+
var thingToValidate = new TestTypeWithNotImplementedProperty
475+
{
476+
PropertyToBeRequired = "test1"
477+
};
478+
479+
480+
await Assert.ThrowsAsync<Exception>(async () => await MiniValidator.TryValidateAsync(thingToValidate, recurse: true));
481+
}
482+
483+
[Fact]
484+
public async Task DoesntThrow_When_Validates_Without_Recurse_And_Object_Has_Not_Implemented_Property()
485+
{
486+
var thingToValidate = new TestTypeWithNotImplementedProperty
487+
{
488+
PropertyToBeRequired = "test1"
489+
};
490+
491+
var (isValid, _) = await MiniValidator.TryValidateAsync(thingToValidate, recurse: false);
492+
493+
Assert.True(isValid);
494+
}
470495
}

tests/MiniValidation.UnitTests/TestTypes.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,12 @@ class TestTypeForTypeDescriptor
258258

259259
[MaxLength(1)]
260260
public string? AnotherProperty { get; set; } = "Test";
261-
}
261+
}
262+
263+
class TestTypeWithNotImplementedProperty
264+
{
265+
[Required]
266+
public string? PropertyToBeRequired { get; set; }
267+
268+
public TestTypeForTypeDescriptor NotImplementedProperty => throw new Exception();
269+
}

tests/MiniValidation.UnitTests/TryValidate.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,11 @@ public async Task TryValidateAsync_Enumerable_With_ServiceProvider()
450450
[Fact]
451451
public async Task TryValidateAsync_With_Attribute_Attached_Via_TypeDescriptor()
452452
{
453-
var thingToValidate = new TestTypeForTypeDescriptor();
454-
455453
typeof(TestTypeForTypeDescriptor).AttachAttribute(
456-
nameof(TestTypeForTypeDescriptor.PropertyToBeRequired),
457-
_ => new RequiredAttribute());
454+
nameof(TestTypeForTypeDescriptor.PropertyToBeRequired),
455+
_ => new RequiredAttribute());
456+
457+
var thingToValidate = new TestTypeForTypeDescriptor();
458458

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

0 commit comments

Comments
 (0)