Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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