Skip to content

Commit

Permalink
Add missing registration for IValidatorFactory in AddFluentValidation…
Browse files Browse the repository at this point in the history
…ClientsideAdapters.
  • Loading branch information
JeremySkinner committed Jun 30, 2022
1 parent 4846a78 commit c2a3fdf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<VersionPrefix>11.1.1</VersionPrefix>
<VersionPrefix>11.1.2</VersionPrefix>
<VersionSuffix></VersionSuffix>
<!-- Use CI build number as version suffix (if defined) -->
<!--<VersionSuffix Condition="'$(GITHUB_RUN_NUMBER)'!=''">ci-$(GITHUB_RUN_NUMBER)</VersionSuffix>-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ public static class FluentValidationMvcExtensions {
services.AddHttpContextAccessor();
services.TryAddSingleton(ValidatorOptions.Global);

#pragma warning disable CS0618
services.TryAddScoped<IValidatorFactory, ServiceProviderValidatorFactory>();
#pragma warning restore CS0618

services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<MvcViewOptions>, FluentValidationViewOptionsSetup>(s => {
return new FluentValidationViewOptionsSetup(configuration, s.GetService<IHttpContextAccessor>());
}));
Expand Down
21 changes: 21 additions & 0 deletions src/FluentValidation.Tests.AspNetCore/ClientsideMessageTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace FluentValidation.Tests;

using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
Expand All @@ -30,9 +31,12 @@ namespace FluentValidation.Tests;
using Xunit;

public class ClientsideMessageTester : IClassFixture<WebAppFixture> {
private readonly WebAppFixture _webApp;
private readonly HttpClient _client;

public ClientsideMessageTester(WebAppFixture webApp) {
_webApp = webApp;

_client = webApp.CreateClientWithServices(services => {
#pragma warning disable CS0618
services.AddMvc().AddNewtonsoftJson().AddFluentValidation();
Expand All @@ -45,6 +49,23 @@ public class ClientsideMessageTester : IClassFixture<WebAppFixture> {
CultureScope.SetDefaultCulture();
}

[Fact]
public async Task Works_with_AddFluentValidationClientsideAdapters() {
// This version of the test uses AddFluentValidationClientsideAdapters
// rather than AddFluentValidation()
var client = _webApp.CreateClientWithServices(services => {
services.AddMvc();
services.AddFluentValidationClientsideAdapters();
services.AddValidatorsFromAssemblyContaining<TestController>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<ClientsideScopedDependency>();
services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });
});

var msg = await client.GetClientsideMessage("Required", "data-val-required");
msg.ShouldEqual("'Required' must not be empty.");
}

[Fact]
public async Task NotEmpty_uses_simplified_message_for_clientside_validation() {
var msg = await _client.GetClientsideMessage("Required", "data-val-required");
Expand Down

0 comments on commit c2a3fdf

Please sign in to comment.