File tree Expand file tree Collapse file tree 5 files changed +45
-6
lines changed
tests/MiniValidation.UnitTests Expand file tree Collapse file tree 5 files changed +45
-6
lines changed Original file line number Diff line number Diff line change 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 -->
Original file line number Diff line number Diff 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 ) ;
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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+ }
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments