Skip to content

Commit

Permalink
Add OnRuleAdded method to AbstractValidator #2114
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremySkinner committed Aug 16, 2023
1 parent d6c2ec1 commit 32befcf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
11.8.0 -
Added AbstractValidator.OnRuleAdded to allow customization of rule instances after creation (#2114)

11.7.1 - 12 Aug 2023
Resolved issue with combining multiple ValidationResult instances where RuleSetsExecuted wasn't properly set on the combined result (#2125)

Expand Down
15 changes: 15 additions & 0 deletions src/FluentValidation/AbstractValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ ValueTask<ValidationResult> completedValueTask
expression.Guard("Cannot pass null to RuleFor", nameof(expression));
var rule = PropertyRule<T, TProperty>.Create(expression, () => RuleLevelCascadeMode);
Rules.Add(rule);
OnRuleAdded(rule);
return new RuleBuilder<T, TProperty>(rule, this);
}

Expand All @@ -305,6 +306,7 @@ ValueTask<ValidationResult> completedValueTask
from.Guard("Cannot pass null to Transform", nameof(from));
var rule = PropertyRule<T, TTransformed>.Create(from, to, () => RuleLevelCascadeMode);
Rules.Add(rule);
OnRuleAdded(rule);
return new RuleBuilder<T, TTransformed>(rule, this);
}

Expand All @@ -324,6 +326,7 @@ ValueTask<ValidationResult> completedValueTask
from.Guard("Cannot pass null to Transform", nameof(from));
var rule = PropertyRule<T, TTransformed>.Create(from, to, () => RuleLevelCascadeMode);
Rules.Add(rule);
OnRuleAdded(rule);
return new RuleBuilder<T, TTransformed>(rule, this);
}

Expand All @@ -338,6 +341,7 @@ ValueTask<ValidationResult> completedValueTask
expression.Guard("Cannot pass null to RuleForEach", nameof(expression));
var rule = CollectionPropertyRule<T, TElement>.Create(expression, () => RuleLevelCascadeMode);
Rules.Add(rule);
OnRuleAdded(rule);
return new RuleBuilder<T, TElement>(rule, this);
}

Expand All @@ -354,6 +358,7 @@ ValueTask<ValidationResult> completedValueTask
expression.Guard("Cannot pass null to RuleForEach", nameof(expression));
var rule = CollectionPropertyRule<T, TTransformed>.CreateTransformed(expression, to, () => RuleLevelCascadeMode);
Rules.Add(rule);
OnRuleAdded(rule);
return new RuleBuilder<T, TTransformed>(rule, this);
}

Expand All @@ -370,6 +375,7 @@ ValueTask<ValidationResult> completedValueTask
expression.Guard("Cannot pass null to RuleForEach", nameof(expression));
var rule = CollectionPropertyRule<T, TTransformed>.CreateTransformed(expression, to, () => RuleLevelCascadeMode);
Rules.Add(rule);
OnRuleAdded(rule);
return new RuleBuilder<T, TTransformed>(rule, this);
}

Expand Down Expand Up @@ -466,6 +472,7 @@ public IConditionBuilder UnlessAsync(Func<T, ValidationContext<T>, CancellationT
rulesToInclude.Guard("Cannot pass null to Include", nameof(rulesToInclude));
var rule = IncludeRule<T>.Create(rulesToInclude, () => RuleLevelCascadeMode);
Rules.Add(rule);
OnRuleAdded(rule);
}

/// <summary>
Expand All @@ -475,6 +482,7 @@ public IConditionBuilder UnlessAsync(Func<T, ValidationContext<T>, CancellationT
rulesToInclude.Guard("Cannot pass null to Include", nameof(rulesToInclude));
var rule = IncludeRule<T>.Create(rulesToInclude, () => RuleLevelCascadeMode);
Rules.Add(rule);
OnRuleAdded(rule);
}

/// <summary>
Expand Down Expand Up @@ -514,4 +522,11 @@ protected virtual void EnsureInstanceNotNull(object instanceToValidate)
/// <exception cref="ValidationException"></exception>
protected virtual void RaiseValidationException(ValidationContext<T> context, ValidationResult result)
=> throw new ValidationException(result.Errors);

/// <summary>
/// This method is invoked when a rule has been created (via RuleFor/RuleForEach) and has been added to the validator.
/// You can override this method to provide customizations to all rule instances.
/// </summary>
/// <param name="rule"></param>
protected virtual void OnRuleAdded(IValidationRule<T> rule) { }
}

0 comments on commit 32befcf

Please sign in to comment.