2 // Copyright (c) 2007 James Newton-King
4 // Permission is hereby granted, free of charge, to any person
5 // obtaining a copy of this software and associated documentation
6 // files (the "Software"), to deal in the Software without
7 // restriction, including without limitation the rights to use,
8 // copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 // OTHER DEALINGS IN THE SOFTWARE.
27 using System.Collections;
28 using System.Collections.Generic;
30 using System.Globalization;
32 namespace Newtonsoft.Json.Utilities
34 internal static class ValidationUtils
36 public const string EmailAddressRegex = @"^([a-zA-Z0-9_'+*$%\^&!\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9:]{2,4})+$";
37 public const string CurrencyRegex = @"(^\$?(?!0,?\d)\d{1,3}(,?\d{3})*(\.\d\d)?)$";
38 public const string DateRegex =
39 @"^(((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00|[048])))$";
40 public const string NumericRegex = @"\d*";
42 public static void ArgumentNotNullOrEmpty(string value, string parameterName)
45 throw new ArgumentNullException(parameterName);
47 if (value.Length == 0)
48 throw new ArgumentException("'{0}' cannot be empty.".FormatWith(CultureInfo.InvariantCulture, parameterName), parameterName);
51 public static void ArgumentNotNullOrEmptyOrWhitespace(string value, string parameterName)
53 ArgumentNotNullOrEmpty(value, parameterName);
55 if (StringUtils.IsWhiteSpace(value))
56 throw new ArgumentException("'{0}' cannot only be whitespace.".FormatWith(CultureInfo.InvariantCulture, parameterName), parameterName);
59 public static void ArgumentTypeIsEnum(Type enumType, string parameterName)
61 ArgumentNotNull(enumType, "enumType");
64 throw new ArgumentException("Type {0} is not an Enum.".FormatWith(CultureInfo.InvariantCulture, enumType), parameterName);
67 public static void ArgumentNotNullOrEmpty<T>(ICollection<T> collection, string parameterName)
69 ArgumentNotNullOrEmpty<T>(collection, parameterName, "Collection '{0}' cannot be empty.".FormatWith(CultureInfo.InvariantCulture, parameterName));
72 public static void ArgumentNotNullOrEmpty<T>(ICollection<T> collection, string parameterName, string message)
74 if (collection == null)
75 throw new ArgumentNullException(parameterName);
77 if (collection.Count == 0)
78 throw new ArgumentException(message, parameterName);
81 public static void ArgumentNotNullOrEmpty(ICollection collection, string parameterName)
83 ArgumentNotNullOrEmpty(collection, parameterName, "Collection '{0}' cannot be empty.".FormatWith(CultureInfo.InvariantCulture, parameterName));
86 public static void ArgumentNotNullOrEmpty(ICollection collection, string parameterName, string message)
88 if (collection == null)
89 throw new ArgumentNullException(parameterName);
91 if (collection.Count == 0)
92 throw new ArgumentException(message, parameterName);
95 public static void ArgumentNotNull(object value, string parameterName)
98 throw new ArgumentNullException(parameterName);
101 public static void ArgumentNotNegative(int value, string parameterName)
104 throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, "Argument cannot be negative.");
107 public static void ArgumentNotNegative(int value, string parameterName, string message)
110 throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, message);
113 public static void ArgumentNotZero(int value, string parameterName)
116 throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, "Argument cannot be zero.");
119 public static void ArgumentNotZero(int value, string parameterName, string message)
122 throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, message);
125 public static void ArgumentIsPositive<T>(T value, string parameterName) where T : struct, IComparable<T>
127 if (value.CompareTo(default(T)) != 1)
128 throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, "Positive number required.");
131 public static void ArgumentIsPositive(int value, string parameterName, string message)
134 throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, message);
137 public static void ObjectNotDisposed(bool disposed, Type objectType)
140 throw new ObjectDisposedException(objectType.Name);
143 public static void ArgumentConditionTrue(bool condition, string parameterName, string message)
146 throw new ArgumentException(message, parameterName);