You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+101-3Lines changed: 101 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -30,18 +30,116 @@ You can use C# 7 Tuples for more complex Types with multiple values:
30
30
31
31
### Validation
32
32
33
-
You can add validation to your Types by overriding the `protected void Validate() { } ` method:
33
+
There are two, independent ways to add validation to your Types. Both ways validate during Type creation.
34
+
35
+
#### Throw an exception during creation
36
+
37
+
You can add validation to your Types by overriding the `protected void Validate() { }` method to throw an exception when the type is created using `.From()`:
38
+
39
+
```
40
+
public class EmailAddress : ValueOf<string, EmailAddress>
41
+
{
42
+
protected override void Validate()
43
+
{
44
+
if (string.IsNullOrWhiteSpace(Value))
45
+
throw new ArgumentException("Value cannot be null or empty");
46
+
}
47
+
}
48
+
49
+
void Main()
50
+
{
51
+
// This will throw an ArgumentException
52
+
var myEmailAddress = EmailAddress.From("");
53
+
}
54
+
```
55
+
56
+
#### Return false while using an `out` argument
57
+
58
+
You may also implement validation to your Types by overriding the `protected bool TryValidate() { }` method to return `false`. This will cause the `TryFrom()` creation method to return false:
59
+
60
+
```
61
+
public class EmailAddress : ValueOf<string, EmailAddress>
62
+
{
63
+
protected override bool TryValidate()
64
+
{
65
+
if (string.IsNullOrWhitespace(Value))
66
+
return false;
67
+
}
68
+
}
69
+
70
+
void Main()
71
+
{
72
+
if (!EmailAddress.TryFrom("", out var myEmailAddress)
73
+
{
74
+
Console.WriteLine("Invalid email address");
75
+
}
76
+
}
77
+
```
78
+
79
+
#### Validation best practices
80
+
81
+
It is recommended to override both `Validate` and `TryValidate` with the same failure cases, if you choose to implement validation at all.
82
+
83
+
For example:
34
84
35
85
```
36
-
public class ValidatedClientRef : ValueOf<string, ValidatedClientRef>
86
+
public class EmailAddress : ValueOf<string, EmailAddress>
87
+
{
88
+
protected override void Validate()
89
+
{
90
+
if (!IsValidEmailAddress(Value))
91
+
throw new ArgumentException("Invalid email address");
92
+
}
93
+
94
+
protected override bool TryValidate()
95
+
{
96
+
return IsValidEmailAddress(Value);
97
+
}
98
+
99
+
// Prevent circular references from Validate and TryValidate by
100
+
// breaking out the logic into a separate method
101
+
public static bool IsValidEmailAddress(string value)
102
+
{
103
+
return !string.IsNullOrWhitespace(value)
104
+
}
105
+
}
106
+
```
107
+
108
+
If you prefer one method over the other, then consider at least overriding the alternative method to always fail. This will prevent someone from creating an invalid Type using the creation method you did not override.
109
+
110
+
For example:
111
+
112
+
```
113
+
public class EmailAddress : ValueOf<string, EmailAddress>
114
+
{
115
+
protected override void Validate()
116
+
{
117
+
throw new NotSupportedException("Use TryValidate() and TryFrom() instead");
118
+
}
119
+
120
+
protected override bool TryValidate()
121
+
{
122
+
return !string.IsNullOrWhitespace(Value)
123
+
}
124
+
}
125
+
```
126
+
127
+
or
128
+
129
+
```
130
+
public class EmailAddress : ValueOf<string, EmailAddress>
37
131
{
38
132
protected override void Validate()
39
133
{
40
134
if (string.IsNullOrWhiteSpace(Value))
41
135
throw new ArgumentException("Value cannot be null or empty");
42
136
}
43
-
}
44
137
138
+
protected override bool TryValidate()
139
+
{
140
+
return false; // Use Validate() and From() instead
0 commit comments