private void ValidateValueTypes(ModelBindingContext bindingContext, Dictionary <string, object> propertyValuePaires)
        {
            var errors = new Dictionary <string, string>();

            // Validate if the property value pairs passed maches the type.
            var typeValidator = new TypeValidator <T>();

            if (!typeValidator.IsValid(propertyValuePaires))
            {
                foreach (var invalidProperty in typeValidator.InvalidProperties)
                {
                    var key = string.Format(_localizationService.GetResource("Api.InvalidType", _languageId, false), invalidProperty);

                    if (!errors.ContainsKey(key))
                    {
                        errors.Add(key, _localizationService.GetResource("Api.InvalidPropertyType", _languageId, false));
                    }
                }
            }

            if (errors.Count > 0)
            {
                foreach (var error in errors)
                {
                    bindingContext.ModelState.AddModelError(error.Key, error.Value);
                }
            }
        }
예제 #2
0
        private void ValidateValueTypes(ModelBindingContext bindingContext, Dictionary <string, object> propertyValuePaires)
        {
            var errors = new Dictionary <string, string>();

            // Validate if the property value pairs passed matches the type.
            var typeValidator = new TypeValidator <T>();

            if (!typeValidator.IsValid(propertyValuePaires))
            {
                foreach (var invalidProperty in typeValidator.InvalidProperties)
                {
                    var key = $"Invalid {invalidProperty} type";
                    if (!errors.ContainsKey(key))
                    {
                        errors.Add(key, "Invalid Property Type");
                    }
                }
            }

            if (errors.Count > 0)
            {
                foreach (var error in errors)
                {
                    bindingContext.ModelState.AddModelError(error.Key, error.Value);
                }
            }
        }
예제 #3
0
        //private Type validator;
        //public TypeValidationAttribute(Type propertyClassType) { 

        //     if (propertyClassType == null)
        //    {
        //        throw new ArgumentNullException("propertyClassType");
        //    }
        //    if (!typeof (CoinValidatorBase).IsAssignableFrom(validator))
        //    {
        //        throw new ArgumentException("Validator Attribute param not validator");
        //    }
        //    type = propertyClassType;

        /// <summary>
        /// Determines whether the specified value is valid.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns><c>true</c> if the specified value is valid; otherwise, <c>false</c>.</returns>
        public override bool IsValid(object value)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
            object originalValue = properties.Find("Attendee", true /* ignoreCase */).GetValue(value);
           // object confirmValue = properties.Find(ConfirmProperty, true /* ignoreCase */).GetValue(value);
            return false;
            if (AcceptedTypes == null) AcceptedTypes = new List<Type>();
            if (AcceptedType1 != null) AcceptedTypes.Add(AcceptedType1);
            if (AcceptedType2 != null) AcceptedTypes.Add(AcceptedType2);
            var validator = new TypeValidator(AcceptedTypes);
            return validator.IsValid(value.GetType());
        }
예제 #4
0
        public void Should_validate_decimal_properly_when_current_culture_uses_comma_as_decimal_point()
        {
            Dictionary <string, object> properties = new Dictionary <string, object>();

            properties.Add("decimal_number", 33.33);

            var validator = new TypeValidator <TestModelDto>();

            bool result = validator.IsValid(properties);

            Assert.IsTrue(result);
        }
        public void WhenCurrentCultureUsesCommaAsDecimalPoint_ShouldProperlyValidateProductPrice()
        {
            //Arange
            Dictionary <string, object> properties = new Dictionary <string, object>();

            properties.Add("price", 33.33);

            var cut = new TypeValidator <ProductDto>();

            //Act
            bool result = cut.IsValid(properties);

            // Assert
            Assert.IsTrue(result);
        }
예제 #6
0
        private async Task ValidateValueTypesAsync(ModelBindingContext bindingContext, Dictionary <string, object> propertyValuePairs)
        {
            var errors = new Dictionary <string, string>();

            // Validate if the property value pairs passed maches the type.
            var typeValidator = new TypeValidator <T>();

            if (!typeValidator.IsValid(propertyValuePairs))
            {
                int languageId;
                // Languages are ordered by display order so the first language will be with the smallest display order.
                var firstLanguage = (await _languageService.GetAllLanguagesAsync()).FirstOrDefault();
                if (firstLanguage != null)
                {
                    languageId = firstLanguage.Id;
                }
                else
                {
                    languageId = 0;
                }

                string invalidTypeText = await _localizationService.GetResourceAsync("Api.InvalidType", languageId, false);

                string invalidPropertyTypeText = await _localizationService.GetResourceAsync("Api.InvalidPropertyType", languageId, false);

                foreach (var invalidProperty in typeValidator.InvalidProperties)
                {
                    var key = string.Format(invalidTypeText, invalidProperty);

                    if (!errors.ContainsKey(key))
                    {
                        errors.Add(key, invalidPropertyTypeText);
                    }
                }
            }

            if (errors.Count > 0)
            {
                foreach (var error in errors)
                {
                    bindingContext.ModelState.AddModelError(error.Key, error.Value);
                }
            }
        }