public void AddValidation_DoesNotTrounceExistingAttributes() { // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); var attribute = new StringLengthAttribute(10) { MinimumLength = 3 }; var adapter = new StringLengthAttributeAdapter(attribute, stringLocalizer: null); var expectedMessage = attribute.FormatErrorMessage("Length"); var actionContext = new ActionContext(); var context = new ClientModelValidationContext(actionContext, metadata, provider, new AttributeDictionary()); context.Attributes.Add("data-val", "original"); context.Attributes.Add("data-val-length", "original"); context.Attributes.Add("data-val-length-max", "original"); context.Attributes.Add("data-val-length-min", "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-length", kvp.Key); Assert.Equal("original", kvp.Value); }, kvp => { Assert.Equal("data-val-length-max", kvp.Key); Assert.Equal("original", kvp.Value); }, kvp => { Assert.Equal("data-val-length-min", kvp.Key); Assert.Equal("original", kvp.Value); }); }
public static string GetStringLengthErrorMessage(int? minimumLength, int maximumLength, string field) { var attr = new StringLengthAttribute(maximumLength); if (minimumLength != null) { attr.MinimumLength = (int)minimumLength; } return attr.FormatErrorMessage(field); }
public void GetClientValidationRules_WithMaxLength_ReturnsValidationParameters() { // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); var attribute = new StringLengthAttribute(8); var adapter = new StringLengthAttributeAdapter(attribute); var serviceCollection = new ServiceCollection(); var requestServices = serviceCollection.BuildServiceProvider(); var context = new ClientModelValidationContext(metadata, provider, requestServices); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); Assert.Equal("length", rule.ValidationType); Assert.Equal(1, rule.ValidationParameters.Count); Assert.Equal(8, rule.ValidationParameters["max"]); Assert.Equal(attribute.FormatErrorMessage("Length"), rule.ErrorMessage); }
public void AddValidation_WithMaxLength_AddsAttributes() { // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); var attribute = new StringLengthAttribute(8); var adapter = new StringLengthAttributeAdapter(attribute, stringLocalizer: null); var expectedMessage = attribute.FormatErrorMessage("Length"); var actionContext = new ActionContext(); var context = new ClientModelValidationContext(actionContext, metadata, provider, 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-length", kvp.Key); Assert.Equal(expectedMessage, kvp.Value); }, kvp => { Assert.Equal("data-val-length-max", kvp.Key); Assert.Equal("8", kvp.Value); }); }
public void GetClientValidationRules_WithMinAndMaxLength_ReturnsValidationParameters() { // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); var attribute = new StringLengthAttribute(10) { MinimumLength = 3 }; var adapter = new StringLengthAttributeAdapter(attribute, stringLocalizer: null); var actionContext = new ActionContext(); var context = new ClientModelValidationContext(actionContext, metadata, provider); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); Assert.Equal("length", rule.ValidationType); Assert.Equal(2, rule.ValidationParameters.Count); Assert.Equal(3, rule.ValidationParameters["min"]); Assert.Equal(10, rule.ValidationParameters["max"]); Assert.Equal(attribute.FormatErrorMessage("Length"), rule.ErrorMessage); }
public void Entity_Invoke_Validates_Property_Validation() { TestEntityContainer container = new TestEntityContainer(); var invocableCity = new City("This is an invalid state name"); container.GetEntitySet<City>().Add(invocableCity); invocableCity.Name = "Redmond"; StringLengthAttribute expectedAttribute = new StringLengthAttribute(2); string expectedMember = "StateName"; string expectedError = expectedAttribute.FormatErrorMessage(expectedMember); ExceptionHelper.ExpectValidationException(delegate { invocableCity.AssignCityZone("West"); }, expectedError, expectedAttribute.GetType(), invocableCity.StateName); }