/// <summary> /// Validates the given value and returns its "cleaned" value as an /// appropriate .Net object. /// </summary> /// <param name="value">Value to clean</param> /// <returns>Cleaned value</returns> /// <exception cref="ValidationException">On an invalid validation</exception> public override object Clean(object value) { base.Clean(value); if (ConversionHelper.IsEmpty(value)) { return(null); } // Get date element DateTime?date = value as DateTime?; if (date != null && date.HasValue) { return(date.Value.TimeOfDay); } // Parse date string dateStr = value as string; if (dateStr != null) { try { return(DateTime.ParseExact(dateStr, InputFormats, CultureInfo.CurrentUICulture, DateTimeStyles.AssumeLocal | DateTimeStyles.AllowWhiteSpaces).TimeOfDay); } catch (FormatException) { } // Ignore here will be delt with later } throw ValidationException.Create(ErrorMessages[MessageInvalid]); }
/// <summary> /// Typed Clean method /// </summary> /// <param name="file">File objec to clean.</param> /// <returns>Cleaned file.</returns> protected override HttpPostedFileBase CleanFile(HttpPostedFileBase file) { file = base.CleanFile(file); Image img; // Try load the image try { img = Image.FromStream(file.InputStream); } catch (ArgumentException) { throw ValidationException.Create(this.ErrorMessages[MessageInvalidImage]); } // Check image width if (MaxWidth != null && img.Width > MaxWidth) { throw ValidationException.Create(this.ErrorMessages[MessageMaxWidth], MaxWidth, img.Width); } // Check image height if (MaxHeight != null && img.Height > MaxHeight) { throw ValidationException.Create(this.ErrorMessages[MessageMaxHeight], MaxHeight, img.Height); } return(file); }
/// <summary> /// Validates the given value and returns its "cleaned" value as an /// appropriate .Net object. /// </summary> /// <param name="value">Value to clean</param> /// <returns>Cleaned value</returns> /// <exception cref="ValidationException">On an invalid validation</exception> public override object Clean(object value) { if (ConversionHelper.IsEmpty(value)) { if (this.Required) { throw new ValidationException(this.ErrorMessages[MessageRequired]); } else { return(new Collection <object>()); } } // Ensure each value is valid. foreach (var val in ConversionHelper.ObjectList(value)) { if (!this.ValidValue(val)) { throw ValidationException.Create(this.ErrorMessages[MessageInvalidListValue], val); } } return(value); }
/// <summary> /// Validates the given value and returns its "cleaned" value as an /// appropriate .Net object. /// </summary> /// <param name="value">Value to clean</param> /// <returns>Cleaned value</returns> /// <exception cref="ValidationException">On an invalid validation</exception> public override object Clean(object value) { base.Clean(value); // String checks if (ConversionHelper.IsEmpty(value)) { return(null); } // Integer checks int result; if (!int.TryParse(value.ToString(), out result)) { throw ValidationException.Create(this.ErrorMessages[MessageInvalid]); } if (this.MaxValue != null && result > this.MaxValue) { throw ValidationException.Create(this.ErrorMessages[MessageMaxValue], this.MaxValue, result); } if (this.MinValue != null && result < this.MinValue) { throw ValidationException.Create(this.ErrorMessages[MessageMinValue], this.MinValue, result); } return(result); }
/// <summary> /// Typed Clean method /// </summary> /// <param name="file">File objec to clean.</param> /// <returns>Cleaned file.</returns> protected virtual HttpPostedFileBase CleanFile(HttpPostedFileBase file) { // Ensure a valid file name is set if (string.IsNullOrEmpty(file.FileName) && Required) { throw ValidationException.Create(this.ErrorMessages[MessageRequired]); } // Ensure filename isn't too long if (MaxLength != null && file.FileName.Length > MaxLength) { throw ValidationException.Create(this.ErrorMessages[MessageMaxLength], MaxLength, file.FileName.Length ); } // Check file isn't empty if (file.ContentLength == 0) { throw ValidationException.Create(this.ErrorMessages[MessageEmpty]); } // Check file isn't to big if (MaxSize != null && file.ContentLength > MaxSize) { throw ValidationException.Create(this.ErrorMessages[MessageMaxSize], Utils.HumaniseHelper.FileSize(MaxSize), Utils.HumaniseHelper.FileSize(file.ContentLength) ); } return(file); }
/// <summary> /// Clean method /// </summary> /// <param name="cleanedData"></param> /// <returns></returns> public static NameObjectDictionary CleanPasswordAgain(NameObjectDictionary cleanedData) { object password, passwordAgain; if (cleanedData.TryGetValue("Password", out password) && cleanedData.TryGetValue("PasswordAgain", out passwordAgain)) { if (!password.Equals(passwordAgain)) { throw ValidationException.Create("Password fields do not match"); } } return(cleanedData); }
/// <summary> /// Validates the given value and returns its "cleaned" value as an /// appropriate .Net object. /// </summary> /// <param name="value">Value to clean</param> /// <returns>Cleaned value</returns> /// <exception cref="ValidationException">On an invalid validation</exception> public override object Clean(object value) { var result = value as string; if (!string.IsNullOrEmpty(result)) { // If no URL scheme given, assume http:// if (!result.Contains("://")) { result = "http://" + result; } } base.Clean(result); if (string.IsNullOrEmpty(result)) { return(result); } if (this.VerifyExists) { // Setup request var sourceUri = new Uri(result, UriKind.Absolute); var request = (HttpWebRequest)HttpWebRequest.Create(sourceUri); request.Method = "HEAD"; request.UserAgent = this.UserAgent; try { // Try to fetch the response request.GetResponse().Close(); } catch (WebException wex) { var statusCode = (int)((HttpWebResponse)wex.Response).StatusCode; // A 4xx response is just an invalid link if (statusCode >= 400 && statusCode < 500) { throw ValidationException.Create(ErrorMessages[MessageInvalidLink]); } else { throw ValidationException.Create(ErrorMessages[MessageInvalid]); } } } return(result); }
/// <summary> /// Validates the given value and returns its "cleaned" value as an /// appropriate .Net object. /// </summary> /// <param name="value">Value to clean</param> /// <returns>Cleaned value</returns> /// <exception cref="ValidationException">On an invalid validation</exception> public override object Clean(object value) { base.Clean(value); if (ConversionHelper.IsEmpty(value)) { return(null); } if (!ValidValue(value)) { throw ValidationException.Create(this.ErrorMessages[MessageInvalidChoice], value); } return(value.ToString()); }
public void ValidationExceptionConstructorTest() { var test1 = ValidationException.Create("Test 1"); Assert.AreEqual("Test 1", test1.Message); Assert.IsNotNull(test1.Messages); Assert.IsTrue(test1.Messages.Count == 1); Assert.AreEqual("Test 1", test1.Messages[0]); var test2 = ValidationException.Create("Test {0}", 2); Assert.AreEqual("Test 2", test2.Message); Assert.IsNotNull(test2.Messages); Assert.IsTrue(test2.Messages.Count == 1); Assert.AreEqual("Test 2", test2.Messages[0]); }
/// <summary> /// Validates the given value and returns its "cleaned" value as an /// appropriate .Net object. /// </summary> /// <param name="value">Value to clean</param> /// <returns>Cleaned value</returns> /// <exception cref="ValidationException">On an invalid validation</exception> public override object Clean(object value) { base.Clean(value); if (ConversionHelper.IsEmpty(value)) { return(null); } // Get date element DateTime?date = value as DateTime?; if (date != null && date.HasValue) { return(date.Value); } // Input from a SplitDateTime widget is split into date and time by a tuple. Tuple tuple = value as Tuple; if (tuple != null) { if (tuple.Length != 2) { throw ValidationException.Create(ErrorMessages[MessageInvalid]); } value = string.Concat(tuple[0], " ", tuple[1]); } // Parse date string dateStr = value as string; if (dateStr != null) { try { return(DateTime.ParseExact(dateStr, InputFormats, CultureInfo.CurrentUICulture, DateTimeStyles.AssumeLocal | DateTimeStyles.AllowWhiteSpaces)); } catch (FormatException) { } // Ignore here will be delt with later } throw ValidationException.Create(ErrorMessages[MessageInvalid]); }
/// <summary> /// Validates the given value and returns its "cleaned" value as an /// appropriate .Net object. /// </summary> /// <param name="value">Value to clean</param> /// <returns>Cleaned value</returns> /// <exception cref="ValidationException">On an invalid validation</exception> public override object Clean(object value) { base.Clean(value); if (ConversionHelper.IsEmpty(value)) { return(null); } string result = value.ToString(); int valueLen = result.Length; if (this.MaxLength != null && valueLen > this.MaxLength) { throw ValidationException.Create(this.ErrorMessages[MessageMaxLength], this.MaxLength, valueLen); } if (this.MinLength != null && valueLen < this.MinLength) { throw ValidationException.Create(this.ErrorMessages[MessageMinLength], this.MinLength, valueLen); } return(result); }
public void MessagesTest() { var test1 = ValidationException.Create("Test 1"); Assert.AreEqual(1, test1.Messages.Count); }