Example #1
0
        public void OnlyAttributes()
        {
            var vc1 = new ValidatorClassAttribute(
                    "NHibernate.Validator.Tests.CustomValidator.IsOneValidator, NHibernate.Validator.Tests");

            Assert.IsNotNull(vc1.Value);

            var vc2 = new ValidatorClassAttribute(typeof(IsOneValidator));

            Assert.IsNotNull(vc1.Value);

            Assert.AreEqual(vc1.Value, vc2.Value);
        }
Example #2
0
        /// <summary>
        /// Create a <see cref="IValidator"/> from a <see cref="ValidatorClassAttribute"/> attribute.
        /// If the attribute is not a <see cref="ValidatorClassAttribute"/> type return null.
        /// </summary>
        /// <param name="attribute">attribute</param>
        /// <returns>the validator for the attribute</returns>
        private IValidator CreateOrGetValidator(Attribute attribute)
        {
            try
            {
                IValidator entityValidator;
                var        delegatedAttribute = attribute as IValidatorInstanceProvider;
                if (delegatedAttribute != null)
                {
                    entityValidator = delegatedAttribute.Validator;
                }
                else
                {
                    entityValidator = attribute as IValidator;
                    if (entityValidator == null)
                    {
                        ValidatorClassAttribute validatorClass = null;
                        object[] attributesInTheAttribute      = attribute.GetType().GetCustomAttributes(typeof(ValidatorClassAttribute),
                                                                                                         false);

                        if (attributesInTheAttribute.Length > 0)
                        {
                            validatorClass = (ValidatorClassAttribute)attributesInTheAttribute[0];
                        }

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

                        entityValidator = constraintValidatorFactory.GetInstance(validatorClass.Value);
                        InitializeValidator(attribute, validatorClass.Value, entityValidator);
                    }
                }

                defaultInterpolator.AddInterpolator(attribute, entityValidator);
                return(entityValidator);
            }
            catch (Exception ex)
            {
                throw new HibernateValidatorException(
                          "could not instantiate ClassValidator, maybe some validator is not well formed; check InnerException", ex);
            }
        }