Skip to content

Commit

Permalink
Merge pull request #2 from SharpGrip/1-mvc-filter-breaks-if-parameter…
Browse files Browse the repository at this point in the history
…-is-not-present-in-the-action-arguments

add check if the parameter exists in the action arguments
  • Loading branch information
mvdgun authored Aug 21, 2023
2 parents 3e71288 + def49fa commit 5322264
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<PropertyGroup>
<PackageVersion>1.0.0</PackageVersion>
<PackageVersion>1.0.1</PackageVersion>
<Company>SharpGrip</Company>
<Authors>SharpGrip</Authors>
<Copyright>SharpGrip</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,26 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE

foreach (var parameter in actionDescriptor.Parameters)
{
var subject = context.ActionArguments[parameter.Name];
var parameterType = parameter.ParameterType;
if (context.ActionArguments.TryGetValue(parameter.Name, out var subject))
{
var parameterType = parameter.ParameterType;

// ReSharper disable once ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
var bindingSource = parameter.BindingInfo?.BindingSource;
// ReSharper disable once ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
var bindingSource = parameter.BindingInfo?.BindingSource;

// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
if (subject != null && (bindingSource == BindingSource.Body || (bindingSource == BindingSource.Query && parameterType.IsClass)))
{
if (serviceProvider.GetValidator(parameterType) is IValidator validator)
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
if (subject != null && (bindingSource == BindingSource.Body || (bindingSource == BindingSource.Query && parameterType.IsClass)))
{
var validationResult = await validator.ValidateAsync(new ValidationContext<object>(subject), context.HttpContext.RequestAborted);

if (!validationResult.IsValid)
if (serviceProvider.GetValidator(parameterType) is IValidator validator)
{
foreach (var error in validationResult.Errors)
var validationResult = await validator.ValidateAsync(new ValidationContext<object>(subject), context.HttpContext.RequestAborted);

if (!validationResult.IsValid)
{
context.ModelState.AddModelError(error.PropertyName, error.ErrorMessage);
foreach (var error in validationResult.Errors)
{
context.ModelState.AddModelError(error.PropertyName, error.ErrorMessage);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ their projects.

## Installation

Register your validators with the Microsoft DI service container, for instructions on settings that up please see https://docs.fluentvalidation.net/en/latest/di.html.
Register your validators with the Microsoft DI service container, for instructions on setting that up please see https://docs.fluentvalidation.net/en/latest/di.html.

### MVC controllers [![NuGet](https://img.shields.io/nuget/v/SharpGrip.FluentValidation.AutoValidation.Mvc)](https://www.nuget.org/packages/SharpGrip.FluentValidation.AutoValidation.Mvc)

Expand Down

0 comments on commit 5322264

Please sign in to comment.