From c2a3fdf76fb856cd658d18c1168b5196fd6ac1fe Mon Sep 17 00:00:00 2001 From: Jeremy Skinner <90130+JeremySkinner@users.noreply.github.com> Date: Thu, 30 Jun 2022 07:41:08 +0100 Subject: [PATCH] Add missing registration for IValidatorFactory in AddFluentValidationClientsideAdapters. --- src/Directory.Build.props | 2 +- .../FluentValidationMvcExtensions.cs | 4 ++++ .../ClientsideMessageTester.cs | 21 +++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 3aa4d8a..499562e 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -1,6 +1,6 @@ - 11.1.1 + 11.1.2 diff --git a/src/FluentValidation.AspNetCore/FluentValidationMvcExtensions.cs b/src/FluentValidation.AspNetCore/FluentValidationMvcExtensions.cs index 1b415e8..717e96a 100644 --- a/src/FluentValidation.AspNetCore/FluentValidationMvcExtensions.cs +++ b/src/FluentValidation.AspNetCore/FluentValidationMvcExtensions.cs @@ -148,6 +148,10 @@ public static IServiceCollection AddFluentValidationClientsideAdapters(this ISer services.AddHttpContextAccessor(); services.TryAddSingleton(ValidatorOptions.Global); +#pragma warning disable CS0618 + services.TryAddScoped(); +#pragma warning restore CS0618 + services.TryAddEnumerable(ServiceDescriptor.Singleton, FluentValidationViewOptionsSetup>(s => { return new FluentValidationViewOptionsSetup(configuration, s.GetService()); })); diff --git a/src/FluentValidation.Tests.AspNetCore/ClientsideMessageTester.cs b/src/FluentValidation.Tests.AspNetCore/ClientsideMessageTester.cs index f325f8c..e99fb25 100644 --- a/src/FluentValidation.Tests.AspNetCore/ClientsideMessageTester.cs +++ b/src/FluentValidation.Tests.AspNetCore/ClientsideMessageTester.cs @@ -20,6 +20,7 @@ namespace FluentValidation.Tests; +using System; using System.Linq; using System.Net.Http; using System.Threading.Tasks; @@ -30,9 +31,12 @@ namespace FluentValidation.Tests; using Xunit; public class ClientsideMessageTester : IClassFixture { + 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(); @@ -45,6 +49,23 @@ public ClientsideMessageTester(WebAppFixture webApp) { 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(); + services.AddSingleton(); + services.AddScoped(); + 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");