/// <summary>
        /// Gets the <see cref="PropertyValidator"/> instances for the specified <paramref name="type"/>.
        /// </summary>
        /// <param name="type">The <see cref="Type"/> that the validators should be retrieved for.</param>
        /// <returns>An <see cref="IEnumerable{T}"/> instance, containing <see cref="IPropertyValidator"/> objects.</returns>
        public IEnumerable<IPropertyValidator> GetValidators(Type type)
        {
            var typeDescriptor =
                new AssociatedMetadataTypeTypeDescriptionProvider(type).GetTypeDescriptor(type);

            var propertyDescriptors =
                typeDescriptor.GetProperties();

            var results =
                new List<IPropertyValidator>();

            foreach (PropertyDescriptor descriptor in propertyDescriptors)
            {
                var attributes =
                    descriptor.Attributes.OfType<ValidationAttribute>();

                var validator =
                    new PropertyValidator
                    {
                        AttributeAdaptors = this.GetAttributeAdaptors(attributes),
                        Descriptor = descriptor
                    };

                results.Add(validator);
            }

            return results;
        }
Пример #2
0
        /// <summary>
        /// Gets the <see cref="PropertyValidator"/> instances for the specified <paramref name="type"/>.
        /// </summary>
        /// <param name="type">The <see cref="Type"/> that the validators should be retrieved for.</param>
        /// <returns>An <see cref="IEnumerable{T}"/> instance, containing <see cref="IPropertyValidator"/> objects.</returns>
        public IEnumerable <IPropertyValidator> GetValidators(Type type)
        {
            var typeDescriptor =
                new AssociatedMetadataTypeTypeDescriptionProvider(type).GetTypeDescriptor(type);

            var propertyDescriptors =
                typeDescriptor.GetProperties();

            var results =
                new List <IPropertyValidator>();

            foreach (PropertyDescriptor descriptor in propertyDescriptors)
            {
                var attributes =
                    descriptor.Attributes.OfType <ValidationAttribute>();

                var validator =
                    new PropertyValidator
                {
                    AttributeAdaptors = this.GetAttributeAdaptors(attributes),
                    Descriptor        = descriptor
                };

                results.Add(validator);
            }

            return(results);
        }
        private PropertyValidator GetTypeValidator(ICustomTypeDescriptor typeDescriptor)
        {
            var classAttributes =
                typeDescriptor.GetAttributes().OfType<ValidationAttribute>();

            var classValidator =
                new PropertyValidator
                {
                    AttributeAdaptors = this.GetAttributeAdaptors(classAttributes)
                };
            return classValidator;
        }
        private PropertyValidator GetTypeValidator(ICustomTypeDescriptor typeDescriptor)
        {
            var classAttributes =
                typeDescriptor.GetAttributes().OfType <ValidationAttribute>();

            var classValidator =
                new PropertyValidator
            {
                AttributeAdaptors = this.GetAttributeAdaptors(classAttributes)
            };

            return(classValidator);
        }
        private IEnumerable<PropertyValidator> GetPropertyValidators(ICustomTypeDescriptor typeDescriptor)
        {
            var propertyDescriptors =
                typeDescriptor.GetProperties();

            foreach (PropertyDescriptor descriptor in propertyDescriptors)
            {
                var attributes =
                    descriptor.Attributes.OfType<ValidationAttribute>();

                var validator =
                    new PropertyValidator
                    {
                        AttributeAdaptors = this.GetAttributeAdaptors(attributes),
                        Descriptor = descriptor
                    };

                yield return validator;
            }
        }
        private IEnumerable <PropertyValidator> GetPropertyValidators(ICustomTypeDescriptor typeDescriptor)
        {
            var propertyDescriptors =
                typeDescriptor.GetProperties();

            foreach (PropertyDescriptor descriptor in propertyDescriptors)
            {
                var attributes =
                    descriptor.Attributes.OfType <ValidationAttribute>();

                var validator =
                    new PropertyValidator
                {
                    AttributeAdaptors = this.GetAttributeAdaptors(attributes),
                    Descriptor        = descriptor
                };

                yield return(validator);
            }
        }
Пример #7
0
        public PropertyValidatorFixture()
        {
            this.adapter1 =
                A.Fake<IDataAnnotationsValidatorAdapter>();

            this.error1 =
                new ModelValidationError("error1", x => string.Empty);

            A.CallTo(() => this.adapter1.Validate(A<object>._, A<ValidationAttribute>._, A<PropertyDescriptor>._))
                .Returns(new[] {this.error1});

            this.adapter2 =
                A.Fake<IDataAnnotationsValidatorAdapter>();

            this.error2 =
                new ModelValidationError("error2", x => string.Empty);

            A.CallTo(() => this.adapter2.Validate(A<object>._, A<ValidationAttribute>._, A<PropertyDescriptor>._))
                .Returns(new[] { this.error2 });

            this.mappings =
                new Dictionary<ValidationAttribute, IEnumerable<IDataAnnotationsValidatorAdapter>>
                {
                    {new RangeAttribute(1, 10), new[] {this.adapter1}},
                    {new RequiredAttribute(), new[] {this.adapter2}}
                };

            var type =
                typeof(Model);

            this.descriptor = new AssociatedMetadataTypeTypeDescriptionProvider(type)
                .GetTypeDescriptor(type)
                .GetProperties()[0];

            this.validator = new PropertyValidator
            {
                AttributeAdaptors = this.mappings,
                Descriptor =this.descriptor
            };
        }