public void CreateInstanceForProxyWithEmptyValidatedPropertyNameThrows()
 {
     object value = new object();
     MockIntegrationProxy integrationProxy = new MockIntegrationProxy(value,
                                                                      "",
                                                                      ValidationSpecificationSource.Attributes,
                                                                      "",
                                                                      typeof(MockValidatedType));
     ValidationIntegrationHelper integrationHelper = new ValidationIntegrationHelper(integrationProxy);
 }
 public void CreateInstanceForProxyWithNullValidatedTypeThrows()
 {
     object value = new object();
     MockIntegrationProxy integrationProxy = new MockIntegrationProxy(value,
                                                                      "",
                                                                      ValidationSpecificationSource.Attributes,
                                                                      "ObjectProperty",
                                                                      null);
     ValidationIntegrationHelper integrationHelper = new ValidationIntegrationHelper(integrationProxy);
 }
        private void OnCellValidating(object source, DataGridViewCellValidatingEventArgs e) {
            var row = Grid.Rows[e.RowIndex];
            
            if (row.IsNewRow) {
                return;
            }

            var validationProxy = new GridValidationIntegrationProxy(Grid.Columns[e.ColumnIndex].DataPropertyName, e.FormattedValue, this);

            ValidationIntegrationHelper helper;

            try {
                helper = new ValidationIntegrationHelper(validationProxy);
            } catch (InvalidOperationException) {
                // ignore missing property
                return;
            }

            var validator = helper.GetValidator();
            
            if(validator == null) {
                // this property should not be checked
                return;
            }

            var validationResults = validator.Validate(validationProxy);

            if (!validationResults.IsValid) {
                var builder = new StringBuilder();
                
                foreach (ValidationResult result in validationResults) {
                    builder.AppendLine(result.Message);
                }

                row.Cells[e.ColumnIndex].ErrorText = builder.ToString();
            } else {
                row.Cells[e.ColumnIndex].ErrorText = string.Empty;
            }
        }
        public void CanBuildValidatorForProperty()
        {
            object value = new object();
            MockIntegrationProxy integrationProxy = new MockIntegrationProxy(value,
                                                                             "",
                                                                             ValidationSpecificationSource.Attributes,
                                                                             "ObjectProperty",
                                                                             typeof(MockValidatedType));
            integrationProxy.ValueConvertEvent += OnValueConvertWithFailure;
            ValidationIntegrationHelper integrationHelper = new ValidationIntegrationHelper(integrationProxy);

            Validator validator = integrationHelper.GetValidator();
            //Assert.IsNotNull(validator);

            //MockValidatedType instance = new MockValidatedType();
            //ValidationResults validationResults = validator.Validate(instance);
            //Assert.IsFalse(validationResults.IsValid);
            //IList<ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);
            //Assert.AreEqual(1, resultsList.Count);
            //Assert.AreEqual("object property message", resultsList[0].Message);
        }
        public void GetValueConvertedWithDefaultConversionReturnsFailureIfConversionIsNotPossible()
        {
            object value = "00012345abc";
            MockIntegrationProxy integrationProxy = new MockIntegrationProxy(value,
                                                                             "",
                                                                             ValidationSpecificationSource.Attributes,
                                                                             "IntProperty",
                                                                             typeof(MockValidatedType));
            ValidationIntegrationHelper integrationHelper = new ValidationIntegrationHelper(integrationProxy);

            object retrievedValue;
            string valueRetrievalFailureMessage;

            bool status = integrationHelper.GetValue(out retrievedValue, out valueRetrievalFailureMessage);

            Assert.IsFalse(status);
            Assert.AreEqual(null, retrievedValue);
            Assert.IsTrue(TemplateStringTester.IsMatch(Resources.ErrorCannotPerfomDefaultConversion, valueRetrievalFailureMessage));
            Assert.AreEqual(null, originalConvertedValue);
            Assert.AreEqual(null, valueToConvert);
        }
        public void CanGetValueConvertedWithDefaultConversionFromNullValueIfNoEventHandlerSet()
        {
            object value = null;
            MockIntegrationProxy integrationProxy = new MockIntegrationProxy(value,
                                                                             "",
                                                                             ValidationSpecificationSource.Attributes,
                                                                             "ObjectProperty",
                                                                             typeof(MockValidatedType));
            ValidationIntegrationHelper integrationHelper = new ValidationIntegrationHelper(integrationProxy);

            object retrievedValue;
            string valueRetrievalFailureMessage;

            bool status = integrationHelper.GetValue(out retrievedValue, out valueRetrievalFailureMessage);

            Assert.IsTrue(status);
            Assert.AreEqual(null, retrievedValue);
            Assert.AreEqual(null, valueRetrievalFailureMessage);
            Assert.AreEqual(null, originalConvertedValue);
            Assert.AreEqual(null, valueToConvert);
        }
        public void GetValueConvertedThroughEventSettingFailureMessageFromIntegrationProxyReturnsFailure()
        {
            object value = new object();
            MockIntegrationProxy integrationProxy = new MockIntegrationProxy(value,
                                                                             "",
                                                                             ValidationSpecificationSource.Attributes,
                                                                             "ObjectProperty",
                                                                             typeof(MockValidatedType));
            integrationProxy.ValueConvertEvent += OnValueConvertWithFailure;
            ValidationIntegrationHelper integrationHelper = new ValidationIntegrationHelper(integrationProxy);

            object retrievedValue;
            string valueRetrievalFailureMessage;

            bool status = integrationHelper.GetValue(out retrievedValue, out valueRetrievalFailureMessage);

            Assert.IsFalse(status);
            Assert.AreEqual(null, retrievedValue);
            Assert.AreEqual(conversionErrorMessage, valueRetrievalFailureMessage);
            Assert.AreSame(value, originalConvertedValue);
            Assert.AreSame(value, valueToConvert);
        }
        public void CanGetObjectValueFromIntegrationProxy()
        {
            object value = new object();
            MockIntegrationProxy integrationProxy = new MockIntegrationProxy(value,
                                                                             "",
                                                                             ValidationSpecificationSource.Attributes,
                                                                             "ObjectProperty",
                                                                             typeof(MockValidatedType));
            ValidationIntegrationHelper integrationHelper = new ValidationIntegrationHelper(integrationProxy);

            object retrievedValue;
            string valueRetrievalFailureMessage;

            bool status = integrationHelper.GetValue(out retrievedValue, out valueRetrievalFailureMessage);

            Assert.IsTrue(status);
            Assert.AreSame(value, retrievedValue);
            Assert.AreEqual(null, valueRetrievalFailureMessage);
        }
Пример #9
0
        /// <summary>
        /// Does the validate.
        /// </summary>
        /// <param name="objectToValidate">The object to validate.</param>
        /// <param name="currentTarget">The current target.</param>
        /// <param name="key">The key.</param>
        /// <param name="validationResults">The validation results.</param>
		protected override void DoValidate(object objectToValidate, object currentTarget, string key, ValidationResults validationResults)
		{
			Validator validator = new ValidationIntegrationHelper(this).GetValidator();

			if (validator != null)
			{
				ValidationResults results = validator.Validate(this);

				if (!results.IsValid)
				{
					validationResults.AddAllResults(results);
				}
			}
		}
Пример #10
0
		/// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="valueAccessFailureMessage">The value access failure message.</param>
        /// <returns></returns>
		internal bool GetValue(out object value, out string valueAccessFailureMessage)
		{
			ValidationIntegrationHelper helper = new ValidationIntegrationHelper(this);

			return helper.GetValue(out value, out valueAccessFailureMessage);
		}
Пример #11
0
        /// <summary>
        /// Validate property.
        /// </summary>
        /// <param name="propName">Property name.</param>
        /// <returns>Error string or null.</returns>
        private string _ValidateProperty(string propName)
        {
            string error = null;

            if (_CanValidate(propName))
            {
                _validationProxy.ValidatedPropertyName = propName;

                ValidationIntegrationHelper hlp = new ValidationIntegrationHelper(
                    _validationProxy);

                Validator validator = hlp.GetValidator();
                ValidationResults res = validator.Validate(this);
                if (!res.IsValid)
                    error = _FindError(res, propName);
            }

            return error;
        }
Пример #12
0
        /// <summary>
        /// Creates validators for objects of the specified type and it's properties.
        /// </summary>
        /// <param name="objectType">The type of objects to create validators for.</param>
        /// <returns>An instance of the validators container with validators for the specified
        /// type.</returns>
        private static ValidatorsContainer _CreateValidators(Type objectType)
        {
            Debug.Assert(objectType != null);

            var propertyFlags =
                BindingFlags.Instance |
                BindingFlags.Public;
            var validateableProperties =
                from propertyInfo in objectType.GetProperties(propertyFlags)
                where propertyInfo.GetCustomAttributes<BaseValidationAttribute>().Any()
                select propertyInfo;

            var propertyValidators = new Dictionary<string, Validator>();
            foreach (var propertyInfo in validateableProperties)
            {
                var validationProxy = new PropertyValidationProxy
                {
                    ProvidesCustomValueConversion = false,
                    SpecificationSource = ValidationSpecificationSource.Attributes,
                    ValidatedType = objectType,
                    Ruleset = string.Empty,
                    ValidatedPropertyName = propertyInfo.Name,
                };

                var validationHelper = new ValidationIntegrationHelper(validationProxy);
                var validator = validationHelper.GetValidator();

                propertyValidators.Add(propertyInfo.Name, validator);
            }

            var objectValidator = ValidationFactory.CreateValidator(objectType);

            return new ValidatorsContainer
            {
                ObjectValidator = objectValidator,
                PropertyValidators = propertyValidators,
            };
        }
        internal bool GetValue(out object value, out string valueAccessFailureMessage)
        {
            var helper = new ValidationIntegrationHelper(this);

            bool result;
            try
            {
                result = helper.GetValue(out value, out valueAccessFailureMessage);
            }
            catch
            {
                result = false;
                value = null;
                valueAccessFailureMessage = String.Empty;
            }

            return result;
        }
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            this.value = value;

            Validator validator = new ValidationIntegrationHelper(this).GetValidator();

            if (validator != null)
            {
                ValidationResults validationResults = validator.Validate(this);

                string errorMessage = FormatErrorMessage(validationResults);
                return new ValidationResult(validationResults.IsValid, errorMessage);
            }
            else
            {
                return new ValidationResult(true, null);
            }
        }