forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidator.cs
More file actions
128 lines (112 loc) · 7.46 KB
/
Copy pathValidator.cs
File metadata and controls
128 lines (112 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Web.Mvc;
using System.Web.WebPages.Resources;
using Microsoft.Internal.Web.Utils;
namespace System.Web.WebPages
{
public abstract class Validator
{
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "We are ok using default parameters for helpers")]
public static IValidator Required(string errorMessage = null)
{
errorMessage = DefaultIfEmpty(errorMessage, WebPageResources.ValidationDefault_Required);
var clientAttributes = new ModelClientValidationRequiredRule(errorMessage);
// We don't care if the value is unsafe when verifying that it is required.
return new ValidationAttributeAdapter(new RequiredAttribute(), errorMessage, clientAttributes, useUnvalidatedValues: true);
}
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "We are ok using default parameters for helpers")]
public static IValidator Range(int minValue, int maxValue, string errorMessage = null)
{
errorMessage = String.Format(CultureInfo.CurrentCulture, DefaultIfEmpty(errorMessage, WebPageResources.ValidationDefault_IntegerRange), minValue, maxValue);
var clientAttributes = new ModelClientValidationRangeRule(errorMessage, minValue, maxValue);
return new ValidationAttributeAdapter(new RangeAttribute(minValue, maxValue), errorMessage, clientAttributes);
}
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "We are ok using default parameters for helpers")]
public static IValidator Range(double minValue, double maxValue, string errorMessage = null)
{
errorMessage = String.Format(CultureInfo.CurrentCulture, DefaultIfEmpty(errorMessage, WebPageResources.ValidationDefault_FloatRange), minValue, maxValue);
var clientAttributes = new ModelClientValidationRangeRule(errorMessage, minValue, maxValue);
return new ValidationAttributeAdapter(new RangeAttribute(minValue, maxValue), errorMessage, clientAttributes);
}
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "We are ok using default parameters for helpers")]
public static IValidator StringLength(int maxLength, int minLength = 0, string errorMessage = null)
{
if (minLength == 0)
{
errorMessage = String.Format(CultureInfo.CurrentCulture, DefaultIfEmpty(errorMessage, WebPageResources.ValidationDefault_StringLength), maxLength);
}
else
{
errorMessage = DefaultIfEmpty(errorMessage, WebPageResources.ValidationDefault_StringLengthRange);
errorMessage = String.Format(CultureInfo.CurrentCulture, errorMessage, minLength, maxLength);
}
var clientAttributes = new ModelClientValidationStringLengthRule(errorMessage, minLength, maxLength);
// We don't care if the value is unsafe when checking the length of the request field passed to us.
return new ValidationAttributeAdapter(new StringLengthAttribute(maxLength) { MinimumLength = minLength }, errorMessage, clientAttributes,
useUnvalidatedValues: true);
}
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "We are ok using default parameters for helpers")]
public static IValidator Regex(string pattern, string errorMessage = null)
{
if (String.IsNullOrEmpty(pattern))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "pattern");
}
errorMessage = DefaultIfEmpty(errorMessage, WebPageResources.ValidationDefault_Regex);
var clientAttributes = new ModelClientValidationRegexRule(errorMessage, pattern);
return new ValidationAttributeAdapter(new RegularExpressionAttribute(pattern), errorMessage, clientAttributes);
}
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "We are ok using default parameters for helpers")]
public static IValidator EqualsTo(string otherFieldName, string errorMessage = null)
{
if (String.IsNullOrEmpty(otherFieldName))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "otherFieldName");
}
errorMessage = DefaultIfEmpty(errorMessage, WebPageResources.ValidationDefault_EqualsTo);
return new CompareValidator(otherFieldName, errorMessage);
}
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "We are ok using default parameters for helpers")]
public static IValidator DateTime(string errorMessage = null)
{
errorMessage = DefaultIfEmpty(errorMessage, WebPageResources.ValidationDefault_DataType);
return new DataTypeValidator(DataTypeValidator.SupportedValidationDataType.DateTime, errorMessage);
}
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "We are ok using default parameters for helpers")]
public static IValidator Decimal(string errorMessage = null)
{
errorMessage = DefaultIfEmpty(errorMessage, WebPageResources.ValidationDefault_DataType);
return new DataTypeValidator(DataTypeValidator.SupportedValidationDataType.Decimal, errorMessage);
}
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "We are ok using default parameters for helpers")]
public static IValidator Integer(string errorMessage = null)
{
errorMessage = DefaultIfEmpty(errorMessage, WebPageResources.ValidationDefault_DataType);
return new DataTypeValidator(DataTypeValidator.SupportedValidationDataType.Integer, errorMessage);
}
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "We are ok using default parameters for helpers")]
public static IValidator Url(string errorMessage = null)
{
errorMessage = DefaultIfEmpty(errorMessage, WebPageResources.ValidationDefault_DataType);
return new DataTypeValidator(DataTypeValidator.SupportedValidationDataType.Url, errorMessage);
}
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "We are ok using default parameters for helpers")]
public static IValidator Float(string errorMessage = null)
{
errorMessage = DefaultIfEmpty(errorMessage, WebPageResources.ValidationDefault_DataType);
return new DataTypeValidator(DataTypeValidator.SupportedValidationDataType.Float, errorMessage);
}
private static string DefaultIfEmpty(string errorMessage, string defaultErrorMessage)
{
if (String.IsNullOrEmpty(errorMessage))
{
return defaultErrorMessage;
}
return errorMessage;
}
}
}