Example #1
1
        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 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 Dictionary <string, string>());

        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_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 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-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); });
    }
Example #4
0
        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); });
        }
Example #5
0
        public void AddValidation_adds_after_and_before_rules_for_nullables()
        {
            // Arrange
            var attribute = new RangeAttribute(typeof(DateTime?), "2016-03-01", "2016-03-31");
            var adapter   = new RangeAttributeAdapter(attribute, _options);

            var context = new ClientModelValidationContextBuilder()
                          .WithModelType <DateTime?>()
                          .Build();

            // Act
            adapter.AddValidation(context);

            // Assert
            context.Attributes.Keys.ShouldContain("v-validate");
            context.Attributes["v-validate"].ShouldBe("{date_format:'dd/MM/yyyy',date_between:['01/03/2016','31/03/2016',true]}");
        }
Example #6
0
        public void AddValidation_adds_min_value_and_max_value_rules()
        {
            // Arrange
            var attribute = new RangeAttribute(1, 100);
            var adapter   = new RangeAttributeAdapter(attribute, _options);

            var context = new ClientModelValidationContextBuilder()
                          .WithModelType <int>()
                          .Build();

            // Act
            adapter.AddValidation(context);

            // Assert
            context.Attributes.Keys.ShouldContain("v-validate");
            context.Attributes["v-validate"].ShouldBe("{max_value:100,min_value:1}");
        }
Example #7
0
        public void AddValidation_adds_after_and_before_rules(string minDate, string maxDate)
        {
            // Arrange
            CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-GB"); // Set culture for date string conversion

            var attribute = new RangeAttribute(typeof(DateTime), minDate, maxDate);
            var adapter   = new RangeAttributeAdapter(attribute, _options);

            var context = new ClientModelValidationContextBuilder()
                          .WithModelType <DateTime>()
                          .Build();

            // Act
            adapter.AddValidation(context);

            // Assert
            context.Attributes.Keys.ShouldContain("v-validate");
            context.Attributes["v-validate"].ShouldBe("{date_format:'dd/MM/yyyy',date_between:['01/03/2016','31/03/2016',true]}");
        }