Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json\Utilities\ValidationUtils.cs

Symbol Coverage: 27.78% (15 of 54)

Branch Coverage: 22.22% (8 of 36)

Cyclomatic Complexity Avg: 2.06 Max:3

Code Lines: 54


L V Source
1
#region License
2
// Copyright (c) 2007 James Newton-King
3
//
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
11
// conditions:
12
//
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
15
//
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.
24
#endregion
25

  
26
using System;
27
using System.Collections;
28
using System.Collections.Generic;
29
using System.Text;
30
using System.Globalization;
31

  
32
namespace Newtonsoft.Json.Utilities
33
{
34
  internal static class ValidationUtils
35
  {
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*";
41

  
42
    public static void ArgumentNotNullOrEmpty(string value, string parameterName)
43
    {
44
 14
      if (value == null)
45
 0
        throw new ArgumentNullException(parameterName);
46

  
47
 14
      if (value.Length == 0)
48
 0
        throw new ArgumentException("'{0}' cannot be empty.".FormatWith(CultureInfo.InvariantCulture, parameterName), parameterName);
49
 14
    }
50

  
51
    public static void ArgumentNotNullOrEmptyOrWhitespace(string value, string parameterName)
52
    {
53
 0
      ArgumentNotNullOrEmpty(value, parameterName);
54

  
55
 0
      if (StringUtils.IsWhiteSpace(value))
56
 0
        throw new ArgumentException("'{0}' cannot only be whitespace.".FormatWith(CultureInfo.InvariantCulture, parameterName), parameterName);
57
 0
    }
58

  
59
    public static void ArgumentTypeIsEnum(Type enumType, string parameterName)
60
    {
61
 74
      ArgumentNotNull(enumType, "enumType");
62

  
63
 74
      if (!enumType.IsEnum)
64
 0
        throw new ArgumentException("Type {0} is not an Enum.".FormatWith(CultureInfo.InvariantCulture, enumType), parameterName);
65
 74
    }
66

  
67
    public static void ArgumentNotNullOrEmpty<T>(ICollection<T> collection, string parameterName)
68
    {
69
 16
      ArgumentNotNullOrEmpty<T>(collection, parameterName, "Collection '{0}' cannot be empty.".FormatWith(CultureInfo.InvariantCulture, parameterName));
70
 16
    }
71

  
72
    public static void ArgumentNotNullOrEmpty<T>(ICollection<T> collection, string parameterName, string message)
73
    {
74
 16
      if (collection == null)
75
 0
        throw new ArgumentNullException(parameterName);
76

  
77
 16
      if (collection.Count == 0)
78
 0
        throw new ArgumentException(message, parameterName);
79
 16
    }
80

  
81
    public static void ArgumentNotNullOrEmpty(ICollection collection, string parameterName)
82
    {
83
 0
      ArgumentNotNullOrEmpty(collection, parameterName, "Collection '{0}' cannot be empty.".FormatWith(CultureInfo.InvariantCulture, parameterName));
84
 0
    }
85

  
86
    public static void ArgumentNotNullOrEmpty(ICollection collection, string parameterName, string message)
87
    {
88
 0
      if (collection == null)
89
 0
        throw new ArgumentNullException(parameterName);
90

  
91
 0
      if (collection.Count == 0)
92
 0
        throw new ArgumentException(message, parameterName);
93
 0
    }
94

  
95
    public static void ArgumentNotNull(object value, string parameterName)
96
    {
97
 1305394
      if (value == null)
98
 0
        throw new ArgumentNullException(parameterName);
99
 1305394
    }
100

  
101
    public static void ArgumentNotNegative(int value, string parameterName)
102
    {
103
 0
      if (value <= 0)
104
 0
        throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, "Argument cannot be negative.");
105
 0
    }
106

  
107
    public static void ArgumentNotNegative(int value, string parameterName, string message)
108
    {
109
 0
      if (value <= 0)
110
 0
        throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, message);
111
 0
    }
112

  
113
    public static void ArgumentNotZero(int value, string parameterName)
114
    {
115
 0
      if (value == 0)
116
 0
        throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, "Argument cannot be zero.");
117
 0
    }
118

  
119
    public static void ArgumentNotZero(int value, string parameterName, string message)
120
    {
121
 0
      if (value == 0)
122
 0
        throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, message);
123
 0
    }
124

  
125
    public static void ArgumentIsPositive<T>(T value, string parameterName) where T : struct, IComparable<T>
126
    {
127
 0
      if (value.CompareTo(default(T)) != 1)
128
 0
        throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, "Positive number required.");
129
 0
    }
130

  
131
    public static void ArgumentIsPositive(int value, string parameterName, string message)
132
    {
133
 0
      if (value > 0)
134
 0
        throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, message);
135
 0
    }
136

  
137
    public static void ObjectNotDisposed(bool disposed, Type objectType)
138
    {
139
 0
      if (disposed)
140
 0
        throw new ObjectDisposedException(objectType.Name);
141
 0
    }
142

  
143
    public static void ArgumentConditionTrue(bool condition, string parameterName, string message)
144
    {
145
 12
      if (!condition)
146
 0
        throw new ArgumentException(message, parameterName);
147
 12
    }
148
  }
149
}