Skip to content

Commit

Permalink
Only run rules for property being validated
Browse files Browse the repository at this point in the history
  • Loading branch information
matthetherington-pa committed Dec 6, 2023
1 parent c2efc1c commit a76ede9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public static void AddFluentValidation(this EditContext editContext, IServicePro
bool disableAssemblyScanning, IValidator? validator, FluentValidationValidator fluentValidationValidator)
{
ArgumentNullException.ThrowIfNull(editContext, nameof(editContext));

ValidatorOptions.Global.ValidatorSelectors.CompositeValidatorSelectorFactory =
(selectors) => new IntersectingCompositeValidatorSelector(selectors);

var messages = new ValidationMessageStore(editContext);

Expand Down Expand Up @@ -89,17 +92,26 @@ private static async Task ValidateField(EditContext editContext,

if (fluentValidationValidator.ValidateOptions is not null)
{
context = ValidationContext<object>.CreateWithOptions(editContext.Model,
(options) => { fluentValidationValidator.ValidateOptions(options); });
context = ValidationContext<object>.CreateWithOptions(editContext.Model, (options) =>
{
fluentValidationValidator.ValidateOptions(options);
options.IncludeProperties(propertyPath);
});
}
else if (fluentValidationValidator.Options is not null)
{
context = ValidationContext<object>.CreateWithOptions(editContext.Model,
(options) => { fluentValidationValidator.Options(options); });
context = ValidationContext<object>.CreateWithOptions(editContext.Model, (options) =>
{
fluentValidationValidator.Options(options);
options.IncludeProperties(propertyPath);
});
}
else
{
context = new ValidationContext<object>(editContext.Model);
context = ValidationContext<object>.CreateWithOptions(editContext.Model, (options) =>
{
options.IncludeProperties(propertyPath);
});
}

validator ??= GetValidatorForModel(serviceProvider, editContext.Model, disableAssemblyScanning);
Expand All @@ -108,7 +120,6 @@ private static async Task ValidateField(EditContext editContext,
{
var validationResults = await validator.ValidateAsync(context);
var errorMessages = validationResults.Errors
.Where(validationFailure => validationFailure.PropertyName == propertyPath)
.Select(validationFailure => validationFailure.ErrorMessage)
.Distinct();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using FluentValidation;
using FluentValidation.Internal;

namespace Blazored.FluentValidation;

internal class IntersectingCompositeValidatorSelector : IValidatorSelector {
private readonly IEnumerable<IValidatorSelector> _selectors;

public IntersectingCompositeValidatorSelector(IEnumerable<IValidatorSelector> selectors) {
_selectors = selectors;
}

public bool CanExecute(IValidationRule rule, string propertyPath, IValidationContext context) {
return _selectors.All(s => s.CanExecute(rule, propertyPath, context));
}
}

0 comments on commit a76ede9

Please sign in to comment.