public void ClientRulesWithCompareAttribute_ErrorMessageUsesPropertyName() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty(typeof(PropertyNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty"); var adapter = new CompareAttributeAdapter(attribute, stringLocalizer: null); var expectedMessage = "'MyProperty' and 'OtherProperty' do not match."; var actionContext = new ActionContext(); var context = new ClientModelValidationContext( actionContext, metadata, metadataProvider, 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-equalto", kvp.Key); Assert.Equal(expectedMessage, kvp.Value); }, kvp => { Assert.Equal("data-val-equalto-other", kvp.Key); Assert.Equal("*.OtherProperty", kvp.Value); }); }
public void AddValidation_DoesNotTrounceExistingAttributes() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty(typeof(PropertyNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty"); var adapter = new CompareAttributeAdapter(attribute, stringLocalizer: null); var actionContext = new ActionContext(); var context = new ClientModelValidationContext( actionContext, metadata, metadataProvider, new Dictionary <string, string>()); context.Attributes.Add("data-val", "original"); context.Attributes.Add("data-val-equalto", "original"); context.Attributes.Add("data-val-equalto-other", "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-equalto", kvp.Key); Assert.Equal("original", kvp.Value); }, kvp => { Assert.Equal("data-val-equalto-other", kvp.Key); Assert.Equal("original", kvp.Value); }); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesDisplayName_WithoutLocalizer() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty(typeof(PropertyDisplayNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty"); var adapter = new CompareAttributeAdapter(attribute, stringLocalizer: null); // Mono issue - https://github.com/aspnet/External/issues/19 var expectedMessage = PlatformNormalizer.NormalizeContent( "'MyPropertyDisplayName' and 'OtherPropertyDisplayName' do not match."); var actionContext = new ActionContext(); var context = new ClientModelValidationContext( actionContext, metadata, metadataProvider, new AttributeDictionary()); // 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-equalto", kvp.Key); Assert.Equal(expectedMessage, kvp.Value); }, kvp => { Assert.Equal("data-val-equalto-other", kvp.Key); Assert.Equal(kvp.Value, "*.OtherProperty"); }); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesDisplayName() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty(typeof(PropertyDisplayNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty"); attribute.ErrorMessage = "CompareAttributeErrorMessage"; var stringLocalizer = new Mock <IStringLocalizer>(); var expectedProperties = new object[] { "MyPropertyDisplayName", "OtherPropertyDisplayName" }; var expectedMessage = "'MyPropertyDisplayName' and 'OtherPropertyDisplayName' do not match."; stringLocalizer.Setup(s => s[attribute.ErrorMessage, expectedProperties]) .Returns(new LocalizedString(attribute.ErrorMessage, expectedMessage)); var adapter = new CompareAttributeAdapter(attribute, stringLocalizer: stringLocalizer.Object); var actionContext = new ActionContext(); var context = new ClientModelValidationContext( actionContext, metadata, metadataProvider, 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-equalto", kvp.Key); Assert.Equal(expectedMessage, kvp.Value); }, kvp => { Assert.Equal("data-val-equalto-other", kvp.Key); Assert.Equal("*.OtherProperty", kvp.Value); }); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesResourceOverride() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty(typeof(PropertyNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty") { ErrorMessageResourceName = "CompareAttributeTestResource", ErrorMessageResourceType = typeof(DataAnnotations.Test.Resources), }; var adapter = new CompareAttributeAdapter(attribute, stringLocalizer: null); var expectedMessage = "Comparing MyProperty to OtherProperty."; var actionContext = new ActionContext(); var context = new ClientModelValidationContext( actionContext, metadata, metadataProvider, new AttributeDictionary()); // 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-equalto", kvp.Key); Assert.Equal(expectedMessage, kvp.Value); }, kvp => { Assert.Equal("data-val-equalto-other", kvp.Key); Assert.Equal(kvp.Value, "*.OtherProperty"); }); }
/// <summary> /// Creates an <see cref="IAttributeAdapter"/> for the given attribute. /// </summary> /// <param name="attribute">The attribute to create an adapter for.</param> /// <param name="stringLocalizer">The localizer to provide to the adapter.</param> /// <returns>An <see cref="IAttributeAdapter"/> for the given attribute.</returns> public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer) { if (attribute == null) { throw new ArgumentNullException(nameof(attribute)); } IAttributeAdapter adapter; var type = attribute.GetType(); if (type == typeof(RegularExpressionAttribute)) { adapter = new RegularExpressionAttributeAdapter((RegularExpressionAttribute)attribute, stringLocalizer); } else if (type == typeof(MaxLengthAttribute)) { adapter = new MaxLengthAttributeAdapter((MaxLengthAttribute)attribute, stringLocalizer); } else if (type == typeof(RequiredAttribute)) { adapter = new RequiredAttributeAdapter((RequiredAttribute)attribute, stringLocalizer); } else if (type == typeof(CompareAttribute)) { adapter = new CompareAttributeAdapter((CompareAttribute)attribute, stringLocalizer); } else if (type == typeof(MinLengthAttribute)) { adapter = new MinLengthAttributeAdapter((MinLengthAttribute)attribute, stringLocalizer); } else if (type == typeof(CreditCardAttribute)) { adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-creditcard", stringLocalizer); } else if (type == typeof(StringLengthAttribute)) { adapter = new StringLengthAttributeAdapter((StringLengthAttribute)attribute, stringLocalizer); } else if (type == typeof(RangeAttribute)) { adapter = new RangeAttributeAdapter((RangeAttribute)attribute, stringLocalizer); } else if (type == typeof(EmailAddressAttribute)) { adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-email", stringLocalizer); } else if (type == typeof(PhoneAttribute)) { adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-phone", stringLocalizer); } else if (type == typeof(UrlAttribute)) { adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-url", stringLocalizer); } else { adapter = null; } return(adapter); }