Exemplo n.º 1
0
        public void AddValidation_WithoutLocalizationAndCustomFileExtensions(string extensions, string expectedExtensions)
        {
            // Arrange
            var provider = TestModelMetadataProvider.CreateDefaultProvider();
            var metadata = provider.GetMetadataForProperty(typeof(Profile), nameof(Profile.PhotoFileName));

            var attribute = new FileExtensionsAttribute()
            {
                Extensions = extensions
            };

            attribute.ErrorMessage = "{0} expects only the following extensions: {1}";

            // FileExtensionsAttribute formats the extension list for the error message
            var formattedExtensions  = string.Join(", ", expectedExtensions.Split(','));
            var expectedErrorMessage = string.Format(attribute.ErrorMessage, nameof(Profile.PhotoFileName), formattedExtensions);

            var adapter = new FileExtensionsAttributeAdapter(attribute, stringLocalizer: null);
            var context = new ClientModelValidationContext(new ActionContext(), metadata, provider, new Dictionary <string, string>());

            // Act
            adapter.AddValidation(context);

            // Assert
            Assert.Collection(
                context.Attributes,
                kvp => { Assert.Equal("data-val", kvp.Key); Assert.Equal("true", kvp.Value); },
                kvp => { Assert.Equal("data-val-fileextensions", kvp.Key); Assert.Equal(expectedErrorMessage, kvp.Value); },
                kvp => { Assert.Equal("data-val-fileextensions-extensions", kvp.Key); Assert.Equal(expectedExtensions, kvp.Value); });
        }
Exemplo n.º 2
0
        public void AddValidation_DoesNotTrounceExistingAttributes()
        {
            // Arrange
            var provider = TestModelMetadataProvider.CreateDefaultProvider();
            var metadata = provider.GetMetadataForProperty(typeof(Profile), "PhotoFileName");

            var attribute = new FileExtensionsAttribute()
            {
                Extensions = "jpg"
            };

            attribute.ErrorMessage = "{0} expects only the following extensions: {1}";

            var adapter = new FileExtensionsAttributeAdapter(attribute, stringLocalizer: null);
            var context = new ClientModelValidationContext(new ActionContext(), metadata, provider, new Dictionary <string, string>());

            context.Attributes.Add("data-val", "original");
            context.Attributes.Add("data-val-fileextensions", "original");
            context.Attributes.Add("data-val-fileextensions-extensions", "original");

            // Act
            adapter.AddValidation(context);

            // Assert
            Assert.Collection(
                context.Attributes,
                kvp => { Assert.Equal("data-val", kvp.Key); Assert.Equal("original", kvp.Value); },
                kvp => { Assert.Equal("data-val-fileextensions", kvp.Key); Assert.Equal("original", kvp.Value); },
                kvp => { Assert.Equal("data-val-fileextensions-extensions", kvp.Key); Assert.Equal("original", kvp.Value); });
        }
        public void AddValidation_adds_ext_rule(string extensions)
        {
            // Arrange
            var attribute = new FileExtensionsAttribute {
                Extensions = extensions
            };
            var adapter = new FileExtensionsAttributeAdapter(attribute);

            var context = new ClientModelValidationContextBuilder()
                          .WithModelType <string>()
                          .Build();

            // Act
            adapter.AddValidation(context);

            // Assert
            context.Attributes.Keys.ShouldContain("v-validate");
            context.Attributes["v-validate"].ShouldBe("{ext:['pdf','png','zip']}");
        }
Exemplo n.º 4
0
        public void AddValidation_WithLocalization(string extensions, string expectedExtensions)
        {
            // Arrange
            var provider = TestModelMetadataProvider.CreateDefaultProvider();
            var metadata = provider.GetMetadataForProperty(typeof(Profile), nameof(Profile.PhotoFileName));

            var attribute = new FileExtensionsAttribute()
            {
                Extensions = extensions
            };

            attribute.ErrorMessage = "{0} expects only the following extensions: {1}";

            var formattedExtensions  = string.Join(", ", expectedExtensions.Split(','));
            var expectedProperties   = new object[] { "PhotoFileName", formattedExtensions };
            var expectedErrorMessage = $"{nameof(Profile.PhotoFileName)} expects only the following extensions: {formattedExtensions}";

            var stringLocalizer = new Mock <IStringLocalizer>();

            stringLocalizer
            .Setup(s => s[attribute.ErrorMessage, expectedProperties])
            .Returns(new LocalizedString(attribute.ErrorMessage, expectedErrorMessage));

            var adapter = new FileExtensionsAttributeAdapter(attribute, stringLocalizer: stringLocalizer.Object);
            var context = new ClientModelValidationContext(new ActionContext(), metadata, provider, new Dictionary <string, string>());

            // Act
            adapter.AddValidation(context);

            // Assert
            Assert.Collection(
                context.Attributes,
                kvp => { Assert.Equal("data-val", kvp.Key); Assert.Equal("true", kvp.Value); },
                kvp => { Assert.Equal("data-val-fileextensions", kvp.Key); Assert.Equal(expectedErrorMessage, kvp.Value); },
                kvp => { Assert.Equal("data-val-fileextensions-extensions", kvp.Key); Assert.Equal(expectedExtensions, kvp.Value); });
        }
Exemplo n.º 5
0
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            if (attribute == null)
            {
                throw new ArgumentNullException(nameof(attribute));
            }

            if (_options == null)
            {
                throw new ArgumentNullException(nameof(_options));
            }

            IAttributeAdapter adapter;

            if (attribute is CompareAttribute compareAttribute)
            {
                adapter = new CompareAttributeAdapter(compareAttribute);
            }
            else if (attribute is CreditCardAttribute creditCardAttribute)
            {
                adapter = new CreditCardAttributeAdapter(creditCardAttribute);
            }
            else if (attribute is EmailAddressAttribute emailAddressAttribute)
            {
                adapter = new EmailAddressAttributeAdapter(emailAddressAttribute);
            }
            else if (attribute is FileExtensionsAttribute fileExtensionsAttribute)
            {
                adapter = new FileExtensionsAttributeAdapter(fileExtensionsAttribute);
            }
            else if (attribute is MaxLengthAttribute maxLengthAttribute)
            {
                adapter = new MaxLengthAttributeAdapter(maxLengthAttribute);
            }
            else if (attribute is MinLengthAttribute minLengthAttribute)
            {
                adapter = new MinLengthAttributeAdapter(minLengthAttribute);
            }
            else if (attribute is RangeAttribute rangeAttribute)
            {
                adapter = new RangeAttributeAdapter(rangeAttribute, _options);
            }
            else if (attribute is RegularExpressionAttribute regularExpressionAttribute)
            {
                adapter = new RegularExpressionAttributeAdapter(regularExpressionAttribute);
            }
            else if (attribute is RequiredAttribute requiredAttribute)
            {
                adapter = new RequiredAttributeAdapter(requiredAttribute);
            }
            else if (attribute is StringLengthAttribute stringLengthAttribute)
            {
                adapter = new StringLengthAttributeAdapter(stringLengthAttribute);
            }
            else if (attribute is UrlAttribute urlAttribute)
            {
                adapter = new UrlAttributeAdapter(urlAttribute);
            }
            else
            {
                adapter = null;
            }

            return(adapter);
        }
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            if (attribute == null)
            {
                throw new ArgumentNullException(nameof(attribute));
            }

            IAttributeAdapter adapter;

            var type = attribute.GetType();

            if (attribute is RegularExpressionAttribute)
            {
                adapter = new RegularExpressionAttributeAdapter((RegularExpressionAttribute)attribute, stringLocalizer);
            }
            else if (attribute is MaxLengthAttribute)
            {
                adapter = new MaxLengthAttributeAdapter((MaxLengthAttribute)attribute, stringLocalizer);
            }
            else if (attribute is RequiredAttribute)
            {
                adapter = new RequiredAttributeAdapter((RequiredAttribute)attribute, stringLocalizer);
            }
            else if (attribute is CompareAttribute)
            {
                adapter = new CompareAttributeAdapter((CompareAttribute)attribute, stringLocalizer);
            }
            else if (attribute is MinLengthAttribute)
            {
                adapter = new MinLengthAttributeAdapter((MinLengthAttribute)attribute, stringLocalizer);
            }
            else if (attribute is CreditCardAttribute)
            {
                adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-creditcard", stringLocalizer);
            }
            else if (attribute is StringLengthAttribute)
            {
                adapter = new StringLengthAttributeAdapter((StringLengthAttribute)attribute, stringLocalizer);
            }
            else if (attribute is RangeAttribute)
            {
                adapter = new RangeAttributeAdapter((RangeAttribute)attribute, stringLocalizer);
            }
            else if (attribute is EmailAddressAttribute)
            {
                adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-email", stringLocalizer);
            }
            else if (attribute is PhoneAttribute)
            {
                adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-phone", stringLocalizer);
            }
            else if (attribute is UrlAttribute)
            {
                adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-url", stringLocalizer);
            }
            else if (attribute is FileExtensionsAttribute)
            {
                adapter = new FileExtensionsAttributeAdapter((FileExtensionsAttribute)attribute, stringLocalizer);
            }
            else
            {
                adapter = defaultClientModelValidatorProvider.GetAttributeAdapter(attribute, stringLocalizer);
            }

            return(adapter);
        }
Exemplo n.º 7
0
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            if (attribute == null)
            {
                throw new ArgumentNullException(nameof(attribute));
            }

            IAttributeAdapter adapter = null;

            var type = attribute.GetType();

#if NETCOREAPP2_2
            if (attribute is RegularExpressionAttribute)
            {
                adapter = new RegularExpressionAttributeAdapter((RegularExpressionAttribute)attribute, stringLocalizer);
            }
            else if (attribute is MaxLengthAttribute)
            {
                adapter = new MaxLengthAttributeAdapter((MaxLengthAttribute)attribute, stringLocalizer);
            }
            else if (attribute is RequiredAttribute)
            {
                adapter = new Microsoft.AspNetCore.Mvc.DataAnnotations.Internal.RequiredAttributeAdapter((RequiredAttribute)attribute, stringLocalizer);
            }
            else if (attribute is CompareAttribute)
            {
                adapter = new CompareAttributeAdapter((CompareAttribute)attribute, stringLocalizer);
            }
            else if (attribute is MinLengthAttribute)
            {
                adapter = new MinLengthAttributeAdapter((MinLengthAttribute)attribute, stringLocalizer);
            }
            else if (attribute is CreditCardAttribute)
            {
                adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-creditcard", stringLocalizer);
            }
            else if (attribute is StringLengthAttribute)
            {
                adapter = new StringLengthAttributeAdapter((StringLengthAttribute)attribute, stringLocalizer);
            }
            else if (attribute is RangeAttribute)
            {
                adapter = new RangeAttributeAdapter((RangeAttribute)attribute, stringLocalizer);
            }
            else if (attribute is EmailAddressAttribute)
            {
                adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-email", stringLocalizer);
            }
            else if (attribute is PhoneAttribute)
            {
                adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-phone", stringLocalizer);
            }
            else if (attribute is UrlAttribute)
            {
                adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-url", stringLocalizer);
            }
            else if (attribute is FileExtensionsAttribute)
            {
                adapter = new FileExtensionsAttributeAdapter((FileExtensionsAttribute)attribute, stringLocalizer);
            }
            else
            {
                adapter = defaultClientModelValidatorProvider.GetAttributeAdapter(attribute, stringLocalizer);
            }
#else
            if (attribute is RegularExpressionAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.RegularExpressionAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (RegularExpressionAttribute)attribute, stringLocalizer);
            }
            else if (attribute is MaxLengthAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.MaxLengthAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (MaxLengthAttribute)attribute, stringLocalizer);
            }
            else if (attribute is RequiredAttribute)
            {
                adapter = new RequiredAttributeAdapter((RequiredAttribute)attribute, stringLocalizer);
            }
            else if (attribute is CompareAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.CompareAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (CompareAttribute)attribute, stringLocalizer);
            }
            else if (attribute is MinLengthAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.MinLengthAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (MinLengthAttribute)attribute, stringLocalizer);
            }
            else if (attribute is CreditCardAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.DataTypeAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (DataTypeAttribute)attribute, "data-val-creditcard", stringLocalizer);
            }
            else if (attribute is StringLengthAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.StringLengthAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (StringLengthAttribute)attribute, stringLocalizer);
            }
            else if (attribute is RangeAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.RangeAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (RangeAttribute)attribute, stringLocalizer);
            }
            else if (attribute is EmailAddressAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.DataTypeAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (DataTypeAttribute)attribute, "data-val-email", stringLocalizer);
            }
            else if (attribute is PhoneAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.DataTypeAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (DataTypeAttribute)attribute, "data-val-phone", stringLocalizer);
            }
            else if (attribute is UrlAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.DataTypeAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (DataTypeAttribute)attribute, "data-val-url", stringLocalizer);
            }
            else if (attribute is FileExtensionsAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.FileExtensionsAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (FileExtensionsAttribute)attribute, stringLocalizer);
            }
            else
            {
                adapter = defaultClientModelValidatorProvider.GetAttributeAdapter(attribute, stringLocalizer);
            }
#endif

            return(adapter);
        }