-
Notifications
You must be signed in to change notification settings - Fork 0
/
IsIntegerRule.cs
39 lines (35 loc) · 1.04 KB
/
IsIntegerRule.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
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 IsIntegerRule : IRule
{
/// <summary>
/// Constructor to set the rule value
/// </summary>
public IsIntegerRule()
{
Rule = new KeyValuePair<RuleType, object>(RuleType.IsInteger, null);
}
/// <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 (value != null && int.TryParse(value.ToString(), out _))
{
return (true, null);
}
return (false, Res.p_RuleIsIntegerNotPassed);
}
}
}