/// <inheritdoc />
        public void CreateValidationMetadata(ValidationMetadataProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (Type != null)
            {
                if (Type.IsAssignableFrom(context.Key.ModelType))
                {
                    context.ValidationMetadata.ValidateChildren = false;
                }

                return;
            }

            if (FullTypeName != null)
            {
                if (IsMatchingName(context.Key.ModelType))
                {
                    context.ValidationMetadata.ValidateChildren = false;
                }

                return;
            }

            Debug.Fail("We shouldn't get here.");
        }
        public void CreateValidationMetadata_SetsHasValidatorsToTrue_IfProviderReturnsTrue()
        {
            // Arrange
            var metadataBasedModelValidatorProvider = new Mock <IMetadataBasedModelValidatorProvider>();

            metadataBasedModelValidatorProvider.Setup(p => p.HasValidators(typeof(object), It.IsAny <IList <object> >()))
            .Returns(true)
            .Verifiable();

            var validationProviders = new IModelValidatorProvider[]
            {
                new DefaultModelValidatorProvider(),
                metadataBasedModelValidatorProvider.Object,
            };
            var metadataProvider = new HasValidatorsValidationMetadataProvider(validationProviders);

            var key             = ModelMetadataIdentity.ForType(typeof(object));
            var modelAttributes = new ModelAttributes(new object[0], new object[0], new object[0]);
            var context         = new ValidationMetadataProviderContext(key, modelAttributes);

            // Act
            metadataProvider.CreateValidationMetadata(context);

            // Assert
            Assert.True(context.ValidationMetadata.HasValidators);
            metadataBasedModelValidatorProvider.Verify();
        }
        /// <inheritdoc />
        public virtual void CreateValidationMetadata(ValidationMetadataProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            foreach (var provider in _providers.OfType<IValidationMetadataProvider>())
            {
                provider.CreateValidationMetadata(context);
            }
        }
Beispiel #4
0
        /// <inheritdoc />
        public void CreateValidationMetadata(ValidationMetadataProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            foreach (var provider in _providers.OfType <IValidationMetadataProvider>())
            {
                provider.CreateValidationMetadata(context);
            }
        }
        public void PropertyValidationFilter_Null_IfPropertyHasValidateNeverOnItsType()
        {
            // Arrange
            var provider = new DefaultValidationMetadataProvider();

            var attributes = new Attribute[] { new ValidateNeverAttribute() };
            var key        = ModelMetadataIdentity.ForProperty(typeof(string).GetProperty(nameof(string.Length)), typeof(int), typeof(string));
            var context    = new ValidationMetadataProviderContext(key, new ModelAttributes(attributes, new object[0], null));

            // Act
            provider.CreateValidationMetadata(context);

            // Assert
            Assert.Null(context.ValidationMetadata.PropertyValidationFilter);
        }
        public void PropertyValidationFilter_Null_ForType()
        {
            // Arrange
            var provider = new DefaultValidationMetadataProvider();

            var attributes = new Attribute[] { new ValidateNeverAttribute() };
            var key        = ModelMetadataIdentity.ForType(typeof(ValidateNeverClass));
            var context    = new ValidationMetadataProviderContext(key, new ModelAttributes(attributes, null, null));

            // Act
            provider.CreateValidationMetadata(context);

            // Assert
            Assert.Null(context.ValidationMetadata.PropertyValidationFilter);
        }
        public void CreateValidationMetadata_DoesNotSetHasValidators_IfProviderIsConfigured()
        {
            // Arrange
            var validationProviders = new IModelValidatorProvider[0];
            var metadataProvider    = new HasValidatorsValidationMetadataProvider(validationProviders);

            var key             = ModelMetadataIdentity.ForType(typeof(object));
            var modelAttributes = new ModelAttributes(new object[0], new object[0], new object[0]);
            var context         = new ValidationMetadataProviderContext(key, modelAttributes);

            // Act
            metadataProvider.CreateValidationMetadata(context);

            // Assert
            Assert.Null(context.ValidationMetadata.HasValidators);
        }
        public void GetValidationDetails_MarkedWithModelValidator_ReturnsValidator()
        {
            // Arrange
            var provider = new DefaultValidationMetadataProvider();

            var attribute = new TestModelValidationAttribute();
            var attributes = new Attribute[] { attribute };
            var key = ModelMetadataIdentity.ForProperty(typeof(int), "Length", typeof(string));
            var context = new ValidationMetadataProviderContext(key, new ModelAttributes(attributes, new object[0]));

            // Act
            provider.CreateValidationMetadata(context);

            // Assert
            var validatorMetadata = Assert.Single(context.ValidationMetadata.ValidatorMetadata);
            Assert.Same(attribute, validatorMetadata);
        }
Beispiel #9
0
        public void CreateValidationMetadata(ValidationMetadataProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            foreach (var attribute in context.Attributes)
            {
                if (attribute is IModelValidator || attribute is IClientModelValidator)
                {
                    // If another provider has already added this attribute, do not repeat it.
                    // This will prevent attributes like RemoteAttribute (which implement ValidationAttribute and
                    // IClientModelValidator) to be added to the ValidationMetadata twice.
                    // This is to ensure we do not end up with duplication validation rules on the client side.
                    if (!context.ValidationMetadata.ValidatorMetadata.Contains(attribute))
                    {
                        context.ValidationMetadata.ValidatorMetadata.Add(attribute);
                    }
                }
            }

            // IPropertyValidationFilter attributes on a type affect properties in that type, not properties that have
            // that type. Thus, we ignore context.TypeAttributes for properties and not check at all for types.
            if (context.Key.MetadataKind == ModelMetadataKind.Property)
            {
                var validationFilter = context.PropertyAttributes !.OfType <IPropertyValidationFilter>().FirstOrDefault();
                if (validationFilter == null)
                {
                    // No IPropertyValidationFilter attributes on the property.
                    // Check if container has such an attribute.
                    validationFilter = context.Key.ContainerType !
                                       .GetCustomAttributes(inherit: true)
                                       .OfType <IPropertyValidationFilter>()
                                       .FirstOrDefault();
                }

                context.ValidationMetadata.PropertyValidationFilter = validationFilter;
            }
            else if (context.Key.MetadataKind == ModelMetadataKind.Parameter)
            {
                var validationFilter = context.ParameterAttributes !.OfType <IPropertyValidationFilter>().FirstOrDefault();
                context.ValidationMetadata.PropertyValidationFilter = validationFilter;
            }
        }
        public void GetValidationDetails_MarkedWithModelValidator_ReturnsValidator()
        {
            // Arrange
            var provider = new DefaultValidationMetadataProvider();

            var attribute  = new TestModelValidationAttribute();
            var attributes = new Attribute[] { attribute };
            var key        = ModelMetadataIdentity.ForProperty(typeof(string).GetProperty(nameof(string.Length)), typeof(int), typeof(string));
            var context    = new ValidationMetadataProviderContext(key, new ModelAttributes(new object[0], attributes, null));

            // Act
            provider.CreateValidationMetadata(context);

            // Assert
            var validatorMetadata = Assert.Single(context.ValidationMetadata.ValidatorMetadata);

            Assert.Same(attribute, validatorMetadata);
        }
        public void PropertyValidationFilter_ShouldValidateEntry_False_IfPropertyHasValidateNever()
        {
            // Arrange
            var provider = new DefaultValidationMetadataProvider();

            var attributes = new Attribute[] { new ValidateNeverAttribute() };
            var key        = ModelMetadataIdentity.ForProperty(typeof(int), "Length", typeof(string));
            var context    = new ValidationMetadataProviderContext(key, new ModelAttributes(new object[0], attributes, null));

            // Act
            provider.CreateValidationMetadata(context);

            // Assert
            Assert.NotNull(context.ValidationMetadata.PropertyValidationFilter);
            Assert.False(context.ValidationMetadata.PropertyValidationFilter.ShouldValidateEntry(
                             new ValidationEntry(),
                             new ValidationEntry()));
        }
        public void PropertyValidationFilter_ShouldValidateEntry_False_IfContainingTypeInheritsValidateNever()
        {
            // Arrange
            var provider = new DefaultValidationMetadataProvider();

            var key = ModelMetadataIdentity.ForProperty(
                typeof(ValidateNeverSubclass).GetProperty(nameof(ValidateNeverSubclass.SubclassName)),
                typeof(string),
                typeof(ValidateNeverSubclass));
            var context = new ValidationMetadataProviderContext(key, new ModelAttributes(new object[0], new object[0], null));

            // Act
            provider.CreateValidationMetadata(context);

            // Assert
            Assert.NotNull(context.ValidationMetadata.PropertyValidationFilter);
            Assert.False(context.ValidationMetadata.PropertyValidationFilter.ShouldValidateEntry(
                             new ValidationEntry(),
                             new ValidationEntry()));
        }
        public void GetValidationDetails_Validator_AlreadyInContext_Ignores()
        {
            // Arrange
            var provider = new DefaultValidationMetadataProvider();

            var attribute  = new TestValidationAttribute();
            var attributes = new Attribute[] { attribute };
            var key        = ModelMetadataIdentity.ForProperty(typeof(int), "Length", typeof(string));
            var context    = new ValidationMetadataProviderContext(key, new ModelAttributes(attributes, new object[0]));

            context.ValidationMetadata.ValidatorMetadata.Add(attribute);

            // Act
            provider.CreateValidationMetadata(context);

            // Assert
            var validatorMetadata = Assert.Single(context.ValidationMetadata.ValidatorMetadata);

            Assert.Same(attribute, validatorMetadata);
        }
        public void CreateValidationMetadata_DoesNotSetHasValidators_IfNonMetadataBasedProviderExists()
        {
            // Arrange
            var validationProviders = new IModelValidatorProvider[]
            {
                new DefaultModelValidatorProvider(),
                Mock.Of <IModelValidatorProvider>(),
            };
            var metadataProvider = new HasValidatorsValidationMetadataProvider(validationProviders);

            var key             = ModelMetadataIdentity.ForType(typeof(object));
            var modelAttributes = new ModelAttributes(new object[0], new object[0], new object[0]);
            var context         = new ValidationMetadataProviderContext(key, modelAttributes);

            // Act
            metadataProvider.CreateValidationMetadata(context);

            // Assert
            Assert.Null(context.ValidationMetadata.HasValidators);
        }
        public void CreateValidationMetadata_SetsHasValidatorsToFalse_IfNoProviderReturnsTrue()
        {
            // Arrange
            var provider            = Mock.Of <IMetadataBasedModelValidatorProvider>(p => p.HasValidators(typeof(object), It.IsAny <IList <object> >()) == false);
            var validationProviders = new IModelValidatorProvider[]
            {
                new DefaultModelValidatorProvider(),
                provider,
            };
            var metadataProvider = new HasValidatorsValidationMetadataProvider(validationProviders);

            var key             = ModelMetadataIdentity.ForType(typeof(object));
            var modelAttributes = new ModelAttributes(new object[0], new object[0], new object[0]);
            var context         = new ValidationMetadataProviderContext(key, modelAttributes);

            // Act
            metadataProvider.CreateValidationMetadata(context);

            // Assert
            Assert.False(context.ValidationMetadata.HasValidators);
        }
        /// <inheritdoc />
        public void CreateValidationMetadata(ValidationMetadataProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            foreach (var attribute in context.Attributes)
            {
                if (attribute is IModelValidator || attribute is IClientModelValidator)
                {
                    // If another provider has already added this attribute, do not repeat it.
                    // This will prevent attributes like RemoteAttribute (which implement ValidationAttribute and
                    // IClientModelValidator) to be added to the ValidationMetadata twice.
                    // This is to ensure we do not end up with duplication validation rules on the client side.
                    if (!context.ValidationMetadata.ValidatorMetadata.Contains(attribute))
                    {
                        context.ValidationMetadata.ValidatorMetadata.Add(attribute);
                    }
                }
            }
        }
        public void CreateValidationMetadata_DoesNotOverrideExistingHasValidatorsValue()
        {
            // Arrange
            var provider            = Mock.Of <IMetadataBasedModelValidatorProvider>(p => p.HasValidators(typeof(object), It.IsAny <IList <object> >()) == false);
            var validationProviders = new IModelValidatorProvider[]
            {
                new DefaultModelValidatorProvider(),
                provider,
            };
            var metadataProvider = new HasValidatorsValidationMetadataProvider(validationProviders);

            var key             = ModelMetadataIdentity.ForType(typeof(object));
            var modelAttributes = new ModelAttributes(new object[0], new object[0], new object[0]);
            var context         = new ValidationMetadataProviderContext(key, modelAttributes);

            // Initialize this value.
            context.ValidationMetadata.HasValidators = true;

            // Act
            metadataProvider.CreateValidationMetadata(context);

            // Assert
            Assert.True(context.ValidationMetadata.HasValidators);
        }
        /// <inheritdoc />
        public void CreateValidationMetadata(ValidationMetadataProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // RequiredAttribute marks a property as required by validation - this means that it
            // must have a non-null value on the model during validation.
            var requiredAttribute = context.Attributes.OfType<RequiredAttribute>().FirstOrDefault();
            if (requiredAttribute != null)
            {
                context.ValidationMetadata.IsRequired = true;
            }

            foreach (var attribute in context.Attributes.OfType<ValidationAttribute>())
            {
                // If another provider has already added this attribute, do not repeat it.
                // This will prevent attributes like RemoteAttribute (which implement ValidationAttribute and
                // IClientModelValidator) to be added to the ValidationMetadata twice.
                // This is to ensure we do not end up with duplication validation rules on the client side.
                if (!context.ValidationMetadata.ValidatorMetadata.Contains(attribute))
                {
                    context.ValidationMetadata.ValidatorMetadata.Add(attribute);
                }
            }
        }