Inheritance: System.ComponentModel.DataAnnotations.ValidationAttribute
        public void GreaterThanAttribute_SetsMinimumFromDouble()
        {
            Decimal actual = new GreaterThanAttribute(12.56).Minimum;
            Decimal expected = 12.56M;

            Assert.Equal(expected, actual);
        }
        public void GreaterThanAttribute_SetsMinimumFromInteger()
        {
            Decimal actual = new GreaterThanAttribute(10).Minimum;
            Decimal expected = 10M;

            Assert.Equal(expected, actual);
        }
        public void FormatErrorMessage_ForDouble()
        {
            attribute = new GreaterThanAttribute(12.56);

            String expected = String.Format(Validations.FieldMustBeGreaterThan, "Sum", attribute.Minimum);
            String actual = attribute.FormatErrorMessage("Sum");

            Assert.Equal(expected, actual);
        }
        public void FormatErrorMessage_ForInteger()
        {
            attribute = new GreaterThanAttribute(10);

            String expected = String.Format(Validations.GreaterThan, "Sum", attribute.Minimum);
            String actual = attribute.FormatErrorMessage("Sum");

            Assert.Equal(expected, actual);
        }
        public void GetClientValidationRules_ReturnsGreaterValidationRule()
        {
            ModelMetadata metadata = new DataAnnotationsModelMetadataProvider().GetMetadataForProperty(null, typeof(AdaptersModel), "GreaterThan");
            GreaterThanAdapter adapter = new GreaterThanAdapter(metadata, new ControllerContext(), new GreaterThanAttribute(128));

            String expectedMessage = new GreaterThanAttribute(128).FormatErrorMessage(metadata.GetDisplayName());
            ModelClientValidationRule actual = adapter.GetClientValidationRules().Single();

            Assert.Equal(128M, actual.ValidationParameters["min"]);
            Assert.Equal(expectedMessage, actual.ErrorMessage);
            Assert.Equal("greater", actual.ValidationType);
            Assert.Single(actual.ValidationParameters);
        }
        public void GetClientValidationRules_ReturnsMinRangeValidationRule()
        {
            ModelMetadata metadata = new DataAnnotationsModelMetadataProvider().GetMetadataForProperty(null, typeof(AdaptersModel), "MinValue");
            GreaterThanAdapter adapter = new GreaterThanAdapter(metadata, new ControllerContext(), new GreaterThanAttribute(128));
            String errorMessage = new GreaterThanAttribute(128).FormatErrorMessage(metadata.GetDisplayName());

            ModelClientValidationRule actual = adapter.GetClientValidationRules().Single();
            ModelClientValidationRule expected = new ModelClientValidationRule();
            expected.ValidationParameters.Add("min", 128M);
            expected.ErrorMessage = errorMessage;
            expected.ValidationType = "greater";

            Assert.Equal(expected.ValidationParameters["min"], actual.ValidationParameters["min"]);
            Assert.Equal(expected.ValidationParameters.Count, actual.ValidationParameters.Count);
            Assert.Equal(expected.ValidationType, actual.ValidationType);
            Assert.Equal(expected.ErrorMessage, actual.ErrorMessage);
        }
示例#7
0
 public IAttributeAdapter?GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer?stringLocalizer)
 {
     return(attribute switch
     {
         StringLengthAttribute stringLength => new StringLengthAdapter(stringLength),
         GreaterThanAttribute greaterThan => new GreaterThanAdapter(greaterThan),
         AcceptFilesAttribute acceptFiles => new AcceptFilesAdapter(acceptFiles),
         MinLengthAttribute minLength => new MinLengthAdapter(minLength),
         EmailAddressAttribute email => new EmailAddressAdapter(email),
         RequiredAttribute required => new RequiredAdapter(required),
         MaxValueAttribute maxValue => new MaxValueAdapter(maxValue),
         MinValueAttribute minValue => new MinValueAdapter(minValue),
         LessThanAttribute lessThan => new LessThanAdapter(lessThan),
         FileSizeAttribute fileSize => new FileSizeAdapter(fileSize),
         EqualToAttribute equalTo => new EqualToAdapter(equalTo),
         IntegerAttribute integer => new IntegerAdapter(integer),
         DigitsAttribute digits => new DigitsAdapter(digits),
         NumberAttribute number => new NumberAdapter(number),
         RangeAttribute range => new RangeAdapter(range),
         _ => null
     });
 public GreaterThanAttributeTests()
 {
     attribute = new GreaterThanAttribute(12.56);
 }