1
+ using System . ComponentModel . DataAnnotations ;
2
+ using System . Text . RegularExpressions ;
3
+
4
+ namespace WebApi . Area . Product . Utility
5
+ {
6
+ /// <summary>
7
+ /// [CustomModelValidation] Validate unique identifier.
8
+ /// </summary>
9
+ /// <remarks>
10
+ /// Criteria:
11
+ /// <br>1. It cannot be null or whitespace.</br>
12
+ /// <br>2. It must be numeric (0-9).</br>
13
+ /// </remarks>
14
+ public class ValidateUniqueIdentifierAttribute : ValidationAttribute
15
+ {
16
+ // Keep the expression compiled to improve performance.
17
+ private static readonly Regex ValidationRegex = new Regex ( @"^[0-9]*" , RegexOptions . Compiled ) ;
18
+
19
+ protected override ValidationResult IsValid ( object value , ValidationContext validationContext )
20
+ {
21
+ if ( string . IsNullOrWhiteSpace ( value . ToString ( ) ) )
22
+ {
23
+ return new ValidationResult ( validationContext . MemberName + " is required." ) ;
24
+ }
25
+ if ( ! ValidationRegex . IsMatch ( value . ToString ( ) ) )
26
+ {
27
+ return new ValidationResult ( validationContext . MemberName + " must be numeric (0-9)." ) ;
28
+ }
29
+ return ValidationResult . Success ;
30
+ }
31
+ }
32
+
33
+ /// <summary>
34
+ /// [CustomModelValidation] Validate Product name.
35
+ /// </summary>
36
+ /// <remarks>
37
+ /// Criteria:
38
+ /// <br>1. It cannot be null or whitespace.</br>
39
+ /// <br>2. It must begin with an alphabet (A-Za-z).</br>
40
+ /// </remarks>
41
+ public class ValidateProductNameAttribute : ValidationAttribute
42
+ {
43
+ // Keep the expression compiled to improve performance.
44
+ private static readonly Regex ValidationRegex = new Regex ( @"^[A-Za-z]*" , RegexOptions . Compiled ) ;
45
+
46
+ protected override ValidationResult IsValid ( object value , ValidationContext validationContext )
47
+ {
48
+ if ( string . IsNullOrWhiteSpace ( value . ToString ( ) ) )
49
+ {
50
+ return new ValidationResult ( validationContext . MemberName + " is required." ) ;
51
+ }
52
+ if ( ! ValidationRegex . IsMatch ( value . ToString ( ) ) )
53
+ {
54
+ return new ValidationResult ( validationContext . MemberName + " must begin wtih an alphabet (A-Za-z)." ) ;
55
+ }
56
+ return ValidationResult . Success ;
57
+ }
58
+ }
59
+
60
+ /// <summary>
61
+ /// [CustomModelValidation] Validate Product unit in stock.
62
+ /// </summary>
63
+ /// <remarks>
64
+ /// Criteria:
65
+ /// <br>1. It cannot be null or whitespace.</br>
66
+ /// <br>1. If not null, it must be a be a round integer (ie. no decimal).</br>
67
+ /// </remarks>
68
+ public class ValidateUnitInStockAttribute : ValidationAttribute
69
+ {
70
+ // Keep the expression compiled to improve performance.
71
+ private static readonly Regex ValidationRegex = new Regex ( @"^[0-9]*$" , RegexOptions . Compiled ) ;
72
+
73
+ protected override ValidationResult IsValid ( object value , ValidationContext validationContext )
74
+ {
75
+ if ( string . IsNullOrWhiteSpace ( value . ToString ( ) ) )
76
+ {
77
+ return new ValidationResult ( validationContext . MemberName + " is required." ) ;
78
+ }
79
+ if ( ! ValidationRegex . IsMatch ( value . ToString ( ) ) )
80
+ {
81
+ return new ValidationResult ( validationContext . MemberName + " must be a round integer (ie. no decimal)." ) ;
82
+ }
83
+ return ValidationResult . Success ;
84
+ }
85
+ }
86
+
87
+ /// <summary>
88
+ /// [CustomModelValidation] Validate Product unit price.
89
+ /// </summary>
90
+ /// <remarks>
91
+ /// Criteria:
92
+ /// <br>1. It cannot be null or whitespace.</br>
93
+ /// <br>2. It must be a valid currency denomination (0-9).</br>
94
+ /// </remarks>
95
+ public class ValidateUnitPriceAttribute : ValidationAttribute
96
+ {
97
+ // Keep the expression compiled to improve performance.
98
+ private static readonly Regex ValidationRegex = new Regex ( @"^[0-9]*[.]{0,1}[0-9]{0,2}$" , RegexOptions . Compiled ) ;
99
+
100
+ protected override ValidationResult IsValid ( object value , ValidationContext validationContext )
101
+ {
102
+ if ( string . IsNullOrWhiteSpace ( value . ToString ( ) ) )
103
+ {
104
+ return new ValidationResult ( validationContext . MemberName + " is required." ) ;
105
+ }
106
+ if ( ! ValidationRegex . IsMatch ( value . ToString ( ) ) )
107
+ {
108
+ return new ValidationResult ( validationContext . MemberName + " must be in a valid currency denomination." ) ;
109
+ }
110
+ return ValidationResult . Success ;
111
+ }
112
+ }
113
+
114
+
115
+ /// <summary>
116
+ /// [CustomModelValidation] Validate nullable Product unit in stock.
117
+ /// </summary>
118
+ /// <remarks>
119
+ /// Criteria:
120
+ /// <br>1. If not null, it must be a be a round integer (ie. no decimal).</br>
121
+ /// </remarks>
122
+ public class ValidateNullableUnitInStockAttribute : ValidationAttribute
123
+ {
124
+ // Keep the expression compiled to improve performance.
125
+ private static readonly Regex ValidationRegex = new Regex ( @"^[0-9]*$" , RegexOptions . Compiled ) ;
126
+
127
+ protected override ValidationResult IsValid ( object value , ValidationContext validationContext )
128
+ {
129
+ if ( value != null )
130
+ {
131
+ if ( ! ValidationRegex . IsMatch ( value . ToString ( ) ) )
132
+ {
133
+ return new ValidationResult ( validationContext . MemberName + " must be a positive whole number (ie. no decimal)." ) ;
134
+ }
135
+ }
136
+ return ValidationResult . Success ;
137
+ }
138
+ }
139
+
140
+ /// <summary>
141
+ /// [CustomModelValidation] Validate nullable Product unit price.
142
+ /// </summary>
143
+ /// <remarks>
144
+ /// Criteria:
145
+ /// <br>1. If not null, it must be a valid currency denomination (0-9).</br>
146
+ /// </remarks>
147
+ public class ValidateNullableUnitPriceAttribute : ValidationAttribute
148
+ {
149
+ // Keep the expression compiled to improve performance.
150
+ private static readonly Regex ValidationRegex = new Regex ( @"^[0-9]*[.]{0,1}[0-9]{0,2}$" , RegexOptions . Compiled ) ;
151
+
152
+ protected override ValidationResult IsValid ( object value , ValidationContext validationContext )
153
+ {
154
+ if ( value != null )
155
+ {
156
+ if ( ! ValidationRegex . IsMatch ( value . ToString ( ) ) )
157
+ {
158
+ return new ValidationResult ( validationContext . MemberName + " must be in a valid positive currency denomination." ) ;
159
+ }
160
+ }
161
+ return ValidationResult . Success ;
162
+ }
163
+ }
164
+
165
+ }
0 commit comments