示例#1
0
        /// <summary>
        /// Validate value against contract implementation
        /// </summary>
        /// <param name="value">Value</param>
        /// <param name="name">Name</param>
        /// <param name="isParameter">Flag indicating if value is from a parameter</param>
        /// <param name="attrs">Attribute</param>
        /// <returns>Exception</returns>
        protected override Exception ValidateContract(object value, string name, bool isParameter, ContractAspect attr)
        {
            if (value != null)
            {
                return(null);
            }

            return(new ArgumentNullException(name));
        }
示例#2
0
        /// <summary>
        /// Validate value against contract implementation
        /// </summary>
        /// <param name="value">Value</param>
        /// <param name="name">Name</param>
        /// <param name="isParameter">Flag indicating if value is from a parameter</param>
        /// <param name="attrs">Attribute</param>
        /// <returns>Exception</returns>
        protected override Exception ValidateContract(object value, string name, bool isParameter, ContractAspect attr)
        {
            if (value == null)
            {
                goto done;
            }

            var str      = value as string;
            var enumAttr = attr as EnumDataTypeAttribute;
            var enumType = enumAttr.EnumType;
            var values   = str != null?Enum.GetNames(enumType) :
                               Enum.GetValues(enumType).Cast <object>().Select(x => x.ToString()).ToArray();

            str = value.ToString();

            if (values.Contains(str))
            {
                return(null);
            }

done:
            return(new ArgumentException(name));
        }
示例#3
0
        /// <summary>
        /// Validate value against contract implementation
        /// </summary>
        /// <param name="value">Value</param>
        /// <param name="name">Name</param>
        /// <param name="isParameter">Flag indicating if value is from a parameter</param>
        /// <param name="attrs">Attribute</param>
        /// <returns>Exception</returns>
        protected override Exception ValidateContract(object value, string name, bool isParameter, ContractAspect attr)
        {
            var str = value as string;

            if (!(value == null || (str != null && string.IsNullOrEmpty(str))))
            {
                return(null);
            }

            return(new ArgumentNullException(name));
        }
示例#4
0
        /// <summary>
        /// Validate value against contract implementation
        /// </summary>
        /// <param name="value">Value</param>
        /// <param name="name">Name</param>
        /// <param name="isParameter">Flag indicating if value is from a parameter</param>
        /// <param name="attrs">Attribute</param>
        /// <returns>Exception</returns>
        protected override Exception ValidateContract(object value, string name, bool isParameter, ContractAspect attr)
        {
            if (value == null)
            {
                return(null);
            }

            var rangeAttr = attr as RangeAttribute;

            var num = (double)value;

            if (num >= rangeAttr.Min && num <= rangeAttr.Max)
            {
                return(null);
            }

            return(new ArgumentOutOfRangeException(name));
        }
        /// <summary>
        /// Validate value against contract implementation
        /// </summary>
        /// <param name="value">Value</param>
        /// <param name="name">Name</param>
        /// <param name="isParameter">Flag indicating if value is from a parameter</param>
        /// <param name="attrs">Attribute</param>
        /// <returns>Exception</returns>
        protected override Exception ValidateContract(object value, string name, bool isParameter, ContractAspect attr)
        {
            var regexAttr = attr as RegularExpressionAttribute;

            if (regexAttr.Regex.IsMatch(value as string))
            {
                return(null);
            }

            return(new ArgumentException(name));
        }
示例#6
0
        /// <summary>
        /// Validate value against contract implementation
        /// </summary>
        /// <param name="value">Value</param>
        /// <param name="name">Name</param>
        /// <param name="isParameter">Flag indicating if value is from a parameter</param>
        /// <param name="attrs">Attribute</param>
        /// <returns>Exception</returns>
        protected override Exception ValidateContract(object value, string name, bool isParameter, ContractAspect attr)
        {
            if (value == null || Uri.IsWellFormedUriString(value as string, UriKind.RelativeOrAbsolute))
            {
                return(null);
            }

            return(new ArgumentException(name));
        }
示例#7
0
        /// <summary>
        /// Validate value against contract implementation
        /// </summary>
        /// <param name="value">Value</param>
        /// <param name="name">Name</param>
        /// <param name="isParameter">Flag indicating if value is from a parameter</param>
        /// <param name="attrs">Attribute</param>
        /// <returns>Exception</returns>
        protected override Exception ValidateContract(object value, string name, bool isParameter, ContractAspect attr)
        {
            var str = value as string;

            if (str != null && string.IsNullOrWhiteSpace(str))
            {
                return(new NullReferenceException(name));
            }

            return(value != null ? null : new NullReferenceException(name));
        }
示例#8
0
        /// <summary>
        /// Validate value against contract implementation
        /// </summary>
        /// <param name="value">Value</param>
        /// <param name="name">Name</param>
        /// <param name="isParameter">Flag indicating if value is from a parameter</param>
        /// <param name="attrs">Attribute</param>
        /// <returns>Exception</returns>
        protected override Exception ValidateContract(object value, string name, bool isParameter, ContractAspect attr)
        {
            var strLengthAttr = attr as StringLengthAttribute;
            var str           = value as string;
            var strLength     = 0;

            if (str == null || (strLength = str.Length) >= strLengthAttr.MinLength && strLength <= strLengthAttr.MaxLength)
            {
                return(null);
            }

            return(new ArgumentException(name));
        }