0
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.ComponentModel;
|
|
4
|
|
5 using BLToolkit.Reflection;
|
|
6
|
|
7 namespace BLToolkit.Validation
|
|
8 {
|
|
9 public class Validator
|
|
10 {
|
|
11 #region Validate
|
|
12
|
|
13 public static void Validate(ValidationContext context)
|
|
14 {
|
|
15 foreach (MemberAccessor ma in context.TypeAccessor)
|
|
16 {
|
|
17 var attrs = ma.GetAttributes<ValidatorBaseAttribute>();
|
|
18
|
|
19 if (attrs == null)
|
|
20 continue;
|
|
21
|
|
22 context.MemberAccessor = ma;
|
|
23 context.Value = ma.GetValue(context.Object);
|
|
24
|
|
25 for (var i = 0; i < attrs.Length; i++)
|
|
26 {
|
|
27 var attr = attrs[i];
|
|
28 if (attr.IsValid(context) == false)
|
|
29 throw new ValidationException(attr.GetErrorMessage(context));
|
|
30 }
|
|
31 }
|
|
32 }
|
|
33
|
|
34 public static void Validate(object obj, ValidationContext.IsNullHandler isNull)
|
|
35 {
|
|
36 Validate(InitContext(null, obj, null, isNull));
|
|
37 }
|
|
38
|
|
39 public static void Validate(object obj)
|
|
40 {
|
|
41 Validate(obj, null);
|
|
42 }
|
|
43
|
|
44 #endregion
|
|
45
|
|
46 #region Protected Members
|
|
47
|
|
48 private static bool IsNullInternal(ValidationContext context)
|
|
49 {
|
|
50 if (context.Value == null)
|
|
51 return true;
|
|
52
|
|
53 if (context.NullValue is DBNull)
|
|
54 return false;
|
|
55
|
|
56 return context.NullValue.Equals(context.Value);
|
|
57 }
|
|
58
|
|
59 public static ValidationContext InitContext(
|
|
60 ValidationContext context,
|
|
61 object obj,
|
|
62 PropertyDescriptor pd,
|
|
63 ValidationContext.IsNullHandler isNull)
|
|
64 {
|
|
65 if (context == null)
|
|
66 context = new ValidationContext();
|
|
67
|
|
68 context.Object = obj;
|
|
69 context.IsNull = isNull ?? new ValidationContext.IsNullHandler(IsNullInternal);
|
|
70 context.PropertyDescriptor = pd;
|
|
71
|
|
72 return context;
|
|
73 }
|
|
74
|
|
75 #endregion
|
|
76
|
|
77 #region IsValid
|
|
78
|
|
79 public static bool IsValid(ValidationContext context, string fieldName)
|
|
80 {
|
|
81 ValidatorBaseAttribute[] attrs = null;
|
|
82 object value = null;
|
|
83
|
|
84 #if !SILVERLIGHT
|
|
85
|
|
86 if (context.PropertyDescriptor != null)
|
|
87 {
|
|
88 value = context.PropertyDescriptor.GetValue(context.Object);
|
|
89
|
|
90 List<ValidatorBaseAttribute> list = null;
|
|
91
|
|
92 foreach (var o in context.PropertyDescriptor.Attributes)
|
|
93 {
|
|
94 if (o is ValidatorBaseAttribute)
|
|
95 {
|
|
96 if (list == null)
|
|
97 list = new List<ValidatorBaseAttribute>();
|
|
98
|
|
99 list.Add((ValidatorBaseAttribute)o);
|
|
100 }
|
|
101 }
|
|
102
|
|
103 if (list != null)
|
|
104 attrs = list.ToArray();
|
|
105 }
|
|
106 else
|
|
107
|
|
108 #endif
|
|
109
|
|
110 {
|
|
111 context.MemberAccessor = context.TypeAccessor[fieldName];
|
|
112
|
|
113 if (context.MemberAccessor != null)
|
|
114 {
|
|
115 value = context.MemberAccessor.GetValue(context.Object);
|
|
116 attrs = context.MemberAccessor.GetAttributes<ValidatorBaseAttribute>();
|
|
117 }
|
|
118 }
|
|
119
|
|
120 if (attrs != null)
|
|
121 {
|
|
122 context.Value = value;
|
|
123
|
|
124 for (var i = 0; i < attrs.Length; i++)
|
|
125 {
|
|
126 if (!attrs[i].IsValid(context))
|
|
127 return false;
|
|
128 }
|
|
129 }
|
|
130
|
|
131 return true;
|
|
132 }
|
|
133
|
|
134 public static bool IsValid(object obj, string fieldName, ValidationContext.IsNullHandler isNull)
|
|
135 {
|
|
136 return IsValid(InitContext(null, obj, null, isNull), fieldName);
|
|
137 }
|
|
138
|
|
139 #if !SILVERLIGHT
|
|
140
|
|
141 public static bool IsValid(object obj, PropertyDescriptor pd, ValidationContext.IsNullHandler isNull)
|
|
142 {
|
|
143 return IsValid(InitContext(null, obj, pd, isNull), pd.Name);
|
|
144 }
|
|
145
|
|
146 public static bool IsValid(object obj, PropertyDescriptor pd)
|
|
147 {
|
|
148 return IsValid(obj, pd, null);
|
|
149 }
|
|
150
|
|
151 #endif
|
|
152
|
|
153 public static bool IsValid(object obj, string fieldName)
|
|
154 {
|
|
155 return IsValid(obj, fieldName, null);
|
|
156 }
|
|
157
|
|
158 #endregion
|
|
159
|
|
160 #region GetErrorMessages
|
|
161
|
|
162 public static string[] GetErrorMessages(ValidationContext context, string fieldName)
|
|
163 {
|
|
164 context.MemberAccessor = context.TypeAccessor[fieldName];
|
|
165
|
|
166 if (context.MemberAccessor != null)
|
|
167 {
|
|
168 var messages = new List<string>();
|
|
169 var attrs = context.MemberAccessor.GetAttributes<ValidatorBaseAttribute>();
|
|
170
|
|
171 if (attrs != null)
|
|
172 {
|
|
173 context.Value = context.MemberAccessor.GetValue(context.Object);
|
|
174
|
|
175 for (var i = 0; i < attrs.Length; i++)
|
|
176 messages.Add(attrs[i].GetErrorMessage(context));
|
|
177
|
|
178 return messages.ToArray();
|
|
179 }
|
|
180 }
|
|
181
|
|
182 return new string[0];
|
|
183 }
|
|
184
|
|
185 public static string[] GetErrorMessages(
|
|
186 object obj, string fieldName, ValidationContext.IsNullHandler isNull)
|
|
187 {
|
|
188 return GetErrorMessages(InitContext(null, obj, null, isNull), fieldName);
|
|
189 }
|
|
190
|
|
191 #if !SILVERLIGHT
|
|
192
|
|
193 public static string[] GetErrorMessages(object obj, PropertyDescriptor pd, ValidationContext.IsNullHandler isNull)
|
|
194 {
|
|
195 return GetErrorMessages(InitContext(null, obj, pd, isNull), pd.Name);
|
|
196 }
|
|
197
|
|
198 public static string[] GetErrorMessages(object obj, PropertyDescriptor pd)
|
|
199 {
|
|
200 return GetErrorMessages(obj, pd, null);
|
|
201 }
|
|
202
|
|
203 #endif
|
|
204
|
|
205 public static string[] GetErrorMessages(object obj, string fieldName)
|
|
206 {
|
|
207 return GetErrorMessages(obj, fieldName, null);
|
|
208 }
|
|
209
|
|
210 #endregion
|
|
211 }
|
|
212 }
|