-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxLengthRule.cs
41 lines (37 loc) · 1.17 KB
/
MaxLengthRule.cs
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
35
36
37
38
39
40
41
using DotnetToolset.ExtensionMethods;
using DotnetToolset.Patterns.Dddd.Interfaces;
using System.Collections.Generic;
using DotnetToolset.Patterns.Dddd.Enums;
using Res = DotnetToolset.Patterns.Resources.Literals;
namespace DotnetToolset.Patterns.Dddd.Implementations.Rules
{
/// <inheritdoc />
public class MaxLengthRule : IRule
{
/// <summary>
/// Constructor to set the rule value
/// </summary>
/// <param name="maxLength"></param>
public MaxLengthRule(int maxLength)
{
Rule = new KeyValuePair<RuleType, object>(RuleType.MaxLength, maxLength);
}
/// <summary>
/// Rule
/// </summary>
public KeyValuePair<RuleType, object> Rule { get; set; }
/// <summary>
/// Validates data passed using the rule logic
/// </summary>
/// <param name="value">Data to be validated</param>
/// <returns>True if rule passed and an optional error message</returns>
public (bool isValid, string errorMessage) Validate(object value)
{
if (string.IsNullOrEmpty((string)value) || ((string)value).Length <= (int)Rule.Value)
{
return (true, null);
}
return (false, Res.p_RuleLengthNotPassed.ParseParameters(new[] { value.ToString(), Rule.Value }));
}
}
}