LEFT | RIGHT |
1 /* | 1 /* |
2 Copyright 2010 Google Inc | 2 Copyright 2010 Google Inc |
3 | 3 |
4 Licensed under the Apache License, Version 2.0 (the "License"); | 4 Licensed under the Apache License, Version 2.0 (the "License"); |
5 you may not use this file except in compliance with the License. | 5 you may not use this file except in compliance with the License. |
6 You may obtain a copy of the License at | 6 You may obtain a copy of the License at |
7 | 7 |
8 http://www.apache.org/licenses/LICENSE-2.0 | 8 http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 |
10 Unless required by applicable law or agreed to in writing, software | 10 Unless required by applicable law or agreed to in writing, software |
11 distributed under the License is distributed on an "AS IS" BASIS, | 11 distributed under the License is distributed on an "AS IS" BASIS, |
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 See the License for the specific language governing permissions and | 13 See the License for the specific language governing permissions and |
14 limitations under the License. | 14 limitations under the License. |
15 */ | 15 */ |
16 | 16 |
17 using System; | 17 using System; |
18 using System.Linq; | 18 using System.Linq; |
19 using System.Text.RegularExpressions; | 19 using System.Text.RegularExpressions; |
20 | 20 |
21 using Google.Apis.Discovery; | 21 using Google.Apis.Discovery; |
22 using Google.Apis.Testing; | 22 using Google.Apis.Testing; |
23 using Google.Apis.Util; | 23 using Google.Apis.Util; |
24 | 24 |
25 namespace Google.Apis.Requests.Parameters | 25 namespace Google.Apis.Requests.Parameters |
26 { | 26 { |
27 /// <summary> | 27 /// <summary>Logic for validating a parameter.</summary> |
28 /// Logic for validating a parameter | |
29 /// </summary> | |
30 public static class ParameterValidator | 28 public static class ParameterValidator |
31 { | 29 { |
32 /// <summary> Validates a parameter value against the methods regex. </s
ummary> | 30 /// <summary>Validates a parameter value against the methods regex.</sum
mary> |
33 [VisibleForTestOnly] | 31 [VisibleForTestOnly] |
34 internal static bool ValidateRegex(IParameter param, string paramValue) | 32 internal static bool ValidateRegex(IParameter param, string paramValue) |
35 { | 33 { |
36 return string.IsNullOrEmpty(param.Pattern) || new Regex(param.Patter
n).IsMatch(paramValue); | 34 return string.IsNullOrEmpty(param.Pattern) || new Regex(param.Patter
n).IsMatch(paramValue); |
37 } | 35 } |
38 | 36 |
39 /// <summary> Validates if a parameter is valid. </summary> | 37 /// <summary>Validates if a parameter is valid.</summary> |
40 public static bool ValidateParameter(IParameter parameter, string value) | 38 public static bool ValidateParameter(IParameter parameter, string value) |
41 { | 39 { |
42 // If a required parameter is not present, fail | 40 // Fail if a required parameter is not present. |
43 if (String.IsNullOrEmpty(value)) | 41 if (String.IsNullOrEmpty(value)) |
44 { | 42 { |
45 return !parameter.IsRequired; | 43 return !parameter.IsRequired; |
46 } | 44 } |
47 | 45 |
48 // The parameter has value so validate the regex | 46 // The parameter has value so validate the regex. |
49 return ValidateRegex(parameter, value); | 47 return ValidateRegex(parameter, value); |
50 } | 48 } |
51 } | 49 } |
52 } | 50 } |
LEFT | RIGHT |