Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit 2880d673 authored by Michael KRISPER's avatar Michael KRISPER
Browse files

added documentation for validation classes.

parent 1956ef1c
No related branches found
No related tags found
No related merge requests found
......@@ -7,8 +7,17 @@ using System.Reflection;
namespace TUGraz.VectoCore.Utils
{
/// <summary>
/// Helper Class for doing the Validation
/// </summary>
public static class ValidationHelper
{
/// <summary>
/// Validates the specified entity and all its properties recursively. (Extension Method)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entity">The entity.</param>
/// <returns>Null, if the validation was successfull. Otherwise a list of ValidationResults with the ErrorMessages.</returns>
public static IList<ValidationResult> Validate<T>(this T entity)
{
var context = new ValidationContext(entity);
......@@ -46,15 +55,31 @@ namespace TUGraz.VectoCore.Utils
return results;
}
/// <summary>
/// Determines whether this instance is valid. (Extension Method)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entity">The entity.</param>
/// <returns></returns>
public static bool IsValid<T>(this T entity)
{
return Validator.TryValidateObject(entity, new ValidationContext(entity), null, true);
}
}
/// <summary>
/// Determines that the attributed object should be validated recursively.
/// </summary>
public class ValidateObjectAttribute : ValidationAttribute
{
/// <summary>
/// Validates an object recursively (all elements if its a list, or all fields and properties if its an object).
/// </summary>
/// <param name="value">The value to validate.</param>
/// <param name="validationContext">The context information about the validation operation.</param>
/// <returns>
/// ValidationResult.Success if the validation was successfull. Otherwise the joined ErrorMessages are returned.
/// </returns>
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value == null) {
......@@ -87,20 +112,59 @@ namespace TUGraz.VectoCore.Utils
}
}
/// <summary>
/// Attribute which validates the Min-Max Range of an SI Object.
/// </summary>
public class SIRangeAttribute : RangeAttribute
{
/// <summary>
/// Checks the Min-Max Range of SI Objects.
/// </summary>
/// <param name="minimum">The minimum.</param>
/// <param name="maximum">The maximum.</param>
public SIRangeAttribute(int minimum, int maximum) : base(minimum, maximum) {}
/// <summary>
/// Checks the Min-Max Range of SI Objects.
/// </summary>
/// <param name="minimum">The minimum.</param>
/// <param name="maximum">The maximum.</param>
public SIRangeAttribute(double minimum, double maximum) : base(minimum, maximum) {}
/// <summary>
/// Checks the Min-Max Range of SI Objects.
/// </summary>
/// <param name="minimum">The minimum.</param>
/// <param name="maximum">The maximum.</param>
public SIRangeAttribute(SI minimum, SI maximum) : base(minimum.Value(), maximum.Value()) {}
/// <summary>
/// Validates that an SI Object is inside the min-max range.
/// </summary>
/// <param name="value">The value to validate.</param>
/// <param name="validationContext">The context information about the validation operation.</param>
/// <returns>
/// ValidationResult.Success if the validation was successfull, otherwise an Instance of ValidationResult with the ErrorMessage.
/// </returns>
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
return base.IsValid(((SI)value).Value(), validationContext);
}
}
/// <summary>
/// Attribute which validates a Path.
/// </summary>
public class PathAttribute : ValidationAttribute
{
/// <summary>
/// Validates that a path actually exists.
/// </summary>
/// <param name="value">The value to validate.</param>
/// <param name="validationContext">The context information about the validation operation.</param>
/// <returns>
/// ValidationResult.Success if the validation was successfull, otherwise an Instance of ValidationResult with the ErrorMessage.
/// </returns>
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (!File.Exists((string)value)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment