public static GuardThis <int> AgainstNegative(this GuardThis <int> guard) { if (guard.Obj < 0) { throw new ArgumentException(guard.CustomErrorMessage != null? guard.CustomErrorMessage: "Value should not be less than zero"); } return(guard); }
public static GuardThis <int> AgainstNotBeingExactly(this GuardThis <int> guard, int value) { if (guard.Obj != value) { throw new ArgumentException(guard.CustomErrorMessage != null ? guard.CustomErrorMessage : string.Format("Value should be exactly {0}", value)); } return(guard); }
public static GuardThis <string> AgainstNullOrEmpty(this GuardThis <string> guard) { if (string.IsNullOrEmpty(guard.Obj)) { throw new ArgumentException(guard.CustomErrorMessage != null ? guard.CustomErrorMessage : "String was found to be either empty or null when it should have a value"); } return(guard); }
public static GuardThis <string> AgainstSpaces(this GuardThis <string> guard) { if (guard.Obj.Trim().Contains(" ")) { throw new ArgumentException(guard.CustomErrorMessage != null ? guard.CustomErrorMessage : "String was found to be contain white space, but white space is not allowed"); } return(guard); }
public static GuardThis <DateTime> AgainstCrmMinimum(this GuardThis <DateTime> guard) { if (guard.Obj.CompareTo(CrmConstants.CrmMinimum) < 0) { throw new ArgumentException(guard.CustomErrorMessage != null ? guard.CustomErrorMessage : "DateTime was expected to be on or after 01/01/1970"); } return(guard); }
public static GuardThis <Guid> AgainstNotEqual(this GuardThis <Guid> guard, Guid otherGuid) { if (guard.Obj.CompareTo(otherGuid) != 0) { throw new ArgumentException( guard.CustomErrorMessage != null ? guard.CustomErrorMessage : "Guids were expected to match, but do not"); } return(guard); }
public static GuardThis <string> AgainstNonGuidFormat(this GuardThis <string> guard) { Guid guid = Guid.Empty; if (!Guid.TryParse(guard.Obj, out guid)) { throw new ArgumentException(guard.CustomErrorMessage != null ? guard.CustomErrorMessage : "String was expected to be in GUID format (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)"); } return(guard); }
public static GuardThis <string> AgainstInvalidLength(this GuardThis <string> guard, int minLength, int maxLength) { if (guard.Obj.Length > maxLength) { throw new ArgumentException(guard.CustomErrorMessage != null ? guard.CustomErrorMessage : string.Format("String was expected to be shorter than {0} characters", maxLength)); } else if (guard.Obj.Length < minLength) { throw new ArgumentException(guard.CustomErrorMessage != null ? guard.CustomErrorMessage : string.Format("String was expected to be longer than {0} characters", minLength)); } return(guard); }
public static GuardThis <int> AgainstInvalidRange(this GuardThis <int> guard, int minValue, int maxValue) { if (guard.Obj > maxValue) { throw new ArgumentException(guard.CustomErrorMessage != null ? guard.CustomErrorMessage : string.Format("Integer value was expected to be smaller than {0}", maxValue)); } else if (guard.Obj < minValue) { throw new ArgumentException(guard.CustomErrorMessage != null ? guard.CustomErrorMessage : string.Format("Integer value was expected to be larger than {0}", minValue)); } return(guard); }