Fixing an issue where setting a custom property on a handler will not propagate it...
[castle.git] / Components / General / Validator / Castle.Components.Validator / ErrorSummary.cs
blob778033b46207bb28872f3ff2d990e31c96da60f3
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Components.Validator
17 using System;
18 using System.Collections;
19 using System.Collections.Generic;
20 using System.Reflection;
22 /// <summary>
23 /// Represents a validation report for an object instance
24 /// which is a snapshot since the last validation check.
25 /// </summary>
26 [Serializable]
27 public class ErrorSummary
29 private int errorsCount;
30 private int invalidPropertiesCount;
31 private IDictionary<string, string[]> property2Messages =
32 new Dictionary<string, string[]>(StringComparer.InvariantCultureIgnoreCase);
34 /// <summary>
35 /// Gets the total of validation errors since the last validation check.
36 /// <para>
37 /// That includes all errors for all properties.
38 /// </para>
39 /// </summary>
40 /// <value>The error count.</value>
41 public int ErrorsCount
43 get { return errorsCount; }
46 /// <summary>
47 /// Gets the total of properties that have failed validation checks.
48 /// </summary>
49 public int InvalidPropertiesCount
51 get { return invalidPropertiesCount; }
54 /// <summary>
55 /// Gets the invalid properties' name.
56 /// </summary>
57 /// <value>The invalid properties.</value>
58 public string[] InvalidProperties
60 get
62 string[] names = new string[property2Messages.Count];
63 property2Messages.Keys.CopyTo(names, 0);
64 return names;
68 /// <summary>
69 /// Gets the error messages.
70 /// </summary>
71 /// <value>The error messages.</value>
72 public string[] ErrorMessages
74 get
76 string[] messages = new string[errorsCount];
78 int index = 0;
80 foreach(IList list in property2Messages.Values)
82 list.CopyTo(messages, index);
83 index += list.Count;
86 return messages;
90 /// <summary>
91 /// Gets the errors for a property.
92 /// </summary>
93 /// <param name="name">The property name.</param>
94 /// <returns>Array of error messages</returns>
95 public string[] GetErrorsForProperty(string name)
97 if (name == null) throw new ArgumentNullException("name");
99 if (!property2Messages.ContainsKey(name))
101 return new string[0];
104 string[] messages = property2Messages[name];
106 return messages;
109 /// <summary>
110 /// Registers the error message per <see cref="PropertyInfo"/>.
111 /// </summary>
112 /// <param name="property">The property.</param>
113 /// <param name="message">The message.</param>
114 public void RegisterErrorMessage(PropertyInfo property, string message)
116 if (property == null) throw new ArgumentNullException("property");
117 if (message == null) throw new ArgumentNullException("message");
119 RegisterErrorMessage(property.Name, message);
122 /// <summary>
123 /// Registers the error message per <see cref="PropertyInfo"/>.
124 /// </summary>
125 /// <param name="property">The property.</param>
126 /// <param name="message">The message.</param>
127 public void RegisterErrorMessage(string property, string message)
129 if (property == null) throw new ArgumentNullException("property");
130 if (message == null) throw new ArgumentNullException("message");
132 if (!property2Messages.ContainsKey(property))
134 property2Messages[property] = new string[] { message };
136 else
138 string[] messages = property2Messages[property];
139 Array.Resize(ref messages, messages.Length + 1);
140 messages[messages.Length - 1] = message;
141 property2Messages[property] = messages;
144 errorsCount++;
145 invalidPropertiesCount = property2Messages.Count;