Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
        /// <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));
                }
            }
        }
Exemplo n.º 3
0
        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));
            }
        }
Exemplo n.º 4
0
        /// <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));
                }
            }
        }
Exemplo n.º 5
0
 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) {}
         }
     }
 }
Exemplo n.º 6
0
        /// <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);
        }
Exemplo n.º 7
0
 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) {}
         }
     }
 }
Exemplo n.º 8
0
 /// <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);
         }
     }
 }
Exemplo n.º 9
0
        /// <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));
                }
            }
        }
Exemplo n.º 10
0
        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));
            }
        }
Exemplo n.º 11
0
 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));
         }
     }
 }
Exemplo n.º 12
0
        /// <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));
                }
            }
        }
Exemplo n.º 13
0
 /// <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);
 }
Exemplo n.º 14
0
 /// <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));
     }
 }
Exemplo n.º 15
0
 /// <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);
 }
Exemplo n.º 16
0
 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);
             }
         }
     }
 }
Exemplo n.º 17
0
 private void ValidateChildNodeProperties(ConfigurationNode node, ValidationErrorCollection errors)
 {
     foreach (ConfigurationNode childNode in node.Nodes)
     {
         Validate(childNode, errors);
     }
 }
Exemplo n.º 18
0
 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);
 }
Exemplo n.º 19
0
        /// <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;
 }
Exemplo n.º 21
0
 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));
         }
     }
 }
Exemplo n.º 22
0
 /// <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);
Exemplo n.º 23
0
 public ConfigurationErrorLogService()
 {
     configurationErrorCollection = new ConfigurationErrorCollection();
     validationErrorCollection = new ValidationErrorCollection();
 }
Exemplo n.º 24
0
        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()));
            }
        }
Exemplo n.º 25
0
        /// <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));
            }
        }
Exemplo n.º 26
0
 /// <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;
 }