Skip to content
This repository was archived by the owner on Dec 11, 2020. It is now read-only.

Commit 7438728

Browse files
Updated conventions to latest (#338)
* Updated conventions to latest * fixed with latest version of fv Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 158bd1d commit 7438728

6 files changed

Lines changed: 161 additions & 58 deletions

File tree

src/AspNetCore/AspNetCoreFluentValidationExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ public static IServiceCollection AddFluentValidationExtensions(
3939
FluentValidationMvcConfiguration? validationMvcConfiguration = null
4040
)
4141
{
42-
validatorConfiguration ??= new ValidatorConfiguration();
42+
validatorConfiguration ??= ValidatorOptions.Global;
4343
validationMvcConfiguration ??= new FluentValidationMvcConfiguration(validatorConfiguration);
44-
4544
services
4645
.Configure<MvcOptions>(
4746
options =>
@@ -66,7 +65,7 @@ public static IServiceCollection AddFluentValidationExtensions(
6665
}
6766
);
6867

69-
services.AddSingleton<IValidatorInterceptor, ValidatorInterceptor>();
68+
services.AddSingleton<IActionContextValidatorInterceptor, ValidatorInterceptor>();
7069
services.AddSingleton<ProblemDetailsFactory, FluentValidationProblemDetailsFactory>();
7170
services.Configure<ApiBehaviorOptions>(
7271
o =>

src/AspNetCore/ValidatorInterceptor.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,14 @@
55

66
namespace Rocket.Surgery.AspNetCore.FluentValidation
77
{
8-
internal class ValidatorInterceptor : IValidatorInterceptor
8+
[System.Obsolete]
9+
internal class ValidatorInterceptor : IActionContextValidatorInterceptor
910
{
10-
public IValidationContext BeforeMvcValidation(
11-
ControllerContext controllerContext,
12-
IValidationContext validationContext
13-
) => validationContext;
11+
public IValidationContext BeforeMvcValidation(ActionContext actionContext, IValidationContext validationContext) => validationContext;
1412

15-
public ValidationResult AfterMvcValidation(
16-
ControllerContext controllerContext,
17-
IValidationContext validationContext,
18-
ValidationResult result
19-
)
13+
public ValidationResult AfterMvcValidation(ActionContext actionContext, IValidationContext validationContext, ValidationResult result)
2014
{
21-
controllerContext.HttpContext.Items[typeof(ValidationResult)] = result;
15+
actionContext.HttpContext.Items[typeof(ValidationResult)] = result;
2216
return result;
2317
}
2418
}

src/Conventions.AspNetCore.NewtonsoftJson/AspNetCoreFluentValidationConvention.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/Conventions.AspNetCore/AspNetCoreFluentValidationConvention.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,18 @@ namespace Rocket.Surgery.Conventions.AspNetCore.FluentValidation
2424
[PublicAPI]
2525
public class AspNetCoreFluentValidationConvention : IServiceConvention
2626
{
27-
private readonly FluentValidationMvcConfiguration _validationMvcConfiguration;
28-
private readonly ValidatorConfiguration _validatorConfiguration;
27+
private readonly ValidatorConfiguration _configuration;
28+
private readonly FluentValidationMvcConfiguration _mvcConfiguration;
2929

3030
/// <summary>
3131
/// THe validation settings
3232
/// </summary>
3333
/// <param name="validatorConfiguration"></param>
3434
/// <param name="validationMvcConfiguration"></param>
35-
public AspNetCoreFluentValidationConvention(
36-
[CanBeNull] ValidatorConfiguration? validatorConfiguration = null,
37-
[CanBeNull] FluentValidationMvcConfiguration? validationMvcConfiguration = null)
35+
public AspNetCoreFluentValidationConvention([CanBeNull] ValidatorConfiguration? validatorConfiguration = null, FluentValidationMvcConfiguration? validationMvcConfiguration = null)
3836
{
39-
_validatorConfiguration = validatorConfiguration ??= new ValidatorConfiguration();
40-
_validationMvcConfiguration = validationMvcConfiguration ?? new FluentValidationMvcConfiguration(validatorConfiguration);
37+
_configuration ??= validatorConfiguration ?? new ValidatorConfiguration();
38+
_mvcConfiguration = validationMvcConfiguration ?? new FluentValidationMvcConfiguration(_configuration);
4139
}
4240

4341
/// <summary>
@@ -52,7 +50,7 @@ public void Register([NotNull] IServiceConventionContext context)
5250
}
5351

5452
context.Services
55-
.AddFluentValidationExtensions(_validatorConfiguration, _validationMvcConfiguration);
53+
.AddFluentValidationExtensions(_configuration, _mvcConfiguration);
5654
}
5755
}
5856
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// using System;
2+
// using JetBrains.Annotations;
3+
// using Microsoft.Extensions.DependencyInjection;
4+
// using Rocket.Surgery.Extensions.FluentValidation;
5+
6+
// // ReSharper disable once CheckNamespace
7+
// namespace FluentValidation
8+
// {
9+
// /// <summary>
10+
// /// FluentValidationPolymorphicPropertyValidatorExtensions.
11+
// /// </summary>
12+
// [PublicAPI]
13+
// public static class FluentValidationPolymorphicPropertyValidatorExtensions
14+
// {
15+
// /// <summary>
16+
// /// Uses the polymorphic validator.
17+
// /// </summary>
18+
// /// <typeparam name="T"></typeparam>
19+
// /// <typeparam name="TProperty">The type of the t property.</typeparam>
20+
// /// <param name="builder">The builder.</param>
21+
// /// <param name="serviceProvider">The service provider.</param>
22+
// /// <returns>IRuleBuilderOptions{T, TProperty}.</returns>
23+
// public static IRuleBuilderOptions<T, TProperty> UsePolymorphicValidator<T, TProperty>(
24+
// this IRuleBuilder<T, TProperty> builder,
25+
// IServiceProvider serviceProvider
26+
// )
27+
// {
28+
// if (builder is null)
29+
// {
30+
// throw new ArgumentNullException(nameof(builder));
31+
// }
32+
33+
// return builder.SetValidator(
34+
// ActivatorUtilities.CreateInstance<PolymorphicPropertyValidator<TProperty>>(serviceProvider)
35+
// );
36+
// }
37+
// }
38+
// }
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// using System;
2+
// using System.Collections.Concurrent;
3+
// using System.Collections.Generic;
4+
// using System.Linq;
5+
// using System.Threading;
6+
// using System.Threading.Tasks;
7+
// using FluentValidation;
8+
// using FluentValidation.Internal;
9+
// using FluentValidation.Results;
10+
// using FluentValidation.Validators;
11+
// using JetBrains.Annotations;
12+
13+
// namespace Rocket.Surgery.Extensions.FluentValidation
14+
// {
15+
// /// <summary>
16+
// /// PolymorphicPropertyValidator.
17+
// /// Implements the <see cref="NoopPropertyValidator" />
18+
// /// </summary>
19+
// /// <typeparam name="T"></typeparam>
20+
// /// <seealso cref="NoopPropertyValidator" />
21+
// public class PolymorphicPropertyValidator<T> : NoopPropertyValidator
22+
// {
23+
// private readonly IValidatorFactory _validatorFactory;
24+
// private readonly IServiceProvider _serviceProvider;
25+
26+
// /// <summary>
27+
// /// Initializes a new instance of the <see cref="PolymorphicPropertyValidator{T}" /> class.
28+
// /// </summary>
29+
// /// <param name="validatorFactory">The validator factory.</param>
30+
// /// <param name="serviceProvider">The service provider.</param>
31+
// internal PolymorphicPropertyValidator(IValidatorFactory validatorFactory, IServiceProvider serviceProvider)
32+
// {
33+
// _validatorFactory = validatorFactory;
34+
// _serviceProvider = serviceProvider;
35+
// }
36+
37+
// /// <summary>
38+
// /// Validates the specified context.
39+
// /// </summary>
40+
// /// <param name="context">The context.</param>
41+
// /// <returns>IEnumerable{ValidationFailure}.</returns>
42+
// public override IEnumerable<ValidationFailure> Validate([NotNull] PropertyValidatorContext context)
43+
// {
44+
// if (context == null)
45+
// {
46+
// throw new ArgumentNullException(nameof(context));
47+
// }
48+
49+
// // bail out if the property is null
50+
// if (context.PropertyValue == null || !( context.PropertyValue is T value ))
51+
// {
52+
// return Enumerable.Empty<ValidationFailure>();
53+
// }
54+
55+
// var validator = _validatorFactory.GetValidator(value.GetType());
56+
// if (context.ParentContext.IsChildCollectionContext)
57+
// {
58+
// return validator.Validate(context.ParentContext.CloneForChildValidator(value)).Errors;
59+
// }
60+
61+
// var validationContext = new ValidationContext<T>(
62+
// value,
63+
// PropertyChain.FromExpression(context.Rule.Expression),
64+
// context.ParentContext.Selector
65+
// );
66+
// validationContext.SetServiceProvider(_serviceProvider);
67+
// return validator.Validate(validationContext).Errors;
68+
// }
69+
70+
// /// <summary>
71+
// /// validate as an asynchronous operation.
72+
// /// </summary>
73+
// /// <param name="context">The context.</param>
74+
// /// <param name="cancellation">The cancellation.</param>
75+
// /// <returns>Task{IEnumerable{ValidationFailure}}.</returns>
76+
// public override async Task<IEnumerable<ValidationFailure>> ValidateAsync(
77+
// [NotNull] PropertyValidatorContext context,
78+
// CancellationToken cancellation
79+
// )
80+
// {
81+
// if (context == null)
82+
// {
83+
// throw new ArgumentNullException(nameof(context));
84+
// }
85+
86+
// // bail out if the property is null
87+
// if (context.PropertyValue == null || !( context.PropertyValue is T value ))
88+
// {
89+
// return Enumerable.Empty<ValidationFailure>();
90+
// }
91+
92+
// var validator = _validatorFactory.GetValidator(value.GetType());
93+
// if (context.ParentContext.IsChildCollectionContext)
94+
// {
95+
// return ( await validator.ValidateAsync(
96+
// context.ParentContext.CloneForChildValidator(value),
97+
// cancellation
98+
// ).ConfigureAwait(false) ).Errors;
99+
// }
100+
101+
// var validationContext = new ValidationContext<T>(
102+
// value,
103+
// PropertyChain.FromExpression(context.Rule.Expression),
104+
// context.ParentContext.Selector
105+
// );
106+
// validationContext.SetServiceProvider(_serviceProvider);
107+
// return ( await validator.ValidateAsync(validationContext, cancellation).ConfigureAwait(false) ).Errors;
108+
// }
109+
// }
110+
// }

0 commit comments

Comments
 (0)