public void CreateValidators_DoesNotAddDuplicateRequiredAttribute_ForIsRequiredTrue()
        {
            // Arrange
            var provider = new DataAnnotationsClientModelValidatorProvider(
                new ValidationAttributeAdapterProvider(),
                new TestOptionsManager<MvcDataAnnotationsLocalizationOptions>(),
                stringLocalizerFactory: null);

            var metadata = _metadataProvider.GetMetadataForProperty(
                typeof(DummyRequiredAttributeHelperClass),
                nameof(DummyRequiredAttributeHelperClass.ValueTypeWithoutAttribute));

            var items = GetValidatorItems(metadata);
            var expectedValidatorItem = new ClientValidatorItem
            {
                Validator = new RequiredAttributeAdapter(new RequiredAttribute(), stringLocalizer: null),
                IsReusable = true
            };
            items.Add(expectedValidatorItem);

            var providerContext = new ClientValidatorProviderContext(metadata, items);

            // Act
            provider.CreateValidators(providerContext);

            // Assert
            var validatorItem = Assert.Single(providerContext.Results);
            Assert.Same(expectedValidatorItem.Validator, validatorItem.Validator);
        }
Example #2
0
            public void CreateValidators(ClientValidatorProviderContext context)
            {
                for (var i = 0; i < context.Results.Count; i++)
                {
                    var validatorItem = context.Results[i];
                    if (validatorItem.Validator != null)
                    {
                        continue;
                    }

                    var attribute = validatorItem.ValidatorMetadata as ValidationAttribute;
                    if (attribute == null)
                    {
                        continue;
                    }

                    var validationAdapterProvider = new ValidationAttributeAdapterProvider();

                    validatorItem.Validator = validationAdapterProvider.GetAttributeAdapter(attribute, stringLocalizer: null);

                    if (attribute is RequiredAttribute)
                    {
                        validatorItem.IsReusable = true;
                    }
                }
            }
 public void CreateValidators(ClientValidatorProviderContext context)
 {
     if (typeof(DateTime?).IsAssignableFrom(context.ModelMetadata.UnderlyingOrModelType))
         context.Results.Add(new ClientValidatorItem
         {
             Validator = new DateValidator(),
             IsReusable = true
         });
 }
        public void CreateValidators_DoesNotCreate()
        {
            ModelMetadata metadata = new EmptyModelMetadataProvider().GetMetadataForType(typeof(String));
            ClientValidatorProviderContext context = new ClientValidatorProviderContext(metadata, new List<ClientValidatorItem>());

            new DateValidatorProvider().CreateValidators(context);

            Assert.Empty(context.Results);
        }
        /// <inheritdoc />
        public void CreateValidators(ClientValidatorProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            IStringLocalizer stringLocalizer = null;
            if (_options.Value.DataAnnotationLocalizerProvider != null && _stringLocalizerFactory != null)
            {
                // This will pass first non-null type (either containerType or modelType) to delegate.
                // Pass the root model type(container type) if it is non null, else pass the model type.
                stringLocalizer = _options.Value.DataAnnotationLocalizerProvider(
                    context.ModelMetadata.ContainerType ?? context.ModelMetadata.ModelType,
                    _stringLocalizerFactory);
            }

            var hasRequiredAttribute = false;

            for (var i = 0; i < context.Results.Count; i++)
            {
                var validatorItem = context.Results[i];
                if (validatorItem.Validator != null)
                {
                    // Check if a required attribute is already cached.
                    hasRequiredAttribute |= validatorItem.Validator is RequiredAttributeAdapter;
                    continue;
                }

                var attribute = validatorItem.ValidatorMetadata as ValidationAttribute;
                if (attribute == null)
                {
                    continue;
                }

                hasRequiredAttribute |= attribute is RequiredAttribute;

                var adapter = _validationAttributeAdapterProvider.GetAttributeAdapter(attribute, stringLocalizer);
                if (adapter != null)
                {
                    validatorItem.Validator = adapter;
                    validatorItem.IsReusable = true;
                }
            }

            if (!hasRequiredAttribute && context.ModelMetadata.IsRequired)
            {
                // Add a default '[Required]' validator for generating HTML if necessary.
                context.Results.Add(new ClientValidatorItem
                {
                    Validator = new RequiredAttributeAdapter(new RequiredAttribute(), stringLocalizer),
                    IsReusable = true
                });
            }
        }
        /// <inheritdoc />
        public void CreateValidators(ClientValidatorProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // Perf: Avoid allocations
            for (var i = 0; i < ValidatorProviders.Count; i++)
            {
                ValidatorProviders[i].CreateValidators(context);
            }
        }
        public void CreateValidators_ForDate(Type type)
        {
            ModelMetadata metadata = new EmptyModelMetadataProvider().GetMetadataForType(type);
            ClientValidatorProviderContext context = new ClientValidatorProviderContext(metadata, new List<ClientValidatorItem>());

            new DateValidatorProvider().CreateValidators(context);

            ClientValidatorItem actual = context.Results.Single();

            Assert.IsType<DateValidator>(actual.Validator);
            Assert.Null(actual.ValidatorMetadata);
            Assert.True(actual.IsReusable);
        }
        public void CreateValidators_DoesNotGetsNumericValidator_ForUnsupportedTypes(Type modelType)
        {
            // Arrange
            var provider = new NumericClientModelValidatorProvider();
            var metadata = _metadataProvider.GetMetadataForType(modelType);

            var providerContext = new ClientValidatorProviderContext(metadata, GetValidatorItems(metadata));

            // Act
            provider.CreateValidators(providerContext);

            // Assert
            Assert.Empty(providerContext.Results);
        }
        public void CreateValidators_AddsRequiredAttribute_ForIsRequiredTrue()
        {
            // Arrange
            var provider = new DataAnnotationsClientModelValidatorProvider(
                new ValidationAttributeAdapterProvider(),
                new TestOptionsManager<MvcDataAnnotationsLocalizationOptions>(),
                stringLocalizerFactory: null);

            var metadata = _metadataProvider.GetMetadataForProperty(
                typeof(DummyRequiredAttributeHelperClass),
                nameof(DummyRequiredAttributeHelperClass.ValueTypeWithoutAttribute));

            var providerContext = new ClientValidatorProviderContext(metadata, GetValidatorItems(metadata));

            // Act
            provider.CreateValidators(providerContext);

            // Assert
            var validatorItem = Assert.Single(providerContext.Results);
            Assert.IsType<RequiredAttributeAdapter>(validatorItem.Validator);
        }
        public void CreateValidators_DoesNotAddDuplicateValidators()
        {
            // Arrange
            var provider = new NumericClientModelValidatorProvider();
            var metadata = _metadataProvider.GetMetadataForType(typeof(float));
            var items = GetValidatorItems(metadata);
            var expectedValidatorItem = new ClientValidatorItem
            {
                Validator = new NumericClientModelValidator(),
                IsReusable = true
            };
            items.Add(expectedValidatorItem);

            var providerContext = new ClientValidatorProviderContext(metadata, items);

            // Act
            provider.CreateValidators(providerContext);

            // Assert
            var validatorItem = Assert.Single(providerContext.Results);
            Assert.Same(expectedValidatorItem.Validator, validatorItem.Validator);
        }
        private void ExecuteProvider(IClientModelValidatorProvider validatorProvider, ModelMetadata metadata, List <ClientValidatorItem> items)
        {
            var context = new ClientValidatorProviderContext(metadata, items);

            validatorProvider.CreateValidators(context);
        }
        public void CreateValidators_DoesNotAddExtraRequiredAttribute_IfAttributeIsSpecifiedExplicitly()
        {
            // Arrange
            var provider = new DataAnnotationsClientModelValidatorProvider(
                new ValidationAttributeAdapterProvider(),
                new TestOptionsManager<MvcDataAnnotationsLocalizationOptions>(),
                stringLocalizerFactory: null);

            var metadata = _metadataProvider.GetMetadataForProperty(
                typeof(DummyRequiredAttributeHelperClass),
                nameof(DummyRequiredAttributeHelperClass.WithAttribute));

            var providerContext = new ClientValidatorProviderContext(metadata, GetValidatorItems(metadata));

            // Act
            provider.CreateValidators(providerContext);

            // Assert
            var validatorItem = Assert.Single(providerContext.Results);
            var adapter = Assert.IsType<RequiredAttributeAdapter>(validatorItem.Validator);
            Assert.Equal("Custom Required Message", adapter.Attribute.ErrorMessage);
        }
        public void UnknownValidationAttribute_IsNotAddedAsValidator()
        {
            // Arrange
            var provider = new DataAnnotationsClientModelValidatorProvider(
                new ValidationAttributeAdapterProvider(),
                new TestOptionsManager<MvcDataAnnotationsLocalizationOptions>(),
                stringLocalizerFactory: null);
            var metadata = _metadataProvider.GetMetadataForType(typeof(DummyClassWithDummyValidationAttribute));

            var providerContext = new ClientValidatorProviderContext(metadata, GetValidatorItems(metadata));

            // Act
            provider.CreateValidators(providerContext);

            // Assert
            var validatorItem = Assert.Single(providerContext.Results);
            Assert.Null(validatorItem.Validator);
        }
        public void CreateValidators_DoesNotAddRequiredAttribute_ForIsRequiredFalse()
        {
            // Arrange
            var provider = new DataAnnotationsClientModelValidatorProvider(
                new ValidationAttributeAdapterProvider(),
                new TestOptionsManager<MvcDataAnnotationsLocalizationOptions>(),
                stringLocalizerFactory: null);

            var metadata = _metadataProvider.GetMetadataForProperty(
                typeof(DummyRequiredAttributeHelperClass),
                nameof(DummyRequiredAttributeHelperClass.ReferenceTypeWithoutAttribute));

            var providerContext = new ClientValidatorProviderContext(metadata, GetValidatorItems(metadata));

            // Act
            provider.CreateValidators(providerContext);

            // Assert
            Assert.Empty(providerContext.Results);
        }
        public void CreateValidators_GetsNumericValidator_ForNumericType(Type modelType)
        {
            // Arrange
            var provider = new NumericClientModelValidatorProvider();
            var metadata = _metadataProvider.GetMetadataForType(modelType);

            var providerContext = new ClientValidatorProviderContext(metadata, GetValidatorItems(metadata));

            // Act
            provider.CreateValidators(providerContext);

            // Assert
            var validatorItem = Assert.Single(providerContext.Results);
            Assert.IsType<NumericClientModelValidator>(validatorItem.Validator);
        }
        public void CreateValidators_FromModelMetadataType_SingleValidator()
        {
            // Arrange
            var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider();
            var validatorProvider = TestClientModelValidatorProvider.CreateDefaultProvider();

            var metadata = metadataProvider.GetMetadataForProperty(
                typeof(ProductViewModel),
                nameof(ProductViewModel.Id));
            var context = new ClientValidatorProviderContext(metadata, GetClientValidatorItems(metadata));

            // Act
            validatorProvider.CreateValidators(context);

            // Assert
            var validatorItems = context.Results;

            Assert.Equal(2, validatorItems.Count);
            Assert.Single(validatorItems, v => v.Validator is RangeAttributeAdapter);
            Assert.Single(validatorItems, v => v.Validator is RequiredAttributeAdapter);
        }
        public void CreateValidators_ClientValidatorAttribute_DefaultAdapter()
        {
            // Arrange
            var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider();
            var validatorProvider = TestClientModelValidatorProvider.CreateDefaultProvider();

            var metadata = metadataProvider.GetMetadataForProperty(
                typeof(CustomValidationAttributeOnProperty),
                nameof(CustomValidationAttributeOnProperty.Property));
            var context = new ClientValidatorProviderContext(metadata, GetClientValidatorItems(metadata));

            // Act
            validatorProvider.CreateValidators(context);

            // Assert
            var validatorItems = context.Results;

             Assert.IsType<CustomValidationAttribute>(Assert.Single(validatorItems).Validator);
        }
Example #18
0
        private void ExecuteProvider(IClientModelValidatorProvider validatorProvider, ModelMetadata metadata, List<ClientValidatorItem> items)
        {
            var context = new ClientValidatorProviderContext(metadata, items);

            validatorProvider.CreateValidators(context);
        }