Skip to content

Commit

Permalink
Handle ByRef type parameters in type constructors (#45)
Browse files Browse the repository at this point in the history
Fixes #44
  • Loading branch information
DamianEdwards committed Mar 14, 2023
1 parent 96ba2b2 commit 6edc701
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 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.7.2</VersionPrefix>
<VersionPrefix>0.7.3</VersionPrefix>
<!-- VersionSuffix used for local builds -->
<VersionSuffix>dev</VersionSuffix>
<!-- VersionSuffix to be used for CI builds -->
Expand Down
2 changes: 1 addition & 1 deletion src/MiniValidation/TypeDetailsCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private void Visit(Type type, HashSet<Type> visited, ref bool requiresAsync)
if (ctor.DeclaringType != type) continue;

// Parameters to Deconstruct are 'byref' so need to call MakeByRefType()
var deconstructParams = ctor.GetParameters().Select(p => p.ParameterType.MakeByRefType()).ToArray();
var deconstructParams = ctor.GetParameters().Select(p => p.ParameterType.IsByRef ? p.ParameterType : p.ParameterType.MakeByRefType()).ToArray();
if (type.GetMethod("Deconstruct", deconstructParams) is { } deconstruct && deconstruct.DeclaringType == type)
{
primaryCtorParams = ctor.GetParameters();
Expand Down
6 changes: 6 additions & 0 deletions tests/MiniValidation.UnitTests/TestTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,9 @@ public TestRecordType(string anotherParam, bool doTheThing) : this("Another name
}
};
#endif

class ClassWithUri
{
[Required]
public Uri? BaseAddress { get; set; }
}
23 changes: 23 additions & 0 deletions tests/MiniValidation.UnitTests/TryValidate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,27 @@ public void Throws_ArgumentException_When_TryValidate_Called_On_Target_Requiring
var isValid = MiniValidator.TryValidate(thingToValidate, out var errors);
});
}

[Fact]
public void Valid_When_Target_Has_Required_Uri_Property()
{
var thingToValidate = new ClassWithUri { BaseAddress = new("https://example.com") };

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

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

[Fact]
public void Inalid_When_Target_Has_Required_Uri_Property_With_Null_Value()
{
var thingToValidate = new ClassWithUri();

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

Assert.False(result);
Assert.Equal(1, errors.Count);
Assert.Equal(nameof(ClassWithUri.BaseAddress), errors.Keys.First());
}
}

0 comments on commit 6edc701

Please sign in to comment.