public void IsValid_throws_for_negative_lengths() { var attribute = new MinLengthAttribute(-1); Assert.Equal( Strings.MinLengthAttribute_InvalidMinLength, Assert.Throws<InvalidOperationException>(() => attribute.IsValid("Rincewind")).Message); }
public void AddValidation_DoesNotTrounceExistingAttributes() { // Arrange var propertyName = "Length"; var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), propertyName); var attribute = new MinLengthAttribute(2) { ErrorMessage = "Array must have at least {1} items." }; var adapter = new MinLengthAttributeAdapter(attribute, stringLocalizer: null); var actionContext = new ActionContext(); var context = new ClientModelValidationContext(actionContext, metadata, provider, new AttributeDictionary()); context.Attributes.Add("data-val", "original"); context.Attributes.Add("data-val-minlength", "original"); context.Attributes.Add("data-val-minlength-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-minlength", kvp.Key); Assert.Equal("original", kvp.Value); }, kvp => { Assert.Equal("data-val-minlength-min", kvp.Key); Assert.Equal("original", kvp.Value); }); }
public static void GetValidationResult_throws_for_negative_lengths() { var attribute = new MinLengthAttribute(-1); Assert.Throws <InvalidOperationException>( () => attribute.GetValidationResult("Rincewind", s_testValidationContext)); }
public void ClientRulesWithMinLengthAttribute_Localize() { // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); var attribute = new MinLengthAttribute(6); attribute.ErrorMessage = "Property must be at least '{1}' characters long."; var expectedProperties = new object[] { "Length", 6 }; var expectedMessage = "Property must be at least '6' characters long."; var stringLocalizer = new Mock<IStringLocalizer>(); stringLocalizer.Setup(s => s[attribute.ErrorMessage, expectedProperties]) .Returns(new LocalizedString(attribute.ErrorMessage, expectedMessage)); var adapter = new MinLengthAttributeAdapter(attribute, stringLocalizer: stringLocalizer.Object); 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("minlength", rule.ValidationType); Assert.Equal(1, rule.ValidationParameters.Count); Assert.Equal(6, rule.ValidationParameters["min"]); Assert.Equal(attribute.FormatErrorMessage("Length"), rule.ErrorMessage); }
public void IsValid_throws_for_negative_lengths() { var attribute = new MinLengthAttribute(-1); Assert.Equal( Strings.MinLengthAttribute_InvalidMinLength, Assert.Throws <InvalidOperationException>(() => attribute.IsValid("Rincewind")).Message); }
public void MinLengthAdapter_SetsErrorMessage() { ModelMetadata metadata = new DataAnnotationsModelMetadataProvider() .GetMetadataForProperty(null, typeof(AdaptersModel), "MinLength"); MinLengthAttribute attribute = new MinLengthAttribute(128); new MinLengthAdapter(metadata, new ControllerContext(), attribute); String expected = Validations.FieldMustBeWithMinLengthOf; String actual = attribute.ErrorMessage; Assert.Equal(expected, actual); }
public void ClientRulesWithMinLengthAttribute() { // Arrange var metadata = ModelMetadataProviders.Current.GetMetadataForProperty(() => null, typeof(string), "Length"); var context = new ControllerContext(); var attribute = new MinLengthAttribute(6); var adapter = new MinLengthAttributeAdapter(metadata, context, attribute); // Act var rules = adapter.GetClientValidationRules(); // Assert ModelClientValidationRule rule = Assert.Single(rules); Assert.Equal("minlength", rule.ValidationType); Assert.Equal(1, rule.ValidationParameters.Count); Assert.Equal(6, rule.ValidationParameters["min"]); Assert.Equal("The field Length must be a string or array type with a minimum length of '6'.", rule.ErrorMessage); }
public void ClientRulesWithMinLengthAttributeAndCustomMessage() { // Arrange string propertyName = "Length"; string message = "Array must have at least {1} items."; var metadata = ModelMetadataProviders.Current.GetMetadataForProperty(() => null, typeof(int[]), propertyName); var context = new ControllerContext(); var attribute = new MinLengthAttribute(2) { ErrorMessage = message }; var adapter = new MinLengthAttributeAdapter(metadata, context, attribute); // Act var rules = adapter.GetClientValidationRules(); // Assert ModelClientValidationRule rule = Assert.Single(rules); Assert.Equal("minlength", rule.ValidationType); Assert.Equal(1, rule.ValidationParameters.Count); Assert.Equal(2, rule.ValidationParameters["min"]); Assert.Equal("Array must have at least 2 items.", rule.ErrorMessage); }
public void ClientRulesWithMinLengthAttribute() { // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); var attribute = new MinLengthAttribute(6); var adapter = new MinLengthAttributeAdapter(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("minlength", rule.ValidationType); Assert.Equal(1, rule.ValidationParameters.Count); Assert.Equal(6, rule.ValidationParameters["min"]); Assert.Equal(attribute.FormatErrorMessage("Length"), rule.ErrorMessage); }
public void ClientRulesWithMinLengthAttributeAndCustomMessage() { // Arrange var propertyName = "Length"; var message = "Array must have at least {1} items."; var provider = new DataAnnotationsModelMetadataProvider(); var metadata = provider.GetMetadataForProperty(() => null, typeof(string), propertyName); var attribute = new MinLengthAttribute(2) { ErrorMessage = message }; var adapter = new MinLengthAttributeAdapter(attribute); var context = new ClientModelValidationContext(metadata, provider); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); Assert.Equal("minlength", rule.ValidationType); Assert.Equal(1, rule.ValidationParameters.Count); Assert.Equal(2, rule.ValidationParameters["min"]); Assert.Equal("Array must have at least 2 items.", rule.ErrorMessage); }
public void ClientRulesWithMinLengthAttributeAndCustomMessage() { // Arrange var propertyName = "Length"; var message = "Array must have at least {1} items."; var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), propertyName); var attribute = new MinLengthAttribute(2) { ErrorMessage = message }; var adapter = new MinLengthAttributeAdapter(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("minlength", rule.ValidationType); Assert.Equal(1, rule.ValidationParameters.Count); Assert.Equal(2, rule.ValidationParameters["min"]); Assert.Equal("Array must have at least 2 items.", rule.ErrorMessage); }
public void MinLengthAttribute_AddValidation_Attribute() { // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); var attribute = new MinLengthAttribute(6); var adapter = new MinLengthAttributeAdapter(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-minlength", kvp.Key); Assert.Equal(expectedMessage, kvp.Value); }, kvp => { Assert.Equal("data-val-minlength-min", kvp.Key); Assert.Equal("6", kvp.Value); }); }
public MinLengthAdapter(ModelMetadata metadata, ControllerContext context, MinLengthAttribute attribute) : base(metadata, context, attribute) { Attribute.ErrorMessage = Validations.FieldMustBeWithMinLengthOf; }
/// <summary> /// Initializes a new instance of the LocalizedMinLengthAttributeAdapter class. /// </summary> /// <param name="metadata">The model metadata.</param> /// <param name="context">The controller context.</param> /// <param name="attribute">The MinLength attribute.</param> public LocalizedMinLengthAttributeAdapter(ModelMetadata metadata, ControllerContext context, MinLengthAttribute attribute) : base(metadata, context, attribute) { }
public static string GetMinLengthErrorMessage(int length, string field) { var attr = new MinLengthAttribute(length); return attr.FormatErrorMessage(field); }
public static void GetValidationResult_throws_for_negative_lengths() { var attribute = new MinLengthAttribute(-1); Assert.Throws<InvalidOperationException>( () => attribute.GetValidationResult("Rincewind", s_testValidationContext)); }
public MinLengthAdapter(MinLengthAttribute attribute) : base(attribute, null) { }
public void MinLengthAttribute_AddValidation_Localize() { // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); var attribute = new MinLengthAttribute(6); attribute.ErrorMessage = "Property must be at least '{1}' characters long."; var expectedProperties = new object[] { "Length", 6 }; var expectedMessage = "Property must be at least '6' characters long."; var stringLocalizer = new Mock<IStringLocalizer>(); stringLocalizer.Setup(s => s[attribute.ErrorMessage, expectedProperties]) .Returns(new LocalizedString(attribute.ErrorMessage, expectedMessage)); var adapter = new MinLengthAttributeAdapter(attribute, stringLocalizer: stringLocalizer.Object); 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-minlength", kvp.Key); Assert.Equal(expectedMessage, kvp.Value); }, kvp => { Assert.Equal("data-val-minlength-min", kvp.Key); Assert.Equal("6", kvp.Value); }); }