Skip to content

Commit

Permalink
Add severity with validation context
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiří Mašek authored and JeremySkinner committed Mar 15, 2023
1 parent 67042d8 commit 422b687
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/FluentValidation/DefaultValidatorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,25 @@ public static class DefaultValidatorOptions {
return rule;
}

/// <summary>
/// Specifies custom severity that should be stored alongside the validation message when validation fails for this rule.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TProperty"></typeparam>
/// <param name="rule"></param>
/// <param name="severityProvider"></param>
/// <returns></returns>
public static IRuleBuilderOptions<T, TProperty> WithSeverity<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule, Func<T, TProperty, ValidationContext<T>, Severity> severityProvider) {
severityProvider.Guard("A lambda expression must be passed to WithSeverity", nameof(severityProvider));

Severity SeverityProvider(ValidationContext<T> ctx, TProperty value) {
return severityProvider(ctx.InstanceToValidate, value, ctx);
}

Configurable(rule).Current.SeverityProvider = SeverityProvider;
return rule;
}

/// <summary>
/// Allows the generated indexer to be overridden for collection rules.
/// </summary>
Expand Down

0 comments on commit 422b687

Please sign in to comment.