-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathNonEmptyNameDTO.cs
47 lines (40 loc) · 1.34 KB
/
NonEmptyNameDTO.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
42
43
44
45
46
47
using System.Linq;
using OSPSuite.Assets;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Extensions;
using OSPSuite.Utility.Validation;
namespace OSPSuite.Presentation.DTO
{
public class NonEmptyNameDTO : ValidatableDTO
{
private string _name;
public virtual string Name
{
get => _name;
set
{
_name = value.TrimmedValue();
OnPropertyChanged(() => Name);
}
}
public NonEmptyNameDTO()
{
Rules.Add(AllRules.NameNotEmpty);
Rules.Add(AllRules.NameDoesNotContainIllegalCharacters);
}
private static bool nameDoesNotContainerIllegalCharacters(string name)
{
if (string.IsNullOrEmpty(name))
return true;
return !Constants.ILLEGAL_CHARACTERS.Any(name.Contains);
}
protected static class AllRules
{
public static IBusinessRule NameNotEmpty { get; } = GenericRules.NonEmptyRule<NonEmptyNameDTO>(x => x.Name, Error.NameIsRequired);
public static IBusinessRule NameDoesNotContainIllegalCharacters { get; } = CreateRule.For<NonEmptyNameDTO>()
.Property(item => item.Name)
.WithRule((dto, name) => nameDoesNotContainerIllegalCharacters(name))
.WithError(Error.NameCannotContainIllegalCharacters(Constants.ILLEGAL_CHARACTERS));
}
}
}