/// <summary>
        /// Gets the name of the transformed property.
        /// </summary>
        /// <param name="validationModelPropertyName">
        /// Name of the validation model property.
        /// </param>
        /// <param name="validationModelType">
        /// The validation Model Type.
        /// </param>
        /// <returns>
        /// The transformed property name.
        /// </returns>
        private string GetTransformedPropertyName(string validationModelPropertyName, Type validationModelType)
        {
            if (m_ValidationTransformerManager == null)
            {
                return(null);
            }

            if (validationModelType == null)
            {
                return(null);
            }

            IValidationTransformer validationTransformerForValidationModel = m_ValidationTransformerManager.GetValidationTransformerForValidationModel(validationModelType);

            if (validationTransformerForValidationModel == null)
            {
                return(null);
            }

            MappingMemberInfo mappingMemberInfo = validationTransformerForValidationModel.TransformPropertyNameFromValidationModel(validationModelPropertyName);

            if (mappingMemberInfo != null)
            {
                return(mappingMemberInfo.PropertyName);
            }

            return(null);
        }
        /// <summary>
        /// Gets the name of the property.
        /// </summary>
        /// <param name="validationTransformer">The validation transformer.</param>
        /// <param name="defaultPropertyName">Default name of the property.</param>
        /// <returns>The property name.</returns>
        public static string GetPropertyName(IValidationTransformer validationTransformer, string defaultPropertyName)
        {
            if (validationTransformer == null)
            {
                return defaultPropertyName;
            }

            MappingMemberInfo mappingMemberInfo = validationTransformer.TransformPropertyNameFromUIModel(defaultPropertyName);
            return mappingMemberInfo == null ? defaultPropertyName : mappingMemberInfo.PropertyName;
        }
        /// <summary>
        /// Registers the validation transformer.
        /// </summary>
        /// <param name="validationTransformer">The validation transformer.</param>
        public void RegisterValidationTransformer(IValidationTransformer validationTransformer)
        {
            if (validationTransformer == null)
            {
                throw new ArgumentNullException("validationTransformer");
            }

            m_ValidationTransformersForUIModels.Add(validationTransformer.UIModelType, validationTransformer);
            m_ValidationTransformersForValidationModels.Add(validationTransformer.ValidationModelType, validationTransformer);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="LaboModelValidator"/> class.
        /// </summary>
        /// <param name="metadata">The metadata.</param>
        /// <param name="controllerContext">The controller context.</param>
        /// <param name="validator">The validator.</param>
        /// <param name="validationTransformer">The validation transformer.</param>
        /// <exception cref="System.ArgumentNullException">validator</exception>
        public LaboModelValidator(ModelMetadata metadata, ControllerContext controllerContext, IEntityValidator validator, IValidationTransformer validationTransformer = null)
            : base(metadata, controllerContext)
        {
            if (validator == null)
            {
                throw new ArgumentNullException("validator");
            }

            m_Validator = validator;
            m_ValidationTransformer = validationTransformer;
        }
Esempio n. 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LaboModelValidator"/> class.
        /// </summary>
        /// <param name="metadata">The metadata.</param>
        /// <param name="controllerContext">The controller context.</param>
        /// <param name="validator">The validator.</param>
        /// <param name="validationTransformer">The validation transformer.</param>
        /// <exception cref="System.ArgumentNullException">validator</exception>
        public LaboModelValidator(ModelMetadata metadata, ControllerContext controllerContext, IEntityValidator validator, IValidationTransformer validationTransformer = null)
            : base(metadata, controllerContext)
        {
            if (validator == null)
            {
                throw new ArgumentNullException("validator");
            }

            m_Validator             = validator;
            m_ValidationTransformer = validationTransformer;
        }
Esempio n. 6
0
        /// <summary>
        /// Gets the name of the property.
        /// </summary>
        /// <param name="validationTransformer">The validation transformer.</param>
        /// <param name="defaultPropertyName">Default name of the property.</param>
        /// <returns>The property name.</returns>
        public static string GetPropertyName(IValidationTransformer validationTransformer, string defaultPropertyName)
        {
            if (validationTransformer == null)
            {
                return(defaultPropertyName);
            }

            MappingMemberInfo mappingMemberInfo = validationTransformer.TransformPropertyNameFromUIModel(defaultPropertyName);

            return(mappingMemberInfo == null ? defaultPropertyName : mappingMemberInfo.PropertyName);
        }
Esempio n. 7
0
        public void GetValidationTransformerForModelShouldReturnNullWhenNoTransformerIsFoundForTheSpecifiedType()
        {
            IValidationTransformer validationTransformer = Substitute.For <IValidationTransformer>();

            validationTransformer.UIModelType.Returns(typeof(string));
            validationTransformer.ValidationModelType.Returns(typeof(int));

            DefaultValidationTransformerManager validationTransformerManager = new DefaultValidationTransformerManager();

            validationTransformerManager.RegisterValidationTransformer(validationTransformer);

            Assert.IsNull(validationTransformerManager.GetValidationTransformerForUIModel(typeof(int)));
        }
        /// <summary>
        /// Gets the member information.
        /// </summary>
        /// <returns>The member info.</returns>
        private MemberInfo GetMemberInfo()
        {
            IValidationTransformerManager validationTransformerManager = ValidatorSettings.ValidationTransformerManager;
            IValidationTransformer        validationTransformer        = validationTransformerManager.GetValidationTransformerForValidationModel(typeof(TEntity));
            MemberInfo memberInfo;

            if (validationTransformer == null)
            {
                memberInfo = MemberInfo;
            }
            else
            {
                MappingMemberInfo mappingMemberInfo = validationTransformer.TransformPropertyNameFromValidationModel(MemberName);
                memberInfo = mappingMemberInfo == null ? MemberInfo : mappingMemberInfo.MemberInfo;
            }

            return(memberInfo);
        }
        /// <summary>
        /// Gets a list of validators.
        /// </summary>
        /// <returns>
        /// A list of validators.
        /// </returns>
        /// <param name="metadata">The metadata.</param><param name="context">The context.</param>
        public override IEnumerable <ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context)
        {
            if (metadata == null)
            {
                throw new ArgumentNullException("metadata");
            }

            bool isModelProperty = ModelMetadataHelper.IsModelProperty(metadata);

            Type modelType = isModelProperty ? metadata.ContainerType : metadata.ModelType;
            IValidationTransformer validationTransformer = GetValidationTransformer(modelType);
            IEntityValidator       validator             = ValidatorFactory.GetValidatorForOptional(validationTransformer == null ? modelType : validationTransformer.ValidationModelType);

            if (isModelProperty)
            {
                return(GetValidatorsForProperty(metadata, context, validator, validationTransformer));
            }

            return(GetValidatorsForModel(metadata, context, validator, validationTransformer));
        }
 /// <summary>
 /// Gets the validators for model.
 /// </summary>
 /// <param name="metadata">The metadata.</param>
 /// <param name="context">The context.</param>
 /// <param name="validator">The validator.</param>
 /// <param name="validationTransformer">The validation transformer.</param>
 /// <returns>The model validators.</returns>
 private static IEnumerable<ModelValidator> GetValidatorsForModel(ModelMetadata metadata, ControllerContext context, IEntityValidator validator, IValidationTransformer validationTransformer)
 {
     if (validator != null)
     {
         yield return new LaboModelValidator(metadata, context, validator, validationTransformer);
     }
 }
        /// <summary>
        /// Gets the validators for property.
        /// </summary>
        /// <param name="metadata">The metadata.</param>
        /// <param name="context">The context.</param>
        /// <param name="validator">The validator.</param>
        /// <param name="validationTransformer">The validation transformer.</param>
        /// <returns>The validators.</returns>
        private IEnumerable<ModelValidator> GetValidatorsForProperty(ModelMetadata metadata, ControllerContext context, IEntityValidator validator, IValidationTransformer validationTransformer)
        {
            List<ModelValidator> modelValidators = new List<ModelValidator>();

            if (validator != null)
            {
                IList<IEntityValidationRule> validationRules = validator.ValidationRules;

                HashSet<string> requiredPropertyValidators = new HashSet<string>(StringComparer.OrdinalIgnoreCase); 

                for (int i = 0; i < validationRules.Count; i++)
                {
                    IEntityValidationRule entityValidationRule = validationRules[i];

                    string propertyName = ValidationTransformerHelper.GetPropertyName(metadata, validationTransformer);
                    if (entityValidationRule.MemberInfo.Name == propertyName)
                    {
                        ModelValidator modelValidator = GetModelPropertyValidator(metadata, context, entityValidationRule);
                        if (modelValidator != null)
                        {
                            if (modelValidator.IsRequired)
                            {
                                // There can be one required validator per property
                                if (!requiredPropertyValidators.Contains(propertyName))
                                {
                                    requiredPropertyValidators.Add(propertyName);
                                    modelValidators.Add(modelValidator); 
                                }
                            }
                            else
                            {
                                modelValidators.Add(modelValidator);                                
                            }
                        }
                    }
                }

                if (metadata.IsRequired && AddImplicitRequiredAttributeForValueTypes)
                {
                    bool hasRequiredValidators = modelValidators.Any(x => x.IsRequired);

                    if (!hasRequiredValidators)
                    {
                        modelValidators.Add(CreateNotNullValidatorForProperty(metadata, context));
                    }
                }
            }

            return modelValidators;
        }
 /// <summary>
 /// Gets the name of the property.
 /// </summary>
 /// <param name="metadata">The metadata.</param>
 /// <param name="validationTransformer">The validation transformer.</param>
 /// <returns>The property name.</returns>
 public static string GetPropertyName(ModelMetadata metadata, IValidationTransformer validationTransformer)
 {
     string propertyName = metadata.PropertyName;
     return GetPropertyName(validationTransformer, propertyName);
 }
Esempio n. 13
0
        /// <summary>
        /// Gets the name of the property.
        /// </summary>
        /// <param name="metadata">The metadata.</param>
        /// <param name="validationTransformer">The validation transformer.</param>
        /// <returns>The property name.</returns>
        public static string GetPropertyName(ModelMetadata metadata, IValidationTransformer validationTransformer)
        {
            string propertyName = metadata.PropertyName;

            return(GetPropertyName(validationTransformer, propertyName));
        }
 /// <summary>
 /// Gets the validators for model.
 /// </summary>
 /// <param name="metadata">The metadata.</param>
 /// <param name="context">The context.</param>
 /// <param name="validator">The validator.</param>
 /// <param name="validationTransformer">The validation transformer.</param>
 /// <returns>The model validators.</returns>
 private static IEnumerable <ModelValidator> GetValidatorsForModel(ModelMetadata metadata, ControllerContext context, IEntityValidator validator, IValidationTransformer validationTransformer)
 {
     if (validator != null)
     {
         yield return(new LaboModelValidator(metadata, context, validator, validationTransformer));
     }
 }
        /// <summary>
        /// Gets the validators for property.
        /// </summary>
        /// <param name="metadata">The metadata.</param>
        /// <param name="context">The context.</param>
        /// <param name="validator">The validator.</param>
        /// <param name="validationTransformer">The validation transformer.</param>
        /// <returns>The validators.</returns>
        private IEnumerable <ModelValidator> GetValidatorsForProperty(ModelMetadata metadata, ControllerContext context, IEntityValidator validator, IValidationTransformer validationTransformer)
        {
            List <ModelValidator> modelValidators = new List <ModelValidator>();

            if (validator != null)
            {
                IList <IEntityValidationRule> validationRules = validator.ValidationRules;

                HashSet <string> requiredPropertyValidators = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

                for (int i = 0; i < validationRules.Count; i++)
                {
                    IEntityValidationRule entityValidationRule = validationRules[i];

                    string propertyName = ValidationTransformerHelper.GetPropertyName(metadata, validationTransformer);
                    if (entityValidationRule.MemberInfo.Name == propertyName)
                    {
                        ModelValidator modelValidator = GetModelPropertyValidator(metadata, context, entityValidationRule);
                        if (modelValidator != null)
                        {
                            if (modelValidator.IsRequired)
                            {
                                // There can be one required validator per property
                                if (!requiredPropertyValidators.Contains(propertyName))
                                {
                                    requiredPropertyValidators.Add(propertyName);
                                    modelValidators.Add(modelValidator);
                                }
                            }
                            else
                            {
                                modelValidators.Add(modelValidator);
                            }
                        }
                    }
                }

                if (metadata.IsRequired && AddImplicitRequiredAttributeForValueTypes)
                {
                    bool hasRequiredValidators = modelValidators.Any(x => x.IsRequired);

                    if (!hasRequiredValidators)
                    {
                        modelValidators.Add(CreateNotNullValidatorForProperty(metadata, context));
                    }
                }
            }

            return(modelValidators);
        }