forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRulesetValidatorSelector.cs
More file actions
34 lines (30 loc) · 1.36 KB
/
Copy pathRulesetValidatorSelector.cs
File metadata and controls
34 lines (30 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
namespace ServiceStack.FluentValidation.Internal
{
using System;
using System.Linq;
/// <summary>
/// Selects validators that belong to the specified rulesets.
/// </summary>
public class RulesetValidatorSelector : IValidatorSelector {
readonly string[] rulesetsToExecute;
/// <summary>
/// Creates a new instance of the RulesetValidatorSelector.
/// </summary>
public RulesetValidatorSelector(params string[] rulesetsToExecute) {
this.rulesetsToExecute = rulesetsToExecute;
}
/// <summary>
/// Determines whether or not a rule should execute.
/// </summary>
/// <param name="rule">The rule</param>
/// <param name="propertyPath">Property path (eg Customer.Address.Line1)</param>
/// <param name="context">Contextual information</param>
/// <returns>Whether or not the validator can execute.</returns>
public bool CanExecute(IValidationRule rule, string propertyPath, ValidationContext context) {
if (string.IsNullOrEmpty(rule.RuleSet) && rulesetsToExecute.Length == 0) return true;
if (!string.IsNullOrEmpty(rule.RuleSet) && rulesetsToExecute.Length > 0 && rulesetsToExecute.Contains(rule.RuleSet)) return true;
if (rulesetsToExecute.Contains("*")) return true;
return false;
}
}
}