Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
build:
strategy:
matrix:
os: ['windows-2019', 'ubuntu-20.04']
os: ['windows-latest', 'ubuntu-latest']
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
Expand All @@ -24,6 +24,7 @@ jobs:
uses: actions/setup-dotnet@v2
with:
dotnet-version: |
9.0.x
7.0.100
6.0.x

Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "7.0.100",
"version": "9.0.100",
"rollForward": "latestFeature"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,13 @@ public virtual IEnumerable<ModelValidationResult> Validate(ModelValidationContex

IValidationContext context = new ValidationContext<object>(mvContext.Model, new PropertyChain(), selector);
context.RootContextData["InvokedByMvc"] = true;
#pragma warning disable CS0618
context.SetServiceProvider(mvContext.ActionContext.HttpContext.RequestServices);
#pragma warning restore CS0618

// For backwards compatibility, store the service provider in the validation context.
// This approach works with both FluentValidation.DependencyInjectionExtensions 11.x
// and FluentValidation.DependencyInjectionExtensions 12.x.
// Do not use context.SetServiceProvider extension method as this no longer
// exists in 12.x.
context.RootContextData["_FV_ServiceProvider"] = mvContext.ActionContext.HttpContext.RequestServices;

if (interceptor != null) {
// Allow the user to provide a customized context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace FluentValidation.Tests;

#pragma warning disable CS0618

#if !NET9_0
public class DependencyInjectionTests : IClassFixture<WebAppFixture> {
private readonly ITestOutputHelper _output;
private readonly HttpClient _client;
Expand Down Expand Up @@ -51,3 +52,4 @@ public async Task Resolves_explicit_child_validator_for_collection() {
result.GetError("Children[0].Name").ShouldEqual("NotNullInjected");
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net9.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<RootNamespace>FluentValidation.Tests</RootNamespace>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
Expand All @@ -25,6 +25,13 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net9.0'">
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="9.0.0" />
<!-- For .net 9 builds also target FV 12 -->
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="12.0.0" />
<PackageReference Include="FluentValidation" Version="12.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FluentValidation.AspNetCore\FluentValidation.AspNetCore.csproj" />
</ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/FluentValidation.Tests.AspNetCore/TestModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ public class ChildModel7 {
public string Name { get; set; }
}

#if !NET9_0
public class InjectsExplicitChildValidator : AbstractValidator<ParentModel> {
public InjectsExplicitChildValidator() {
#pragma warning disable CS0618
Expand All @@ -443,6 +444,7 @@ public InjectsExplicitChildValidatorCollection() {
#pragma warning restore CS0618
}
}
#endif

public class BadAsyncModel {
public int Id { get; set; }
Expand Down
10 changes: 5 additions & 5 deletions src/FluentValidation.Tests.AspNetCore/TypeFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public async Task Finds_and_executes_validator() {
fv.RegisterValidatorsFromAssemblyContaining<TestController>();
});
});
var result = await client.GetErrors("InjectsExplicitChildValidator");
var result = await client.GetErrors("Test1");

// Validator was found and executed so field shouldn't be valid.
result.IsValidField("Child.Name").ShouldBeFalse();
result.IsValidField("Name").ShouldBeFalse();

}

Expand All @@ -54,14 +54,14 @@ public async Task Filters_types() {
var client = _webApp.CreateClientWithServices(services => {
services.AddMvc().AddNewtonsoftJson().AddFluentValidation(fv => {
fv.RegisterValidatorsFromAssemblyContaining<TestController>(scanResult => {
return scanResult.ValidatorType != typeof(InjectsExplicitChildValidator);
return scanResult.ValidatorType != typeof(TestModelValidator);
});
});
});

var result = await client.GetErrors("InjectsExplicitChildValidator");
var result = await client.GetErrors("Test1");

// Should be valid as the validator was skipped.
result.IsValidField("Child.Name").ShouldBeTrue();
result.IsValidField("Name").ShouldBeTrue();
}
}
Loading