Skip to content

Commit f7eb94d

Browse files
author
Chris Young
committed
Updated fluent validation
1 parent b8ec60a commit f7eb94d

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/ArchitectNow.Web/Configuration/WebApiExtensions.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.IO;
2+
using ArchitectNow.Models.Options;
23
using ArchitectNow.Web.Filters;
4+
using ArchitectNow.Web.Models;
35
using FluentValidation.AspNetCore;
46
using Microsoft.AspNetCore.Authorization;
57
using Microsoft.AspNetCore.Builder;
@@ -14,15 +16,15 @@ namespace ArchitectNow.Web.Configuration
1416
{
1517
public static class WebApiExtensions
1618
{
17-
public static void ConfigureApi(this IServiceCollection services)
19+
public static void ConfigureApi(this IServiceCollection services, FluentValidationOptions fluentValidationOptions)
1820
{
1921
/*************************
2022
* IConfiguration is not available yet
2123
*************************/
2224

2325
services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN");
2426
services.AddRouting(options => options.LowercaseUrls = true);
25-
services.AddMvc(o =>
27+
var mvcBuilder = services.AddMvc(o =>
2628
{
2729
o.Filters.AddService(typeof(GlobalExceptionFilter));
2830
o.ModelValidatorProviders.Clear();
@@ -45,7 +47,13 @@ public static void ConfigureApi(this IServiceCollection services)
4547
new IsoDateTimeConverter(),
4648
new StringEnumConverter(true)
4749
};
48-
}).AddFluentValidation();
50+
});
51+
52+
53+
if (fluentValidationOptions.Enabled == true)
54+
{
55+
mvcBuilder.AddFluentValidation(configuration => fluentValidationOptions?.Configure(configuration));
56+
}
4957
}
5058

5159
public static void ConfigureAssets(this IApplicationBuilder app, IConfigurationRoot configurationRoot)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using FluentValidation.AspNetCore;
3+
4+
namespace ArchitectNow.Web.Models
5+
{
6+
public class FluentValidationOptions
7+
{
8+
public Action<FluentValidationMvcConfiguration> Configure;
9+
public bool Enabled { get; set; }
10+
}
11+
}

src/ArchitectNow.Web/StartupBase.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Reflection;
44
using ArchitectNow.Models.Options;
55
using ArchitectNow.Web.Configuration;
6+
using ArchitectNow.Web.Models;
67
using Autofac;
78
using Autofac.Extensions.DependencyInjection;
89
using Hangfire;
@@ -15,7 +16,6 @@
1516
using Serilog;
1617
using SwaggerOptions = ArchitectNow.Web.Models.SwaggerOptions;
1718
using AutoMapper;
18-
using NSwag.AspNetCore;
1919

2020
namespace ArchitectNow.Web
2121
{
@@ -34,6 +34,9 @@ protected StartupBase(IHostingEnvironment env, ILoggerFactory loggerFactory)
3434

3535
protected abstract IEnumerable<SwaggerOptions> SwaggerOptions { get; }
3636

37+
protected virtual FluentValidationOptions FluentValidationOptions { get; } =
38+
new FluentValidationOptions {Enabled = true};
39+
3740
protected IConfigurationRoot ConfigurationRoot => _configurationRoot;
3841
protected IContainer ApplicationContainer { get; private set; }
3942

@@ -49,7 +52,7 @@ protected virtual IServiceProvider ConfigureServicesInternal(IServiceCollection
4952

5053
services.ConfigureOptions();
5154

52-
services.ConfigureApi();
55+
services.ConfigureApi(FluentValidationOptions);
5356

5457
services.ConfigureAutomapper(ConfigureAutoMapper);
5558

0 commit comments

Comments
 (0)