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) { base.Validate(instance, propertyInfo, errors); if (errors.Count == 0) { string name = propertyInfo.Name; errors.Add(instance, name, SR.ValueOutsideRangeErrorMessage(name)); } }
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) {} } } }
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); string name = null; if (propertyValue == null) { name = propertyInfo.Name; errors.Add(instance, propertyInfo.Name, SR.MinLengthExceededErrorMessage(name, minimumLength)); } if (propertyValue is String) { string valueString = (string)propertyValue; if ((valueString == null) || (valueString.Length == 0) || (valueString.Length < minimumLength)) { name = propertyInfo.Name; errors.Add(instance, name, SR.MinLengthExceededErrorMessage(name, minimumLength)); } } }
/// <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>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 name = null; if (propertyValue == null) { name = propertyInfo.Name; errors.Add(instance, propertyInfo.Name, SR.MinLengthExceededErrorMessage(name, minimumLength)); } if (propertyValue is String) { string valueString = (string)propertyValue; if ((valueString == null) || (valueString.Length == 0) || (valueString.Length < minimumLength)) { name = propertyInfo.Name; errors.Add(instance, name, SR.MinLengthExceededErrorMessage(name, minimumLength)); } } }
/// <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)); } }
/// <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); } } }
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 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) {} } } }
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)); } } }
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 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)); } }