1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Collections
;
19 using System
.Collections
.Generic
;
20 using System
.Reflection
;
23 /// Represents a validation report for an object instance
24 /// which is a snapshot since the last validation check.
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
);
35 /// Gets the total of validation errors since the last validation check.
37 /// That includes all errors for all properties.
40 /// <value>The error count.</value>
41 public int ErrorsCount
43 get { return errorsCount; }
47 /// Gets the total of properties that have failed validation checks.
49 public int InvalidPropertiesCount
51 get { return invalidPropertiesCount; }
55 /// Gets the invalid properties' name.
57 /// <value>The invalid properties.</value>
58 public string[] InvalidProperties
62 string[] names
= new string[property2Messages
.Count
];
63 property2Messages
.Keys
.CopyTo(names
, 0);
69 /// Gets the error messages.
71 /// <value>The error messages.</value>
72 public string[] ErrorMessages
76 string[] messages
= new string[errorsCount
];
80 foreach(IList list
in property2Messages
.Values
)
82 list
.CopyTo(messages
, index
);
91 /// Gets the errors for a property.
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
];
110 /// Registers the error message per <see cref="PropertyInfo"/>.
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
);
123 /// Registers the error message per <see cref="PropertyInfo"/>.
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 }
;
138 string[] messages
= property2Messages
[property
];
139 Array
.Resize(ref messages
, messages
.Length
+ 1);
140 messages
[messages
.Length
- 1] = message
;
141 property2Messages
[property
] = messages
;
145 invalidPropertiesCount
= property2Messages
.Count
;