Skip to content

Commit

Permalink
add path binding source validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdgun committed Oct 6, 2023
1 parent cb69a0b commit fd4038e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,29 @@ public class AutoValidationMvcConfiguration
public ValidationStrategy ValidationStrategy { get; set; } = ValidationStrategy.All;

/// <summary>
/// Enables asynchronous automatic validation for parameters bound from the <see cref="BindingSource.Body"/> binding source (typically parameters decorated with the [FromBody] attribute).
/// Enables asynchronous automatic validation for parameters bound from <see cref="BindingSource.Body"/> binding sources (typically parameters decorated with the [FromBody] attribute).
/// </summary>
/// <see cref="FromBodyAttribute"/>
public bool EnableBodyBindingSourceAutomaticValidation { get; set; } = true;

/// <summary>
/// Enables asynchronous automatic validation for parameters bound from the <see cref="BindingSource.Form"/> binding source (typically parameters decorated with the [FromForm] attribute).
/// Enables asynchronous automatic validation for parameters bound from <see cref="BindingSource.Form"/> binding sources (typically parameters decorated with the [FromForm] attribute).
/// </summary>
/// <see cref="FromFormAttribute"/>
public bool EnableFormBindingSourceAutomaticValidation { get; set; } = false;

/// <summary>
/// Enables asynchronous automatic validation for parameters bound from the <see cref="BindingSource.Query"/> binding source (typically parameters decorated with the [FromQuery] attribute).
/// Enables asynchronous automatic validation for parameters bound from <see cref="BindingSource.Query"/> binding sources (typically parameters decorated with the [FromQuery] attribute).
/// </summary>
/// <see cref="FromQueryAttribute"/>
public bool EnableQueryBindingSourceAutomaticValidation { get; set; } = true;

/// <summary>
/// Enables asynchronous automatic validation for parameters bound from <see cref="BindingSource.Path"/> binding sources (typically parameters decorated with the [FromRoute] attribute).
/// </summary>
/// <see cref="FromRouteAttribute"/>
public bool EnablePathBindingSourceAutomaticValidation { get; set; } = false;

/// <summary>
/// Enables asynchronous automatic validation for parameters bound from <see cref="BindingSource.Custom"/> binding sources.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
((autoValidationMvcConfiguration.EnableBodyBindingSourceAutomaticValidation && bindingSource == BindingSource.Body) ||
(autoValidationMvcConfiguration.EnableFormBindingSourceAutomaticValidation && bindingSource == BindingSource.Form) ||
(autoValidationMvcConfiguration.EnableQueryBindingSourceAutomaticValidation && bindingSource == BindingSource.Query) ||
(autoValidationMvcConfiguration.EnablePathBindingSourceAutomaticValidation && bindingSource == BindingSource.Path) ||
(autoValidationMvcConfiguration.EnableCustomBindingSourceAutomaticValidation && bindingSource == BindingSource.Custom)))
{
if (serviceProvider.GetValidator(parameterType) is IValidator validator)

Check warning on line 63 in FluentValidation.AutoValidation.Mvc/src/Filters/FluentValidationAutoValidationActionFilter.cs

View workflow job for this annotation

GitHub Actions / build

Merge this if statement with the enclosing one. (https://rules.sonarsource.com/csharp/RSPEC-1066)

Check warning on line 63 in FluentValidation.AutoValidation.Mvc/src/Filters/FluentValidationAutoValidationActionFilter.cs

View workflow job for this annotation

GitHub Actions / build

Merge this if statement with the enclosing one. (https://rules.sonarsource.com/csharp/RSPEC-1066)

Check warning on line 63 in FluentValidation.AutoValidation.Mvc/src/Filters/FluentValidationAutoValidationActionFilter.cs

View workflow job for this annotation

GitHub Actions / build

Merge this if statement with the enclosing one. (https://rules.sonarsource.com/csharp/RSPEC-1066)

Check warning on line 63 in FluentValidation.AutoValidation.Mvc/src/Filters/FluentValidationAutoValidationActionFilter.cs

View workflow job for this annotation

GitHub Actions / build

Merge this if statement with the enclosing one. (https://rules.sonarsource.com/csharp/RSPEC-1066)

Check warning on line 63 in FluentValidation.AutoValidation.Mvc/src/Filters/FluentValidationAutoValidationActionFilter.cs

View workflow job for this annotation

GitHub Actions / build

Merge this if statement with the enclosing one. (https://rules.sonarsource.com/csharp/RSPEC-1066)
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ app.MapPost("/", (SomeOtherModel someOtherModel) => $"Hello again {someOtherMode
| EnableBodyBindingSourceAutomaticValidation | `true` | Enables asynchronous automatic validation for parameters bound from `BindingSource.Body` binding sources (typically parameters decorated with the `[FromBody]` attribute). |
| EnableFormBindingSourceAutomaticValidation | `false` | Enables asynchronous automatic validation for parameters bound from `BindingSource.Form` binding sources (typically parameters decorated with the `[FromForm]` attribute). |
| EnableQueryBindingSourceAutomaticValidation | `true` | Enables asynchronous automatic validation for parameters bound from `BindingSource.Query` binding sources (typically parameters decorated with the `[FromQuery]` attribute). |
| EnablePathBindingSourceAutomaticValidation | `false` | Enables asynchronous automatic validation for parameters bound from `BindingSource.Path` binding sources (typically parameters decorated with the `[FromRoute]` attribute). |
| EnableCustomBindingSourceAutomaticValidation | `false` | Enables asynchronous automatic validation for parameters bound from `BindingSource.Custom` binding sources. |

```
Expand All @@ -81,6 +82,9 @@ builder.Services.AddFluentValidationAutoValidation(configuration =>
// Enable validation for parameters bound from `BindingSource.Query` binding sources.
configuration.EnableQueryBindingSourceAutomaticValidation = true;
// Enable validation for parameters bound from `BindingSource.Path` binding sources.
configuration.EnablePathBindingSourceAutomaticValidation = true;
// Enable validation for parameters bound from 'BindingSource.Custom' binding sources.
configuration.EnableCustomBindingSourceAutomaticValidation = true;
Expand Down
12 changes: 6 additions & 6 deletions Tests/FluentValidation.AutoValidation.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="NSubstitute.Analyzers.CSharp" Version="1.0.16">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.console" Version="2.5.0">
<PackageReference Include="xunit" Version="2.5.1" />
<PackageReference Include="xunit.runner.console" Version="2.5.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.runner.msbuild" Version="2.5.0">
<PackageReference Include="xunit.runner.msbuild" Version="2.5.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit fd4038e

Please sign in to comment.