Esempio n. 1
0
        public void DefaultErrorMessage_LessThanOrEqualValidator()
        {
            var validator = new LessThanOrEqualValidator(1);

            Assert.That(validator.ErrorMessageSource, Is.TypeOf(typeof(LocalizedStringSource)));
            Assert.That(validator.ErrorMessageSource.GetString(), Is.EqualTo(Messages.lessthanorequal_error));
        }
Esempio n. 2
0
        public override IEnumerable <ModelClientValidationRule> GetClientValidationRules()
        {
            if (!ShouldGenerateClientSideRules())
            {
                yield break;
            }

            string comparisionValuePlaceHolder = "ComparisonValue";

            // Convert validator
            LessThanOrEqualValidator lessThanOrEqualValidator = (LessThanOrEqualValidator)Validator;

            // Initialize client rule
            ModelClientValidationRule rule = new ModelClientValidationRule();

            rule.ValidationType = "lessthanorequalto";

            MessageFormatter formatter = new MessageFormatter()
                                         .AppendPropertyName(Metadata.DisplayName ?? Rule.PropertyName);

            if (lessThanOrEqualValidator.MemberToCompare != null)
            {
                // Append comparision member to message
                formatter.AppendArgument(
                    comparisionValuePlaceHolder,
                    lessThanOrEqualValidator.MemberToCompare.GetDisplayName());

                // Append '*.' to the name of field for prefix merging
                rule.ValidationParameters["field"] = string.Format("*.{0}",
                                                                   lessThanOrEqualValidator.MemberToCompare.Name);
            }
            else if (lessThanOrEqualValidator.ValueToCompare != null)
            {
                string valueToCompare = null;

                if (lessThanOrEqualValidator.ValueToCompare is DateTime)
                {
                    // If value is of type DateTime convert it to DateTime
                    // and format as yyyy-MM-dd to be able to parse it at client side
                    // easily using jQuery.
                    DateTime dateValueToCompare = (DateTime)lessThanOrEqualValidator.ValueToCompare;
                    valueToCompare = dateValueToCompare.ToString("yyyy-MM-dd");
                }
                else
                {
                    valueToCompare = lessThanOrEqualValidator.ValueToCompare.ToString();
                }

                // Append comparision value to message
                formatter.AppendArgument(comparisionValuePlaceHolder, valueToCompare);
                // Set value to compare
                rule.ValidationParameters["value"] = valueToCompare;
            }

            // Set error message of rule
            rule.ErrorMessage = formatter.BuildMessage(Validator.ErrorMessageSource.GetString(null));

            yield return(rule);
        }
        public void Should_create_lessthanorequaladapter_for_lessthanorequalvalidator()
        {
            // Given
            var validator = new LessThanOrEqualValidator(1);

            // When
            var result = factory.Create(this.rule, validator);

            // Then
            result.ShouldBeOfType <LessThanOrEqualAdapter>();
        }
Esempio n. 4
0
        private ValidationMessage LessThanOrEqualTo <TProp>(
            TProp value,
            TProp valueToCompare,
            IComparer comparer = null,
            ValidationMessageType validationMessageType = ValidationMessageType.Error)
            where TProp : IComparable <TProp>
        {
            var lessThanOrEqualToValidator = new LessThanOrEqualValidator <TestValidatableObject, TProp>(_ => valueToCompare, comparer, validationMessageType);
            var context           = new ValidationContext <TestValidatableObject, TProp>(null, nameof(TestValidatableObject.Number), null, value);
            var validationMessage = lessThanOrEqualToValidator.ValidateProperty(context).FirstOrDefault();

            return(validationMessage);
        }
        public void Format_IComparisonValidators()
        {
            var validator1 = new EqualValidator(5);
            var validator2 = new NotEqualValidator(10);
            var validator3 = new GreaterThanValidator(8);
            var validator4 = new GreaterThanOrEqualValidator(7);
            var validator5 = new LessThanValidator(2);
            var validator6 = new LessThanOrEqualValidator(1);
            var validator7 = new EqualValidator(o => o, typeof(Customer).GetProperty("UserName"));

            Assert.That(_formatter.Format(validator1, _typeNameFormatter), Is.EqualTo("EqualValidator { ValueToCompare = '5' }"));
            Assert.That(_formatter.Format(validator2, _typeNameFormatter), Is.EqualTo("NotEqualValidator { ValueToCompare = '10' }"));
            Assert.That(_formatter.Format(validator3, _typeNameFormatter), Is.EqualTo("GreaterThanValidator { ValueToCompare = '8' }"));
            Assert.That(_formatter.Format(validator4, _typeNameFormatter), Is.EqualTo("GreaterThanOrEqualValidator { ValueToCompare = '7' }"));
            Assert.That(_formatter.Format(validator5, _typeNameFormatter), Is.EqualTo("LessThanValidator { ValueToCompare = '2' }"));
            Assert.That(_formatter.Format(validator6, _typeNameFormatter), Is.EqualTo("LessThanOrEqualValidator { ValueToCompare = '1' }"));
            Assert.That(_formatter.Format(validator7, _typeNameFormatter), Is.EqualTo("EqualValidator { MemberToCompare = 'Customer.UserName\r\n' }"));
        }
        public void Should_create_lessthanorequaladapter_for_lessthanorequalvalidator()
        {
            // Given
            var validator = new LessThanOrEqualValidator(1);

            // When
            var result = factory.Create(this.rule, validator);

            // Then
            result.ShouldBeOfType<LessThanOrEqualAdapter>();
        }