コード例 #1
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (length <= 0)
            {
                throw new InvalidOperationException("ListPropertyMaxLengthAttribute must have a length value that is greater than zero.");
            }

            if (value == null)
            {
                return(ValidationResult.Success);
            }

            var list = value as IList;

            if (list == null)
            {
                throw new InvalidOperationException("Attribute used on a type that doesn't implement IList.");
            }

            foreach (var item in list)
            {
                if (item == null)
                {
                    continue;
                }
                // Check that we have the right item with Type property defined by typeValue
                if (!string.IsNullOrEmpty(typeValue))
                {
                    var typeProperty = item.GetType().GetProperty("Type");
                    if (typeProperty == null || typeProperty.GetValue(item, null)?.ToString() != typeValue)
                    {
                        continue; // the Type is not right so jump to the next item
                    }
                }

                // Get the property
                var property = item.GetType().GetProperty(propertyName);
                if (property == null)
                {
                    return(new ValidationResult(string.Format(CoreMessages.OpenApi.UnknownProperty, propertyName)));
                }

                // Get the property value
                var propertyValue = property.GetValue(item, null);
                if (propertyValue == null)
                {
                    continue;
                }
                if (!maxLengthAttribute.IsValid(propertyValue.ToString()))
                {
                    var formattedName = $"'{propertyName}'";
                    formattedName += string.IsNullOrEmpty(typeValue) ? "" : $" for '{typeValue}'";
                    return(new ValidationResult($"Maximum length of property {formattedName} must be '{length}'."));
                }
            }
            return(ValidationResult.Success);
        }
コード例 #2
0
        /// <summary>
        /// Verify whether value is lessthan or equal special maxlength.
        /// maxLength must be greater than zero.
        /// Allow value is null
        /// </summary>
        /// <param name="value">Value</param>
        /// <param name="maxLength">max length,must be greater than zero</param>
        /// <returns>Return whether the verification has passed</returns>
        public static bool MaxLength(this object value, int maxLength)
        {
            if (maxLength < 1)
            {
                throw new ArgumentException($"{nameof(maxLength)} must be greater than zero");
            }
            var maxLengthAttribute = new MaxLengthAttribute(maxLength);

            return(maxLengthAttribute.IsValid(value));
        }
コード例 #3
0
        public void CheckMaxLength()
        {
            var attr = new MaxLengthAttribute(2);

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

            Assert.IsFalse(attr.IsValid("123"), "#A4");

            Assert.IsTrue(attr.IsValid(BuildQuickList(1)), "#A5");
            Assert.IsTrue(attr.IsValid(BuildQuickList(2)), "#A6");
            Assert.IsFalse(attr.IsValid(BuildQuickList(3)), "#A7");
        }
コード例 #4
0
        public override bool IsValid(object value)
        {
            if (value == null)
            {
                return(false);
            }

            if (value.GetType() == typeof(string))
            {
                var maxValAttr = new MaxLengthAttribute((int)MaxValue);

                return(maxValAttr.IsValid(value));
            }

            if (Convert.ToDouble(value) > MaxValue)
            {
                return(false);
            }

            return(true);
        }
コード例 #5
0
 public void Validate_ICollection_NetCore_Invalid(MaxLengthAttribute attribute, object value)
 {
     Assert.Throws <ValidationException>(() => attribute.Validate(value, new ValidationContext(new object())));
     Assert.False(attribute.IsValid(value));
 }
コード例 #6
0
 public void Validate_ICollection_NetCore_Valid(MaxLengthAttribute attribute, object value)
 {
     attribute.Validate(value, new ValidationContext(new object()));
     Assert.True(attribute.IsValid(value));
 }
コード例 #7
0
 public void Validate_ICollection_NetFx_ThrowsInvalidCastException(MaxLengthAttribute attribute, object value)
 {
     Assert.Throws <InvalidCastException>(() => attribute.Validate(value, new ValidationContext(new object())));
     Assert.Throws <InvalidCastException>(() => attribute.IsValid(value));
 }