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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public class AutoValidationMvcConfiguration
/// </summary>
public bool EnableQueryBindingSourceAutomaticValidation { get; set; } = true;

/// <summary>
/// Enables asynchronous automatic validation for parameters bound from the <see cref="BindingSource.Custom"/> binding source.
/// </summary>
public bool EnableCustomBindingSourceAutomaticValidation { get; set; } = false;

/// <summary>
/// Holds the overridden result factory. This property is meant for infrastructure and should not be used by application code.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
var parameterType = parameter.ParameterType;
var bindingSource = parameter.BindingInfo?.BindingSource;

if (subject != null && parameterType.IsCustomType() &&
((autoValidationMvcConfiguration.EnableBodyBindingSourceAutomaticValidation && bindingSource == BindingSource.Body) ||
if (subject != null
&& parameterType.IsCustomType()
&& ((autoValidationMvcConfiguration.EnableBodyBindingSourceAutomaticValidation && bindingSource == BindingSource.Body) ||
(autoValidationMvcConfiguration.EnableFormBindingSourceAutomaticValidation && bindingSource == BindingSource.Form) ||
(autoValidationMvcConfiguration.EnableQueryBindingSourceAutomaticValidation && bindingSource == BindingSource.Query)))
(autoValidationMvcConfiguration.EnableQueryBindingSourceAutomaticValidation && bindingSource == BindingSource.Query) ||
(autoValidationMvcConfiguration.EnableCustomBindingSourceAutomaticValidation && bindingSource == BindingSource.Custom)))
{
if (serviceProvider.GetValidator(parameterType) is IValidator validator)
{
Expand Down
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ app.MapPost("/", (SomeOtherModel someOtherModel) => $"Hello again {someOtherMode
### MVC controllers

| Property | Default value | Description |
|---------------------------------------------|--------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| DisableBuiltInModelValidation | `false` | Disables the built-in .NET model (data annotations) validation. |
| ValidationStrategy | `ValidationStrategy.All` | Configures the validation strategy. Validation strategy `ValidationStrategy.All` enables asynchronous automatic validation on all controllers inheriting from `ControllerBase`. Validation strategy `ValidationStrategy.Annotations` enables asynchronous automatic validation on controllers inheriting from `ControllerBase` decorated (class or method) with a `FluentValidationAutoValidationAttribute` attribute. |
| EnableBodyBindingSourceAutomaticValidation | `true` | Enables asynchronous automatic validation for parameters bound from the `BindingSource.Body` binding source (typically parameters decorated with the `[FormBody]` attribute). |
| EnableFormBindingSourceAutomaticValidation | `false` | Enables asynchronous automatic validation for parameters bound from the `BindingSource.Form` binding source (typically parameters decorated with the `[FormForm]` attribute). |
| EnableQueryBindingSourceAutomaticValidation | `true` | Enables asynchronous automatic validation for parameters bound from the `BindingSource.Query` binding source (typically parameters decorated with the `[FormQuery]` attribute). |
|----------------------------------------------|--------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| DisableBuiltInModelValidation | `false` | Disables the built-in .NET model (data annotations) validation. |
| ValidationStrategy | `ValidationStrategy.All` | Configures the validation strategy. Validation strategy `ValidationStrategy.All` enables asynchronous automatic validation on all controllers inheriting from `ControllerBase`. Validation strategy `ValidationStrategy.Annotations` enables asynchronous automatic validation on controllers inheriting from `ControllerBase` decorated (class or method) with a `FluentValidationAutoValidationAttribute` attribute. |
| EnableBodyBindingSourceAutomaticValidation | `true` | Enables asynchronous automatic validation for parameters bound from the `BindingSource.Body` binding source (typically parameters decorated with the `[FormBody]` attribute). |
| EnableFormBindingSourceAutomaticValidation | `false` | Enables asynchronous automatic validation for parameters bound from the `BindingSource.Form` binding source (typically parameters decorated with the `[FormForm]` attribute). |
| EnableQueryBindingSourceAutomaticValidation | `true` | Enables asynchronous automatic validation for parameters bound from the `BindingSource.Query` binding source (typically parameters decorated with the `[FormQuery]` attribute). |
| EnableCustomBindingSourceAutomaticValidation | `false` | Enables asynchronous automatic validation for parameters bound from the `BindingSource.Custom` binding source. |

```
using SharpGrip.FluentValidation.AutoValidation.Mvc.Extensions;
Expand All @@ -80,6 +81,9 @@ builder.Services.AddFluentValidationAutoValidation(configuration =>
// Enable validation for parameters bound from the `BindingSource.Query` binding source.
configuration.EnableQueryBindingSourceAutomaticValidation = true;

// Enable validation for parameters bound from 'BindingSource.Custom' sources.
configuration.EnableCustomBindingSourceAutomaticValidation = true;

// Replace the default result factory with a custom implementation.
configuration.OverrideDefaultResultFactoryWith<CustomResultFactory>();
});
Expand Down