forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelValidationNode.cs
More file actions
223 lines (190 loc) · 8.34 KB
/
Copy pathModelValidationNode.cs
File metadata and controls
223 lines (190 loc) · 8.34 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// 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.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web.Http.Controllers;
using System.Web.Http.Metadata;
using System.Web.Http.ModelBinding;
using System.Web.Http.Properties;
namespace System.Web.Http.Validation
{
public sealed class ModelValidationNode
{
private IEnumerable<ModelValidator> _validators;
private readonly List<ModelValidationNode> _childNodes;
public ModelValidationNode(ModelMetadata modelMetadata, string modelStateKey)
: this(modelMetadata, modelStateKey, null)
{
}
public ModelValidationNode(ModelMetadata modelMetadata, string modelStateKey, IEnumerable<ModelValidationNode> childNodes)
{
if (modelMetadata == null)
{
throw Error.ArgumentNull("modelMetadata");
}
if (modelStateKey == null)
{
throw Error.ArgumentNull("modelStateKey");
}
ModelMetadata = modelMetadata;
ModelStateKey = modelStateKey;
_childNodes = (childNodes != null) ? childNodes.ToList() : new List<ModelValidationNode>();
}
public event EventHandler<ModelValidatedEventArgs> Validated;
public event EventHandler<ModelValidatingEventArgs> Validating;
public ICollection<ModelValidationNode> ChildNodes
{
get { return _childNodes; }
}
public ModelMetadata ModelMetadata { get; private set; }
public string ModelStateKey { get; private set; }
public bool ValidateAllProperties { get; set; }
public bool SuppressValidation { get; set; }
public void CombineWith(ModelValidationNode otherNode)
{
if (otherNode != null && !otherNode.SuppressValidation)
{
Validated += otherNode.Validated;
Validating += otherNode.Validating;
List<ModelValidationNode> otherChildNodes = otherNode._childNodes;
for (int i = 0; i < otherChildNodes.Count; i++)
{
ModelValidationNode childNode = otherChildNodes[i];
_childNodes.Add(childNode);
}
}
}
private void OnValidated(ModelValidatedEventArgs e)
{
EventHandler<ModelValidatedEventArgs> handler = Validated;
if (handler != null)
{
handler(this, e);
}
}
private void OnValidating(ModelValidatingEventArgs e)
{
EventHandler<ModelValidatingEventArgs> handler = Validating;
if (handler != null)
{
handler(this, e);
}
}
private object TryConvertContainerToMetadataType(ModelValidationNode parentNode)
{
if (parentNode != null)
{
object containerInstance = parentNode.ModelMetadata.Model;
if (containerInstance != null)
{
Type expectedContainerType = ModelMetadata.ContainerType;
if (expectedContainerType != null)
{
if (expectedContainerType.IsInstanceOfType(containerInstance))
{
return containerInstance;
}
}
}
}
return null;
}
public void Validate(HttpActionContext actionContext)
{
Validate(actionContext, null /* parentNode */);
}
public void Validate(HttpActionContext actionContext, ModelValidationNode parentNode)
{
if (actionContext == null)
{
throw Error.ArgumentNull("actionContext");
}
if (SuppressValidation)
{
// no-op
return;
}
// pre-validation steps
ModelValidatingEventArgs validatingEventArgs = new ModelValidatingEventArgs(actionContext, parentNode);
OnValidating(validatingEventArgs);
if (validatingEventArgs.Cancel)
{
return;
}
ValidateChildren(actionContext);
ValidateThis(actionContext, parentNode);
// post-validation steps
ModelValidatedEventArgs validatedEventArgs = new ModelValidatedEventArgs(actionContext, parentNode);
OnValidated(validatedEventArgs);
}
private void ValidateChildren(HttpActionContext actionContext)
{
for (int i = 0; i < _childNodes.Count; i++)
{
ModelValidationNode child = _childNodes[i];
child.Validate(actionContext, this);
}
if (ValidateAllProperties)
{
ValidateProperties(actionContext);
}
}
private void ValidateProperties(HttpActionContext actionContext)
{
// Based off CompositeModelValidator.
ModelStateDictionary modelState = actionContext.ModelState;
// DevDiv Bugs #227802 - Caching problem in ModelMetadata requires us to manually regenerate
// the ModelMetadata.
object model = ModelMetadata.Model;
ModelMetadata updatedMetadata = actionContext.GetMetadataProvider().GetMetadataForType(() => model, ModelMetadata.ModelType);
foreach (ModelMetadata propertyMetadata in updatedMetadata.Properties)
{
// Only want to add errors to ModelState if something doesn't already exist for the property node,
// else we could end up with duplicate or irrelevant error messages.
string propertyKeyRoot = ModelBindingHelper.CreatePropertyModelName(ModelStateKey, propertyMetadata.PropertyName);
if (modelState.IsValidField(propertyKeyRoot))
{
foreach (ModelValidator propertyValidator in actionContext.GetValidators(propertyMetadata))
{
foreach (ModelValidationResult propertyResult in propertyValidator.Validate(propertyMetadata, model))
{
string thisErrorKey = ModelBindingHelper.CreatePropertyModelName(propertyKeyRoot, propertyResult.MemberName);
modelState.AddModelError(thisErrorKey, propertyResult.Message);
}
}
}
}
}
private void ValidateThis(HttpActionContext actionContext, ModelValidationNode parentNode)
{
ModelStateDictionary modelState = actionContext.ModelState;
if (!modelState.IsValidField(ModelStateKey))
{
return; // short-circuit
}
// If 'this' is null and there is no parent, we cannot validate, and
// the DataAnnotationsModelValidator will throw. So we intercept here
// to provide a catch-all value-required validation error
if (parentNode == null && ModelMetadata.Model == null)
{
string trueModelStateKey = ModelBindingHelper.CreatePropertyModelName(ModelStateKey, ModelMetadata.GetDisplayName());
modelState.AddModelError(trueModelStateKey, SRResources.Validation_ValueNotFound);
return;
}
_validators = actionContext.GetValidators(ModelMetadata);
object container = TryConvertContainerToMetadataType(parentNode);
// Optimize for the common case where the validators are in an array
ModelValidator[] validators = _validators.AsArray();
for (int i = 0; i < validators.Length; i++)
{
ModelValidator validator = validators[i];
foreach (ModelValidationResult validationResult in validator.Validate(ModelMetadata, container))
{
string trueModelStateKey = ModelBindingHelper.CreatePropertyModelName(ModelStateKey, validationResult.MemberName);
modelState.AddModelError(trueModelStateKey, validationResult.Message);
}
}
}
}
}