private IEnumerable<ModelValidator> GetValidatorsForProperty(ModelMetadata metadata)
        {
            ICustomTypeDescriptor typeDescriptor = GetTypeDescriptor(metadata.ContainerType);

            PropertyDescriptor property = typeDescriptor.GetProperties().Find(metadata.PropertyName, true);
            Check.Requires<ArgumentException>(property != null, string.Format("Property {0} not found in {1}", metadata.ContainerType.FullName, metadata.PropertyName));

            return GetValidators(metadata, property.Attributes.OfType<Attribute>());
        }
        public DataAnnotationsModelValidator(ModelMetadata metadata,ValidationAttribute attribute)
            : base(metadata)
        {
            if (attribute == null)
            {
                throw new ArgumentNullException("attribute");
            }

            Attribute = attribute;
        }
        public override sealed IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata)
        {
            Check.Requires<ArgumentNullException>(metadata != null, "metadata");

            if (metadata.ContainerType != null && !String.IsNullOrEmpty(metadata.PropertyName))
            {
                return GetValidatorsForProperty(metadata);
            }

            return GetValidatorsForType(metadata);
        }
 public abstract IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata);
 public ValidatableObjectAdapter(ModelMetadata metadata)
     : base(metadata)
 {
 }
 public static IEnumerable<ModelValidator> GetValidators(this Collection<ModelValidatorProvider> providers, ModelMetadata metadata)
 {
     return providers.SelectMany(provider => provider.GetValidators(metadata));
 }
Beispiel #7
0
 public CompositeModelValidator(ModelMetadata metadata)
     : base(metadata)
 {
 }
Beispiel #8
0
 public static ModelValidator GetModelValidator(ModelMetadata metadata)
 {
     return new CompositeModelValidator(metadata);
 }
Beispiel #9
0
 protected ModelValidator(ModelMetadata metadata)
 {
     Check.Requires<ArgumentNullException>(metadata != null);
     Metadata = metadata;
 }
 private IEnumerable<ModelValidator> GetValidatorsForType(ModelMetadata metadata)
 {
     return GetValidators(metadata, GetTypeDescriptor(metadata.ModelType).GetAttributes().Cast<Attribute>());
 }
 protected abstract IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, IEnumerable<Attribute> attributes);
        protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, IEnumerable<Attribute> attributes)
        {
            _adaptersLock.EnterReadLock();

            try
            {
                List<ModelValidator> results = new List<ModelValidator>();

                // Add an implied [Required] attribute for any non-nullable value type,
                // unless they've configured us not to do that.
                if (AddImplicitRequiredAttributeForValueTypes /*&& metadata.IsRequired */ && !attributes.Any(a => a is RequiredAttribute))
                {
                    attributes = attributes.Concat(new[] { new RequiredAttribute() });
                }

                // Produce a validator for each validation attribute we find
                foreach (ValidationAttribute attribute in attributes.OfType<ValidationAttribute>())
                {
                    DataAnnotationsModelValidationFactory factory;
                    if (!AttributeFactories.TryGetValue(attribute.GetType(), out factory))
                    {
                        factory = DefaultAttributeFactory;
                    }
                    results.Add(factory(metadata, attribute));
                }

                // Produce a validator if the type supports IValidatableObject
                if (typeof(IValidatableObject).IsAssignableFrom(metadata.ModelType))
                {
                    DataAnnotationsValidatableObjectAdapterFactory factory;
                    if (!ValidatableFactories.TryGetValue(metadata.ModelType, out factory))
                    {
                        factory = DefaultValidatableFactory;
                    }
                    results.Add(factory(metadata));
                }

                return results;
            }
            finally
            {
                _adaptersLock.ExitReadLock();
            }
        }