Example #1
0
        /// <summary>
        /// Verify whether value is greater than or equal special maxlength.
        /// minLength must be greater than or equal to zero.
        /// Allow value is null
        /// </summary>
        /// <param name="value">Value</param>
        /// <param name="minLength">min length,must be greater than or equal to zero</param>
        /// <returns>Return whether the verification has passed</returns>
        public static bool MinLength(this object value, int minLength)
        {
            if (minLength < 0)
            {
                throw new ArgumentException($"{nameof(minLength)} must be greater than or equal to zero");
            }
            var minLengthAttribute = new MinLengthAttribute(minLength);

            return(minLengthAttribute.IsValid(value));
        }
        protected override ValidationResult?IsValid(object?value, ValidationContext validationContext)
        {
            var required = new RequiredAttribute();
            var length   = new MinLengthAttribute(IdentityPasswordConstants.RequiredLength);

            if (!required.IsValid(value))
            {
                return(new ValidationResult(SharedValidationConstants.RequiredPassword));
            }

            return(!length.IsValid(value)
                ? new ValidationResult(SharedValidationConstants.InvalidPasswordLength)
                : ValidationResult.Success);
        }
        public void CheckMinLength()
        {
            var attr = new MinLengthAttribute(2);

            Assert.IsTrue(attr.IsValid(null), "#A1");
            Assert.IsFalse(attr.IsValid("1"), "#A2");

            Assert.IsTrue(attr.IsValid("12"), "#A3");
            Assert.IsTrue(attr.IsValid("123"), "#A4");

            Assert.IsFalse(attr.IsValid(BuildQuickList(1)), "#A5");
            Assert.IsTrue(attr.IsValid(BuildQuickList(2)), "#A6");
            Assert.IsTrue(attr.IsValid(BuildQuickList(3)), "#A7");
        }
Example #4
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (Optional && value == null)
            {
                return(ValidationResult.Success);
            }

            var result = IfMethodValid(value, validationContext);

            if (result.Item1 != IfValidResult.ContinueValidation)
            {
                return(result.Item2);
            }

            return(_attr.IsValid(value)
                ? ValidationResult.Success
                : new ValidationResult(String.Format(CultureInfo.CurrentCulture, ErrorMessageString,
                                                     validationContext.DisplayName ?? validationContext.MemberName), new[] { validationContext.MemberName }));
        }
        public override bool IsValid(object value)
        {
            if (value == null)
            {
                return(false);
            }

            if (value.GetType() == typeof(string))
            {
                var minValAttr = new MinLengthAttribute((int)MinimumValue);

                return(minValAttr.IsValid(value));
            }

            if (Convert.ToDouble(value) < MinimumValue)
            {
                return(false);
            }

            return(true);
        }
 public void Validate_ICollection_NetCore_Valid(MinLengthAttribute attribute, object value)
 {
     attribute.Validate(value, new ValidationContext(new object()));
     Assert.True(attribute.IsValid(value));
 }
 public void Validate_ICollection_NetFx_ThrowsInvalidCastException(MinLengthAttribute attribute, object value)
 {
     Assert.Throws <InvalidCastException>(() => attribute.Validate(value, new ValidationContext(new object())));
     Assert.Throws <InvalidCastException>(() => attribute.IsValid(value));
 }
 public void Validate_ICollection_NetCore_Invalid(MinLengthAttribute attribute, object value)
 {
     Assert.Throws <ValidationException>(() => attribute.Validate(value, new ValidationContext(new object())));
     Assert.False(attribute.IsValid(value));
 }