Esempio n. 1
0
        private void ConfigureValidator(IValidatorConfiguration validatorConfiguration, IValidatorAttribute attribute)
        {
            Type attributeType = attribute.GetType();

            if (attributeType.IsSubclassOf(_MessageValidatorAttributeType))
            {
                MessageValidatorAttribute message = (MessageValidatorAttribute)attribute;
                validatorConfiguration.ErrorMessageLabel  = message.ErrorMessageLabel;
                validatorConfiguration.CustomErrorMessage = message.CustomErrorMessage;
            }
            else if (attributeType.IsSubclassOf(_TypeValidatorAttributeType))
            {
                TypeValidatorAttribute type = (TypeValidatorAttribute)attribute;
                validatorConfiguration.IsNullable = type.IsNullable;
            }
            else if (attributeType.IsSubclassOf(_TestValidatorAttributeType))
            {
                TestValidatorAttribute test = (TestValidatorAttribute)attribute;
                validatorConfiguration.AddTestValidator(test.Create());
            }
            else
            {
                throw new InvalidProgramException("The validator attribute must be of the types: MessageValidatorAttribute, TypeValidatorValidator or TestValidatorAttribute");
            }
        }
Esempio n. 2
0
        public ObjectValidator(Type objType)
        {
            // verify if the type is nullable
            new Validator <Type>("object type")
            .SetUpNullable(false)
            .Validate(objType);

            foreach (var propertyInfo in objType.GetProperties())
            {
                var validatorAttributes = propertyInfo.GetCustomAttributes(typeof(IValidatorAttribute), true);
                if (validatorAttributes.Length == 0)
                {
                    CreateValidator(propertyInfo);
                }
                else
                {
                    IValidatorConfiguration validatorConfiguration = CreateValidator(propertyInfo);
                    foreach (IValidatorAttribute attribute in validatorAttributes)
                    {
                        ConfigureValidator(validatorConfiguration, attribute);
                    }
                }
            }
            this.objType = objType;
        }
Esempio n. 3
0
        /// <summary>
        /// Create a new instance of a <see cref="Validator´1"/> extract from the <see cref="PropertyInfo"/>
        /// </summary>
        /// <param name="propertyInfo"></param>
        /// <returns></returns>
        private IValidatorConfiguration CreateValidator(PropertyInfo propertyInfo)
        {
            Type validatorType = _ValidatorBaseGenericType.MakeGenericType(propertyInfo.PropertyType);
            IValidatorConfiguration validatorConfiguration = (IValidatorConfiguration)Activator.CreateInstance(validatorType);

            // TODO: Change default name to a more smart rule for extract of the property name
            validatorConfiguration.ErrorMessageLabel = propertyInfo.Name.ToLower();
            validationsByPropertyInfo.Add(propertyInfo, (IValidation)validatorConfiguration);
            validationsByName.Add(propertyInfo.Name, (IValidation)validatorConfiguration);
            return(validatorConfiguration);
        }
 /// <inheritdoc />
 public PersonValidator(
     IEnumerable <IValidationRule <Person> > rules,
     IValidatorConfiguration <Person> configuration)
     : base(rules, configuration)
 {
 }