public void AddValidation_DoesNotTrounceExistingAttributes() { // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); var attribute = new RangeAttribute(typeof(decimal), "0", "100"); attribute.ErrorMessage = "The field Length must be between {1} and {2}."; var adapter = new RangeAttributeAdapter(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-range", "original"); context.Attributes.Add("data-val-range-max", "original"); context.Attributes.Add("data-val-range-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-range", kvp.Key); Assert.Equal("original", kvp.Value); }, kvp => { Assert.Equal("data-val-range-max", kvp.Key); Assert.Equal("original", kvp.Value); }, kvp => { Assert.Equal("data-val-range-min", kvp.Key); Assert.Equal("original", kvp.Value); }); }
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 void AddValidation_CorrectValidationTypeAndOverriddenErrorMessage() { // Arrange var expectedMessage = "Error message about 'DisplayId' from override."; var provider = new TestModelMetadataProvider(); provider .ForProperty(typeof(TypeWithNumericProperty), nameof(TypeWithNumericProperty.Id)) .BindingDetails(d => d.ModelBindingMessageProvider.ValueMustBeANumberAccessor = name => $"Error message about '{ name }' from override."); var metadata = provider.GetMetadataForProperty( typeof(TypeWithNumericProperty), nameof(TypeWithNumericProperty.Id)); var adapter = new NumericClientModelValidator(); 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-number", kvp.Key); Assert.Equal(expectedMessage, kvp.Value); }); }
public void AddValidation_WithLocalization() { // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); var attribute = new RangeAttribute(typeof(decimal), "0", "100"); attribute.ErrorMessage = "The field Length must be between {1} and {2}."; var expectedProperties = new object[] { "Length", 0m, 100m }; var expectedMessage = "The field Length must be between 0 and 100."; var stringLocalizer = new Mock<IStringLocalizer>(); stringLocalizer .Setup(s => s[attribute.ErrorMessage, expectedProperties]) .Returns(new LocalizedString(attribute.ErrorMessage, expectedMessage)); var adapter = new RangeAttributeAdapter(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-range", kvp.Key); Assert.Equal(expectedMessage, kvp.Value); }, kvp => { Assert.Equal("data-val-range-max", kvp.Key); Assert.Equal("100", kvp.Value); }, kvp => { Assert.Equal("data-val-range-min", kvp.Key); Assert.Equal("0", kvp.Value); }); }
public void AddValidation_AddsValidation_Localize() { // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); var attribute = new RequiredAttribute(); var expectedProperties = new object[] { "Length" }; var message = "This paramter is required."; var expectedMessage = "FR This parameter is required."; attribute.ErrorMessage = message; var stringLocalizer = new Mock<IStringLocalizer>(); stringLocalizer.Setup(s => s[attribute.ErrorMessage, expectedProperties]) .Returns(new LocalizedString(attribute.ErrorMessage, expectedMessage)); var adapter = new RequiredAttributeAdapter(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-required", kvp.Key); Assert.Equal(expectedMessage, kvp.Value); }); }
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 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 AttributeDictionary()); 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 EqualToAdapterTests() { attributes = new Dictionary<String, String>(); adapter = new EqualToAdapter(new EqualToAttribute("StringLength")); IModelMetadataProvider provider = new EmptyModelMetadataProvider(); ModelMetadata metadata = provider.GetMetadataForProperty(typeof(AdaptersModel), "EqualTo"); context = new ClientModelValidationContext(new ActionContext(), metadata, provider, attributes); }
public FileSizeAdapterTests() { attributes = new Dictionary<String, String>(); adapter = new FileSizeAdapter(new FileSizeAttribute(12.25)); IModelMetadataProvider provider = new EmptyModelMetadataProvider(); ModelMetadata metadata = provider.GetMetadataForProperty(typeof(AdaptersModel), "FileSize"); context = new ClientModelValidationContext(new ActionContext(), metadata, provider, attributes); }
/// <inheritdoc /> public void AddValidation(ClientModelValidationContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } MergeAttribute(context.Attributes, "data-val", "true"); MergeAttribute(context.Attributes, "data-val-number", GetErrorMessage(context.ModelMetadata)); }
public void AddValidation_Date() { IModelMetadataProvider provider = new EmptyModelMetadataProvider(); ModelMetadata metadata = provider.GetMetadataForType(typeof(DateTime)); Dictionary<String, String> attributes = new Dictionary<String, String>(); ClientModelValidationContext context = new ClientModelValidationContext(new ActionContext(), metadata, provider, attributes); new DateValidator().AddValidation(context); Assert.Equal(2, attributes.Count); Assert.Equal("true", attributes["data-val"]); Assert.Equal(String.Format(Validations.Date, "DateTime"), attributes["data-val-date"]); }
public void AddValidation(ClientModelValidationContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } MergeAttribute(context.Attributes, "data-val", "true"); MergeAttribute(context.Attributes, "data-val-classicmovie", GetErrorMessage()); var year = _year.ToString(CultureInfo.InvariantCulture); MergeAttribute(context.Attributes, "data-val-classicmovie-year", year); }
public void AddValidation(ClientModelValidationContext context) { _propertyDisplayName = context.ModelMetadata.GetDisplayName(); IEnumerable<CustomAttributeData> attributes = context.ModelMetadata.ContainerType.GetProperty(_otherPropertyName).CustomAttributes; foreach (CustomAttributeData itm in attributes) { if (itm.AttributeType.Name != "DisplayAttribute") { continue; } _otherPropertyDisplayName = ((CustomAttributeNamedArgument)itm.NamedArguments[0]).TypedValue.Value.ToString(); break; } string errorMessage = FormatErrorMessage(_propertyDisplayName); context.Attributes.Add("data-val-begreaterthan", errorMessage); context.Attributes.Add("data-val-begreaterthan-otherpropertyname", _otherPropertyName); context.Attributes.Add("data-val-begreaterthan-prefix", _prefix); }
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 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 AddValidation_CorrectValidationTypeAndErrorMessage() { // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(TypeWithNumericProperty), "Id"); var adapter = new NumericClientModelValidator(); var actionContext = new ActionContext(); var context = new ClientModelValidationContext(actionContext, metadata, provider, new AttributeDictionary()); var expectedMessage = "The field DisplayId must be a number."; // 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-number", kvp.Key); Assert.Equal(expectedMessage, kvp.Value); }); }
public void AddValidation_AddsValidation() { // Arrange var expectedMessage = ValidationAttributeUtil.GetRequiredErrorMessage("Length"); var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); var attribute = new RequiredAttribute(); var adapter = new RequiredAttributeAdapter(attribute, stringLocalizer: null); 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-required", kvp.Key); Assert.Equal(expectedMessage, kvp.Value); }); }
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); }); }
public void AddValidation_WithMaxLength_AddsAttributes_Localize() { // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); var attribute = new StringLengthAttribute(8); attribute.ErrorMessage = "Property must not be longer than '{1}' characters."; var expectedMessage = "Property must not be longer than '8' characters."; var stringLocalizer = new Mock<IStringLocalizer>(); var expectedProperties = new object[] { "Length", 0, 8 }; stringLocalizer.Setup(s => s[attribute.ErrorMessage, expectedProperties]) .Returns(new LocalizedString(attribute.ErrorMessage, expectedMessage)); var adapter = new StringLengthAttributeAdapter(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-length", kvp.Key); Assert.Equal(expectedMessage, kvp.Value); }, kvp => { Assert.Equal("data-val-length-max", kvp.Key); Assert.Equal("8", kvp.Value); }); }
public void AddValidation_WithMaxLength_AtIntMaxValue_AddsAttributes() { // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); var attribute = new StringLengthAttribute(int.MaxValue); 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); }); }
public void AddValidation(ClientModelValidationContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } CheckForLocalizer(context); var errorMessage = GetErrorMessage(context.ModelMetadata.GetDisplayName()); MergeAttribute(context.Attributes, "data-val", "true"); MergeAttribute(context.Attributes, "data-val-requiredwhen", errorMessage); MergeAttribute(context.Attributes, "data-val-other", "#" + DependentProperty); MergeAttribute(context.Attributes, "data-val-otherval", TargetValue.ToString()); }
private void CheckForLocalizer(ClientModelValidationContext context) { if (!_checkedForLocalizer) { _checkedForLocalizer = true; var services = context.ActionContext.HttpContext.RequestServices; var options = services.GetRequiredService<IOptions<MvcDataAnnotationsLocalizationOptions>>(); var factory = services.GetService<IStringLocalizerFactory>(); var provider = options.Value.DataAnnotationLocalizerProvider; if (factory != null && provider != null) { _stringLocalizer = provider( context.ModelMetadata.ContainerType ?? context.ModelMetadata.ModelType, factory); } } }
public void AddValidation(ClientModelValidationContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } CheckForLocalizer(context); var errorMessage = GetErrorMessage(context.ModelMetadata.GetDisplayName()); MergeAttribute(context.Attributes, "data-val", "true"); MergeAttribute(context.Attributes, "data-val-enforcetrue", errorMessage); MergeAttribute(context.Attributes, "data-val-other", "#" + OtherProperty); }
public void AddValidation_DoesNotTrounceExistingAttributes() { // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(TypeWithNumericProperty), "Id"); var adapter = new NumericClientModelValidator(); var actionContext = new ActionContext(); var context = new ClientModelValidationContext(actionContext, metadata, provider, new AttributeDictionary()); context.Attributes.Add("data-val", "original"); context.Attributes.Add("data-val-number", "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-number", kvp.Key); Assert.Equal("original", kvp.Value); }); }
public void AddValidation(ClientModelValidationContext context) { _propertyDisplayName = context.ModelMetadata.DisplayName ?? context.ModelMetadata.PropertyName; string errorMessage = FormatErrorMessage(_propertyDisplayName); context.Attributes.Add("data-val-greaterthanzero", errorMessage); }
public void AddValidation(ClientModelValidationContext context) { MergeAttribute(context.Attributes, "data-val", "true"); MergeAttribute(context.Attributes, "data-val-nasvalidation", FormatErrorMessage(context.ModelMetadata.PropertyName)); }
public static ClientModelValidationContext AddValidationRule(this ClientModelValidationContext context, string ruleName, object value) { return(AddValidationRule(context, ruleName, value.ToString())); }
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"); }); }
public void MaxLengthAttribute_AddValidation_StringLocalizer_ReturnsLocalizedErrorString() { // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); var errorKey = metadata.GetDisplayName(); var attribute = new MaxLengthAttribute(10); attribute.ErrorMessage = errorKey; var localizedString = new LocalizedString(errorKey, "Longueur est invalide"); var stringLocalizer = new Mock<IStringLocalizer>(); stringLocalizer.Setup(s => s[errorKey, metadata.GetDisplayName(), attribute.Length]).Returns(localizedString); var expectedMessage = "Longueur est invalide"; var adapter = new MaxLengthAttributeAdapter(attribute, 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-maxlength", kvp.Key); Assert.Equal(expectedMessage, kvp.Value); }, kvp => { Assert.Equal("data-val-maxlength-max", kvp.Key); Assert.Equal("10", kvp.Value); }); }
public void MaxLengthAttribute_AddValidation_CustomMessage() { // Arrange var propertyName = "Length"; var message = "{0} must be at most {1}"; var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), propertyName); var expectedMessage = "Length must be at most 5"; var attribute = new MaxLengthAttribute(5) { ErrorMessage = message }; var adapter = new MaxLengthAttributeAdapter(attribute, stringLocalizer: null); 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-maxlength", kvp.Key); Assert.Equal(expectedMessage, kvp.Value); }, kvp => { Assert.Equal("data-val-maxlength-max", kvp.Key); Assert.Equal("5", kvp.Value); }); }
public void AddValidation(ClientModelValidationContext context) { }
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); // Mono issue - https://github.com/aspnet/External/issues/19 var expectedMessage = PlatformNormalizer.NormalizeContent("'MyProperty' and 'OtherProperty' 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 AddValidation(ClientModelValidationContext context) { context.Attributes["data-val"] = "true"; context.Attributes["data-val-date"] = String.Format(Validations.Date, context.ModelMetadata.GetDisplayName()); }
public static ClientModelValidationContext AddValidationRule(this ClientModelValidationContext context, string ruleName, string value) { VueHtmlAttributeHelper.MergeVeeValidateAttribute(context.Attributes, ruleName, value.ToString()); return(context); }