Skip to content

Commit 7c68527

Browse files
YanaVdovinaYana Vdovina
and
Yana Vdovina
authored
Add .Net 6.0 support and fix issue with PropertyName for the whole entity (#4)
* Added .net6.0 support * Fix issue with PropertyName in UseHttpCode Co-authored-by: Yana Vdovina <[email protected]>
1 parent 75ee7a0 commit 7c68527

10 files changed

+36
-12
lines changed

.github/workflows/build.yml

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ jobs:
1919
uses: actions/setup-dotnet@v1
2020
with:
2121
dotnet-version: 5.0.x
22+
- name: Setup .NET 6.0
23+
uses: actions/setup-dotnet@v1
24+
with:
25+
dotnet-version: 6.0.x
2226
- name: Restore dependencies
2327
run: dotnet restore
2428
- name: Build

.github/workflows/publish.yml

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ jobs:
2323
uses: actions/setup-dotnet@v1
2424
with:
2525
dotnet-version: 5.0.x
26+
- name: Setup .NET 6.0
27+
uses: actions/setup-dotnet@v1
28+
with:
29+
dotnet-version: 6.0.x
2630
- name: Pack
2731
run: |
2832
cd src

src/FluentValidation.HttpExtensions.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
4+
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
55
<LangVersion>9</LangVersion>
66
<AssemblyName>Pankraty.FluentValidation.HttpExtensions</AssemblyName>
77
<Title>Pankraty.FluentValidation.HttpExtensions</Title>

src/IRuleBuilderExtensions.cs

+12-5
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,23 @@ public static IRuleBuilderOptions<T, TProperty> AsLocked<T, TProperty>(
3434
ruleBuilder.UseHttpCode(HttpStatusCode.Locked);
3535

3636
private static string BuildPropertyName(HttpStatusCode httpStatusCode) =>
37-
$"{ErrorStatusConst.Prefix}{(int) httpStatusCode}";
37+
$"{ErrorStatusConst.Prefix}{(int)httpStatusCode}";
3838

3939
private static IRuleBuilderOptions<T, TProperty> UseHttpCode<T, TProperty>(
4040
this IRuleBuilderOptions<T, TProperty> ruleBuilder, HttpStatusCode httpStatusCode)
4141
{
4242
string propertyName = "";
43-
return ruleBuilder
44-
.Configure(x => propertyName = x.GetDisplayName(null))
45-
.WithName(propertyName)
46-
.OverridePropertyName(BuildPropertyName(httpStatusCode));
43+
44+
var options = ruleBuilder.Configure(x => propertyName = x.GetDisplayName(null));
45+
46+
if (!string.IsNullOrEmpty(propertyName))
47+
{
48+
options.WithName(propertyName);
49+
}
50+
51+
options.OverridePropertyName(BuildPropertyName(httpStatusCode));
52+
53+
return options;
4754
}
4855
}
4956
}

tests/FluentValidation.HttpExtensions.Unit/FluentValidation.HttpExtensions.Unit.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
4+
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
55
<LangVersion>9</LangVersion>
66
</PropertyGroup>
77

tests/FluentValidation.HttpExtensions.Unit/NotFoundControllerShould.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,32 @@ public async Task Use_Multiple_Messages()
3636
{
3737
"Could not find Name James Bond",
3838
"Could not find Address London, Great Britain",
39+
"Could not find entity"
3940
});
4041
}
4142

4243
[Fact]
4344
public async Task Use_Single_Message_When_Another_Rule_Passed()
4445
{
45-
_testEntity.Name = "Existing";
46-
_testEntity.Address = "London, Great Britain";
46+
_testEntity.Name = "James Bond";
47+
_testEntity.PostalCode = "Existing";
48+
_testEntity.Address = "Existing";
4749

4850
var response = await Post();
4951
var problems = await response.DeserializeAs<ValidationProblemDetails>();
5052

5153
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
5254
problems.Errors.Should()
5355
.OnlyContain(x => x.Key == "" &&
54-
x.Value.Single() == "Could not find Address London, Great Britain");
56+
x.Value.Single() == "Could not find Name James Bond");
5557
}
5658

5759
[Fact]
5860
public async Task Return_200_When_All_Rules_Passed()
5961
{
6062
_testEntity.Name = "Existing";
6163
_testEntity.Address = "Existing";
64+
_testEntity.PostalCode = "Existing";
6265

6366
var response = await Post();
6467

tests/TestApplication/TestApplication.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
4+
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
55
<LangVersion>9</LangVersion>
66
</PropertyGroup>
77

tests/TestInfrastructure/NotFoundValidator.cs

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public NotFoundValidator()
1616
.Must(x => x == "Existing")
1717
.AsNotFound()
1818
.WithMessage("Could not find {PropertyName} {PropertyValue}");
19+
20+
RuleFor(x => x)
21+
.Must(x => x.Address == "Existing" && x.PostalCode == "Existing")
22+
.AsNotFound()
23+
.WithMessage("Could not find entity");
1924
}
2025
}
2126
}

tests/TestInfrastructure/TestEntity.cs

+1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ public class NotFoundEntity
1616
{
1717
public string Name { get; set; }
1818
public string Address { get; set; }
19+
public string PostalCode { get; set; }
1920
}
2021
}

tests/TestInfrastructure/TestInfrastructure.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
4+
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
55
<LangVersion>9</LangVersion>
66
<RootNamespace>FluentValidation.HttpExtensions.TestInfrastructure</RootNamespace>
77
</PropertyGroup>

0 commit comments

Comments
 (0)