1
- using System ;
1
+ using System . Collections . Generic ;
2
2
using System . Linq ;
3
- using FluentValidation ;
3
+ using System . Threading . Tasks ;
4
4
using Microsoft . AspNetCore . Mvc ;
5
+ using Microsoft . AspNetCore . Mvc . Filters ;
5
6
using Microsoft . AspNetCore . Mvc . ModelBinding ;
6
7
using Microsoft . AspNetCore . Mvc . ModelBinding . Validation ;
7
8
8
9
namespace SharpGrip . FluentValidation . AutoValidation . Mvc . Validation
9
10
{
10
11
public class FluentValidationAutoValidationValidationVisitor : ValidationVisitor
11
12
{
12
- private readonly IServiceProvider serviceProvider ;
13
+ private readonly ActionContext actionContext ;
13
14
private readonly bool disableBuiltInModelValidation ;
14
15
15
16
public FluentValidationAutoValidationValidationVisitor (
16
- IServiceProvider serviceProvider ,
17
17
ActionContext actionContext ,
18
18
IModelValidatorProvider validatorProvider ,
19
19
ValidatorCache validatorCache ,
@@ -22,27 +22,27 @@ public FluentValidationAutoValidationValidationVisitor(
22
22
bool disableBuiltInModelValidation )
23
23
: base ( actionContext , validatorProvider , validatorCache , metadataProvider , validationState )
24
24
{
25
- this . serviceProvider = serviceProvider ;
25
+ this . actionContext = actionContext ;
26
26
this . disableBuiltInModelValidation = disableBuiltInModelValidation ;
27
27
}
28
28
29
29
public override bool Validate ( ModelMetadata ? metadata , string ? key , object ? model , bool alwaysValidateAtTopLevel )
30
30
{
31
31
// If built in model validation is disabled return true for later validation in the action filter.
32
32
bool isBaseValid = disableBuiltInModelValidation || base . Validate ( metadata , key , model , alwaysValidateAtTopLevel ) ;
33
- return Validate ( isBaseValid , key , model ) ;
33
+ return ValidateAsync ( isBaseValid , key , model ) . Result ;
34
34
}
35
35
36
36
#if ! NETCOREAPP3_1
37
37
public override bool Validate ( ModelMetadata ? metadata , string ? key , object ? model , bool alwaysValidateAtTopLevel , object ? container )
38
38
{
39
39
// If built in model validation is disabled return true for later validation in the action filter.
40
40
bool isBaseValid = disableBuiltInModelValidation || base . Validate ( metadata , key , model , alwaysValidateAtTopLevel , container ) ;
41
- return Validate ( isBaseValid , key , model ) ;
41
+ return ValidateAsync ( isBaseValid , key , model ) . Result ;
42
42
}
43
43
#endif
44
44
45
- private bool Validate (
45
+ private async Task < bool > ValidateAsync (
46
46
bool isBaseValid ,
47
47
string ? key ,
48
48
object ? model )
@@ -52,21 +52,27 @@ private bool Validate(
52
52
return isBaseValid ;
53
53
}
54
54
55
- // Use FluentValidation to perform additional validation
56
- var validatorType = typeof ( IValidator < > ) . MakeGenericType ( model . GetType ( ) ) ;
57
- if ( ! ( this . serviceProvider . GetService ( validatorType ) is IValidator validator ) )
55
+ var actionExecutingContext = new ActionExecutingContext (
56
+ actionContext ,
57
+ new List < IFilterMetadata > ( ) ,
58
+ new Dictionary < string , object > ( ) ,
59
+ null ) ;
60
+
61
+ var validationResult = await FluentValidationHelper . ValidateWithFluentValidationAsync (
62
+ actionContext . HttpContext . RequestServices ,
63
+ model ,
64
+ actionExecutingContext ) ;
65
+ if ( validationResult == null )
58
66
{
59
67
return isBaseValid ;
60
68
}
61
69
62
- var validationResult = validator . Validate ( new ValidationContext < object > ( model ) ) ;
63
70
foreach ( var error in validationResult . Errors )
64
71
{
65
72
var keyName = string . IsNullOrEmpty ( key ) ? error . PropertyName : $ "{ key } .{ error . PropertyName } ";
66
-
67
- if ( ! ModelState [ keyName ] ? . Errors . Any ( e => e . ErrorMessage == error . ErrorMessage ) ?? true )
73
+ if ( ! this . ModelState [ keyName ] ? . Errors . Any ( e => e . ErrorMessage == error . ErrorMessage ) ?? true )
68
74
{
69
- ModelState . AddModelError ( keyName , error . ErrorMessage ) ;
75
+ this . ModelState . AddModelError ( keyName , error . ErrorMessage ) ;
70
76
}
71
77
}
72
78
0 commit comments