/// <summary> /// <para>Validate the ranige data for the given <paramref name="instance"/> and the <paramref name="propertyInfo"/>.</para> /// </summary> /// <param name="instance"> /// <para>The instance to validate.</para> /// </param> /// <param name="propertyInfo"> /// <para>The property contaning the value to validate.</para> /// </param> /// <param name="errors"> /// <para>The collection to add any errors that occur durring the validation.</para> /// </param> public override void Validate(object instance, PropertyInfo propertyInfo, ValidationErrorCollection errors) { ArgumentValidation.CheckForNullReference(instance, "instance"); ArgumentValidation.CheckForNullReference(propertyInfo, "propertyInfo"); ArgumentValidation.CheckForNullReference(errors, "errors"); object propertyValue = propertyInfo.GetValue(instance, null); string fileName = propertyValue as string; // I have to assume this is ok if ((fileName == null) || (fileName.Length == 0)) return; DetermineIfCanCreateFile(fileName, errors, instance, propertyInfo); DetermineIfCanWriteFile(fileName, errors, instance, propertyInfo); }
/// <summary> /// <para>Validate the ranige data for the given <paramref name="instance"/> and the <paramref name="propertyInfo"/>.</para> /// </summary> /// <param name="instance"> /// <para>The instance to validate.</para> /// </param> /// <param name="propertyInfo"> /// <para>The property contaning the value to validate.</para> /// </param> /// <param name="errors"> /// <para>The collection to add any errors that occur durring the validation.</para> /// </param> public override void Validate(object instance, PropertyInfo propertyInfo, ValidationErrorCollection errors) { ArgumentValidation.CheckForNullReference(instance, "instance"); ArgumentValidation.CheckForNullReference(propertyInfo, "propertyInfo"); ArgumentValidation.CheckForNullReference(errors, "errors"); object propertyValue = propertyInfo.GetValue(instance, null); if (propertyValue is String) { string valueString = (string)propertyValue; if ((valueString == null) || (valueString.Length == 0) || (valueString.Length > maximumLength)) { string name = propertyInfo.Name; errors.Add(instance, name, SR.MaxLengthExceededErrorMessage(name, maximumLength)); } } }
public override void Validate(object instance, PropertyInfo propertyInfo, ValidationErrorCollection errors) { ArgumentValidation.CheckForNullReference(instance, "instance"); ArgumentValidation.CheckForNullReference(propertyInfo, "propertyInfo"); ArgumentValidation.CheckForNullReference(errors, "errors"); base.Validate(instance, propertyInfo, errors); object propertyValue = propertyInfo.GetValue(instance, null); string typeName = propertyValue as string; // the required attribute will catch this for us if (typeName == null || typeName.Length == 0) return; Type t = Type.GetType(typeName, false, true); if (t == null) { string name = propertyInfo.Name; errors.Add(instance, name, SR.ExceptionTypeNotValid(typeName)); } }
/// <summary> /// <para>Validate the ranige data for the given <paramref name="instance"/> and the <paramref name="propertyInfo"/>.</para> /// </summary> /// <param name="instance"> /// <para>The instance to validate.</para> /// </param> /// <param name="propertyInfo"> /// <para>The property contaning the value to validate.</para> /// </param> /// <param name="errors"> /// <para>The collection to add any errors that occur durring the validation.</para> /// </param> public override void Validate(object instance, PropertyInfo propertyInfo, ValidationErrorCollection errors) { ArgumentValidation.CheckForNullReference(instance, "instance"); ArgumentValidation.CheckForNullReference(propertyInfo, "propertyInfo"); ArgumentValidation.CheckForNullReference(errors, "errors"); object propertyValue = propertyInfo.GetValue(instance, null); if (propertyValue is String) { string valueString = (string)propertyValue; if ((valueString == null) || (valueString.Length == 0) || (valueString.Length > maximumLength)) { string name = propertyInfo.Name; errors.Add(instance, name, SR.MaxLengthExceededErrorMessage(name, maximumLength)); } } }
private static void DetermineIfCanCreateFile(string fileName, ValidationErrorCollection errors, object instance, PropertyInfo info) { if (!File.Exists(fileName)) { bool createdDirectory = false; string directory = string.Empty; try { directory = Path.GetDirectoryName(fileName); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } using(File.Create(fileName)) {} } catch (IOException e) { errors.Add(new ValidationError(instance, info.Name, e.Message)); } catch (UnauthorizedAccessException e) { errors.Add(new ValidationError(instance, info.Name, e.Message)); } catch (ArgumentException e) { errors.Add(new ValidationError(instance, info.Name, e.Message)); } finally { try { File.Delete(fileName); if (createdDirectory) { Directory.Delete(directory); } } catch(IOException) {} catch (UnauthorizedAccessException) {} } } }
/// <summary> /// <para>Validate the ranige data for the given <paramref name="instance"/> and the <paramref name="propertyInfo"/>.</para> /// </summary> /// <param name="instance"> /// <para>The instance to validate.</para> /// </param> /// <param name="propertyInfo"> /// <para>The property contaning the value to validate.</para> /// </param> /// <param name="errors"> /// <para>The collection to add any errors that occur durring the validation.</para> /// </param> public override void Validate(object instance, PropertyInfo propertyInfo, ValidationErrorCollection errors) { ArgumentValidation.CheckForNullReference(instance, "instance"); ArgumentValidation.CheckForNullReference(propertyInfo, "propertyInfo"); ArgumentValidation.CheckForNullReference(errors, "errors"); object propertyValue = propertyInfo.GetValue(instance, null); string fileName = propertyValue as string; // I have to assume this is ok if ((fileName == null) || (fileName.Length == 0)) { return; } DetermineIfCanCreateFile(fileName, errors, instance, propertyInfo); DetermineIfCanWriteFile(fileName, errors, instance, propertyInfo); }
private static void DetermineIfCanCreateFile(string fileName, ValidationErrorCollection errors, object instance, PropertyInfo info) { if (!File.Exists(fileName)) { bool createdDirectory = false; string directory = string.Empty; try { directory = Path.GetDirectoryName(fileName); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } using (File.Create(fileName)) {} } catch (IOException e) { errors.Add(new ValidationError(instance, info.Name, e.Message)); } catch (UnauthorizedAccessException e) { errors.Add(new ValidationError(instance, info.Name, e.Message)); } catch (ArgumentException e) { errors.Add(new ValidationError(instance, info.Name, e.Message)); } finally { try { File.Delete(fileName); if (createdDirectory) { Directory.Delete(directory); } } catch (IOException) {} catch (UnauthorizedAccessException) {} } } }
/// <summary> /// Validates that the value of the specified /// property does not contain any syntax errors. /// </summary> /// <param name="instance"> /// <para>The instance to validate.</para> /// </param> /// <param name="info"> /// <para>The property contaning the value to validate.</para> /// </param> /// <param name="errors"> /// <para>The collection to add any errors that occur durring the validation.</para> /// </param> public override void Validate( object instance, PropertyInfo info, ValidationErrorCollection errors) { string expression = (string)info.GetValue(instance, null); if (expression != null && expression.Length != 0) { try { Parser parser = new Parser(); parser.Parse(expression); } catch (SyntaxException e) { errors.Add(instance, info.Name, e.Message); } } }
/// <summary> /// <para>Validate the ranige data for the given <paramref name="instance"/> and the <paramref name="propertyInfo"/>.</para> /// </summary> /// <param name="instance"> /// <para>The instance to validate.</para> /// </param> /// <param name="propertyInfo"> /// <para>The property contaning the value to validate.</para> /// </param> /// <param name="errors"> /// <para>The collection to add any errors that occur durring the validation.</para> /// </param> public override void Validate(object instance, PropertyInfo propertyInfo, ValidationErrorCollection errors) { ArgumentValidation.CheckForNullReference(instance, "instance"); ArgumentValidation.CheckForNullReference(propertyInfo, "propertyInfo"); ArgumentValidation.CheckForNullReference(errors, "errors"); object propertyValue = propertyInfo.GetValue(instance, null); if (propertyValue is String) { string valueString = (string)propertyValue; if ((valueString == null) || (valueString.Trim().Length == 0)) { errors.Add(instance, propertyInfo.Name, SR.ExceptionValueNullMessage(propertyInfo.Name)); } } else if (!(propertyValue is ValueType)) { if (propertyValue == null) { errors.Add(instance, propertyInfo.Name, SR.ExceptionValueNullMessage(propertyInfo.Name)); } } }
public override void Validate(object instance, PropertyInfo propertyInfo, ValidationErrorCollection errors) { ArgumentValidation.CheckForNullReference(instance, "instance"); ArgumentValidation.CheckForNullReference(propertyInfo, "propertyInfo"); ArgumentValidation.CheckForNullReference(errors, "errors"); base.Validate(instance, propertyInfo, errors); object propertyValue = propertyInfo.GetValue(instance, null); string typeName = propertyValue as string; // the required attribute will catch this for us if (typeName == null || typeName.Length == 0) { return; } Type t = Type.GetType(typeName, false, true); if (t == null) { string name = propertyInfo.Name; errors.Add(instance, name, SR.ExceptionTypeNotValid(typeName)); } }
private void DetermineIfCanWriteFile(string fileName, ValidationErrorCollection errors, object instance, PropertyInfo info) { if (File.Exists(fileName)) { try { using (File.OpenWrite(fileName)) {} } catch (IOException e) { errors.Add(new ValidationError(instance, info.Name, e.Message)); } catch (UnauthorizedAccessException e) { errors.Add(new ValidationError(instance, info.Name, e.Message)); } catch (ArgumentException e) { errors.Add(new ValidationError(instance, info.Name, e.Message)); } } }
/// <summary> /// <para>Validate the ranige data for the given <paramref name="instance"/> and the <paramref name="propertyInfo"/>.</para> /// </summary> /// <param name="instance"> /// <para>The instance to validate.</para> /// </param> /// <param name="propertyInfo"> /// <para>The property contaning the value to validate.</para> /// </param> /// <param name="errors"> /// <para>The collection to add any errors that occur durring the validation.</para> /// </param> public override void Validate(object instance, PropertyInfo propertyInfo, ValidationErrorCollection errors) { ArgumentValidation.CheckForNullReference(instance, "instance"); ArgumentValidation.CheckForNullReference(propertyInfo, "propertyInfo"); ArgumentValidation.CheckForNullReference(errors, "errors"); object propertyValue = propertyInfo.GetValue(instance, null); if (propertyValue is String) { string valueString = (string)propertyValue; if ((valueString == null) || (valueString.Trim().Length == 0)) { errors.Add(instance, propertyInfo.Name, SR.ExceptionValueNullMessage(propertyInfo.Name)); } } else if (!(propertyValue is ValueType)) { if (propertyValue == null) { errors.Add(instance, propertyInfo.Name, SR.ExceptionValueNullMessage(propertyInfo.Name)); } } }
/// <summary> /// <para>Adds a collection of <see cref="ValidationError"/> objects to the collection.</para> /// </summary> /// <param name="validationErrors"> /// <para>The collection of <see cref="ValidationError"/> objects to the collection.</para> /// </param> public void AddRange(ValidationErrorCollection validationErrors) { InnerList.AddRange(validationErrors); }
/// <summary> /// <para>Validate the ranige data for the given <paramref name="instance"/> and the <paramref name="propertyInfo"/>.</para> /// </summary> /// <param name="instance"> /// <para>The instance to validate.</para> /// </param> /// <param name="propertyInfo"> /// <para>The property contaning the value to validate.</para> /// </param> /// <param name="errors"> /// <para>The collection to add any errors that occur durring the validation.</para> /// </param> public override void Validate(object instance, PropertyInfo propertyInfo, ValidationErrorCollection errors) { base.Validate(instance, propertyInfo, errors); if (errors.Count == 0) { string name = propertyInfo.Name; errors.Add(instance, name, SR.ValueOutsideRangeErrorMessage(name)); } }
/// <summary> /// <para>Adds a collection of <see cref="ValidationError"/> objects to the collection.</para> /// </summary> /// <param name="validationErrors"> /// <para>The collection of <see cref="ValidationError"/> objects to the collection.</para> /// </param> public void AddRange(ValidationErrorCollection validationErrors) { InnerList.AddRange(validationErrors); }
private void ValidateProperties(ConfigurationNode node, PropertyInfo[] properties, ValidationErrorCollection errors) { foreach (PropertyInfo property in properties) { ValidationAttribute[] validationAttributes = (ValidationAttribute[])property.GetCustomAttributes(typeof(ValidationAttribute), true); foreach (ValidationAttribute validationAttribute in validationAttributes) { if (validationAttribute.IsActive) { validationAttribute.Validate(node, property, errors); } } } }
private void ValidateChildNodeProperties(ConfigurationNode node, ValidationErrorCollection errors) { foreach (ConfigurationNode childNode in node.Nodes) { Validate(childNode, errors); } }
private void Validate(ConfigurationNode node, ValidationErrorCollection errors) { Type t = node.GetType(); PropertyInfo[] properties = t.GetProperties(BindingFlags.Public | BindingFlags.Instance); ValidateProperties(node, properties, errors); ValidateChildNodeProperties(node, errors); }
/// <summary> /// <para> /// Executes the validation for the current node and all the child nodes. /// </para> /// </summary> /// <param name="node"> /// <para>The <see cref="ConfigurationNode"/> to validate.</para> /// </param> protected override void ExecuteCore(ConfigurationNode node) { ValidationErrorCollection errors = new ValidationErrorCollection(); Validate(node, errors); if (errors.Count > 0) { foreach(ValidationError error in errors) { ConfigurationErrorLogService.LogError(error); } } if (ConfigurationErrorLogService.ValidationErrors.Count > 0) { if (ConfigurationErrorLogService.ValidationErrors.Count > 0) { UIService.DisplayErrorLog(ConfigurationErrorLogService); } if (reportErrorsOnFailure) { UIService.ShowMessage(SR.ValidationErrorsMessage, SR.ValidationCaption); } ConfigurationErrorLogService.ClearErrorLog(); validationSucceeded = false; } else { validationSucceeded = true; } }
internal ReadOnlyValidationErrorCollection(Validation.ValidationErrorCollection validationErrorCollection) { this.validationErrorCollection = validationErrorCollection; }
private void DetermineIfCanWriteFile(string fileName, ValidationErrorCollection errors, object instance, PropertyInfo info) { if (File.Exists(fileName)) { try { using(File.OpenWrite(fileName)) {} } catch (IOException e) { errors.Add(new ValidationError(instance, info.Name, e.Message)); } catch (UnauthorizedAccessException e) { errors.Add(new ValidationError(instance, info.Name, e.Message)); } catch (ArgumentException e) { errors.Add(new ValidationError(instance, info.Name, e.Message)); } } }
/// <summary> /// <para>Validate the ranige data for the given <paramref name="instance"/> and the <paramref name="propertyInfo"/>.</para> /// </summary> /// <param name="instance"> /// <para>The instance to validate.</para> /// </param> /// <param name="propertyInfo"> /// <para>The property contaning the value to validate.</para> /// </param> /// <param name="errors"> /// <para>The collection to add any errors that occur durring the validation.</para> /// </param> public abstract void Validate(object instance, PropertyInfo propertyInfo, ValidationErrorCollection errors);
public ConfigurationErrorLogService() { configurationErrorCollection = new ConfigurationErrorCollection(); validationErrorCollection = new ValidationErrorCollection(); }
public override void Validate(object instance, PropertyInfo propertyInfo, ValidationErrorCollection errors) { ArgumentValidation.CheckForNullReference(instance, "instance"); ArgumentValidation.CheckForNullReference(propertyInfo, "propertyInfo"); ArgumentValidation.CheckForNullReference(errors, "errors"); object propertyValue = propertyInfo.GetValue(instance, null); Regex expression = null; if (this.compiledRegexType != null) { if (this.optionsSpecified) { expression = (Regex)Activator.CreateInstance(this.compiledRegexType, new object[] {this.options}); } else { expression = (Regex)Activator.CreateInstance(this.compiledRegexType); } } else if (this.optionsSpecified) { expression = new Regex(this.pattern, this.options); } else { expression = new Regex(this.pattern); } if (!expression.IsMatch((string)propertyValue)) { errors.Add(instance, propertyInfo.Name, SR.RegExErrorMessage(propertyValue.ToString())); } }
/// <summary> /// <para>Validate the ranige data for the given <paramref name="instance"/> and the <paramref name="propertyInfo"/>.</para> /// </summary> /// <param name="instance"> /// <para>The instance to validate.</para> /// </param> /// <param name="propertyInfo"> /// <para>The property contaning the value to validate.</para> /// </param> /// <param name="errors"> /// <para>The collection to add any errors that occur durring the validation.</para> /// </param> public override void Validate(object instance, PropertyInfo propertyInfo, ValidationErrorCollection errors) { ArgumentValidation.CheckForNullReference(instance, "instance"); ArgumentValidation.CheckForNullReference(propertyInfo, "propertyInfo"); ArgumentValidation.CheckForNullReference(errors, "errors"); object propertyValue = propertyInfo.GetValue(instance, null); if (propertyValue == null) { return; } IComparable compareToObject = (IComparable)propertyValue; int lowerBoundGreaterThanValue = this.lowerBound.CompareTo(compareToObject); int upperBoundLessThanValue = this.upperBound.CompareTo(compareToObject); if ((lowerBoundGreaterThanValue == 0) && (this.lowerBoundType == RangeBoundaryType.Exclusive)) { errors.Add(instance, propertyInfo.Name, SR.ValueNotInRangeErrorMessage(propertyInfo.Name)); } if (lowerBoundGreaterThanValue > 0) { errors.Add(instance, propertyInfo.Name, SR.ValueNotInRangeErrorMessage(propertyInfo.Name)); } if ((upperBoundLessThanValue == 0) && (this.upperBoundType == RangeBoundaryType.Exclusive)) { errors.Add(instance, propertyInfo.Name, SR.ValueNotInRangeErrorMessage(propertyInfo.Name)); } if (upperBoundLessThanValue < 0) { errors.Add(instance, propertyInfo.Name, SR.ValueNotInRangeErrorMessage(propertyInfo.Name)); } }
/// <summary> /// <para>Validate the ranige data for the given <paramref name="instance"/> and the <paramref name="propertyInfo"/>.</para> /// </summary> /// <param name="instance"> /// <para>The instance to validate.</para> /// </param> /// <param name="propertyInfo"> /// <para>The property contaning the value to validate.</para> /// </param> /// <param name="errors"> /// <para>The collection to add any errors that occur durring the validation.</para> /// </param> public abstract void Validate(object instance, PropertyInfo propertyInfo, ValidationErrorCollection errors);
internal ReadOnlyValidationErrorCollection(Validation.ValidationErrorCollection validationErrorCollection) { this.validationErrorCollection = validationErrorCollection; }