public void ClientRulesWithCompareAttribute_ErrorMessageUsesPropertyName() { // Arrange var metadataProvider = new DataAnnotationsModelMetadataProvider(); var metadata = metadataProvider.GetMetadataForProperty(() => null, typeof(PropertyNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty"); var context = new ClientModelValidationContext(metadata, metadataProvider); var adapter = new CompareAttributeAdapter(attribute); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); Assert.Equal("'MyProperty' and 'OtherProperty' do not match.", rule.ErrorMessage); }
public void GetClientValidationRules_ReturnsValidationParameters() { // Arrange var provider = new DataAnnotationsModelMetadataProvider(); var metadata = provider.GetMetadataForProperty(() => null, typeof(string), "Length"); var attribute = new RequiredAttribute(); var adapter = new RequiredAttributeAdapter(attribute); var context = new ClientModelValidationContext(metadata, provider); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); Assert.Equal("required", rule.ValidationType); Assert.Empty(rule.ValidationParameters); Assert.Equal("The Length field is required.", rule.ErrorMessage); }
public void ClientRulesWithMaxLengthAttribute() { // Arrange var provider = new DataAnnotationsModelMetadataProvider(); var metadata = provider.GetMetadataForProperty(() => null, typeof(string), "Length"); var attribute = new MaxLengthAttribute(10); var adapter = new MaxLengthAttributeAdapter(attribute); var context = new ClientModelValidationContext(metadata, provider); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); Assert.Equal("maxlength", rule.ValidationType); Assert.Equal(1, rule.ValidationParameters.Count); Assert.Equal(10, rule.ValidationParameters["max"]); Assert.Equal("The field Length must be a string or array type with a maximum length of '10'.", rule.ErrorMessage); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesResourceOverride() { // Arrange var metadataProvider = new DataAnnotationsModelMetadataProvider(); var metadata = metadataProvider.GetMetadataForProperty(() => null, typeof(PropertyNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty") { ErrorMessageResourceName = "CompareAttributeTestResource", ErrorMessageResourceType = typeof(Test.Resources), }; var context = new ClientModelValidationContext(metadata, metadataProvider); var adapter = new CompareAttributeAdapter(attribute); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); Assert.Equal("Comparing MyProperty to OtherProperty.", rule.ErrorMessage); }
public void ClientRulesWithMaxLengthAttribute() { // Arrange var provider = new DataAnnotationsModelMetadataProvider(); var metadata = provider.GetMetadataForProperty(() => null, typeof(string), "Length"); var attribute = new MaxLengthAttribute(10); var adapter = new MaxLengthAttributeAdapter(attribute); var context = new ClientModelValidationContext(metadata, provider); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); Assert.Equal("maxlength", rule.ValidationType); Assert.Equal(1, rule.ValidationParameters.Count); Assert.Equal(10, rule.ValidationParameters["max"]); Assert.Equal(attribute.FormatErrorMessage("Length"), rule.ErrorMessage); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesOverride() { // Arrange var metadataProvider = new DataAnnotationsModelMetadataProvider(); var metadata = metadataProvider.GetMetadataForProperty(() => null, typeof(PropertyNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty") { ErrorMessage = "Hello '{0}', goodbye '{1}'." }; var context = new ClientModelValidationContext(metadata, metadataProvider); var adapter = new CompareAttributeAdapter(attribute); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); Assert.Equal("Hello 'MyProperty', goodbye 'OtherProperty'.", rule.ErrorMessage); }
public void ClientRulesWithMinLengthAttribute() { // Arrange var provider = new DataAnnotationsModelMetadataProvider(); var metadata = provider.GetMetadataForProperty(() => null, typeof(string), "Length"); var attribute = new MinLengthAttribute(6); 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(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 GetClientValidationRules_ReturnsValidationParameters() { // Arrange var provider = new DataAnnotationsModelMetadataProvider(); var metadata = provider.GetMetadataForProperty(() => null, typeof(string), "Length"); var attribute = new RangeAttribute(typeof(decimal), "0", "100"); var adapter = new RangeAttributeAdapter(attribute); var context = new ClientModelValidationContext(metadata, provider); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); Assert.Equal("range", rule.ValidationType); Assert.Equal(2, rule.ValidationParameters.Count); Assert.Equal(0m, rule.ValidationParameters["min"]); Assert.Equal(100m, rule.ValidationParameters["max"]); Assert.Equal(@"The field Length must be between 0 and 100.", rule.ErrorMessage); }
public void ClientRulesWithMaxLengthAttributeAndCustomMessage() { // Arrange var propertyName = "Length"; var message = "{0} must be at most {1}"; var provider = new DataAnnotationsModelMetadataProvider(); var metadata = provider.GetMetadataForProperty(() => null, typeof(string), propertyName); var attribute = new MaxLengthAttribute(5) { ErrorMessage = message }; var adapter = new MaxLengthAttributeAdapter(attribute); var context = new ClientModelValidationContext(metadata, provider); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); Assert.Equal("maxlength", rule.ValidationType); Assert.Equal(1, rule.ValidationParameters.Count); Assert.Equal(5, rule.ValidationParameters["max"]); Assert.Equal("Length must be at most 5", rule.ErrorMessage); }
public void GetClientValidationRules_WithMaxLength_ReturnsValidationParameters() { // Arrange var provider = new DataAnnotationsModelMetadataProvider(); var metadata = provider.GetMetadataForProperty(() => null, typeof(string), "Length"); var attribute = new StringLengthAttribute(8); var adapter = new StringLengthAttributeAdapter(attribute); var context = new ClientModelValidationContext(metadata, provider); // 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("The field Length must be a string with a maximum length of 8.", rule.ErrorMessage); }
public void GetClientValidationRules_WithMinAndMaxLength_ReturnsValidationParameters() { // Arrange var provider = new DataAnnotationsModelMetadataProvider(); var metadata = provider.GetMetadataForProperty(() => null, typeof(string), "Length"); var attribute = new StringLengthAttribute(10) { MinimumLength = 3 }; var adapter = new StringLengthAttributeAdapter(attribute); var context = new ClientModelValidationContext(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("The field Length must be a string with a minimum length of 3 and a maximum length of 10.", rule.ErrorMessage); }
private string GetOtherPropertyDisplayName(ClientModelValidationContext context) { // The System.ComponentModel.DataAnnotations.CompareAttribute doesn't populate the // OtherPropertyDisplayName until after IsValid() is called. Therefore, by the time we get // the error message for client validation, the display name is not populated and won't be used. var metadata = context.ModelMetadata; var otherPropertyDisplayName = OtherPropertyDisplayName; if (otherPropertyDisplayName == null && metadata.ContainerType != null) { var otherProperty = context.MetadataProvider .GetMetadataForProperty(() => metadata.Model, metadata.ContainerType, OtherProperty); if (otherProperty != null) { return(otherProperty.GetDisplayName()); } } return(OtherProperty); }
public virtual IEnumerable <ModelClientValidationRule> GetClientValidationRules( [NotNull] ClientModelValidationContext context) { return(Enumerable.Empty <ModelClientValidationRule>()); }