/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }