forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataAnnotationsModelValidator.cs
More file actions
120 lines (103 loc) · 5.03 KB
/
Copy pathDataAnnotationsModelValidator.cs
File metadata and controls
120 lines (103 loc) · 5.03 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
// 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.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using System.Linq;
using System.Web.Http.Metadata;
namespace System.Web.Http.Validation.Validators
{
public class DataAnnotationsModelValidator : ModelValidator
{
internal static readonly string UseLegacyValidationMemberNameKey = "webapi:UseLegacyValidationMemberName";
private static bool _useLegacyValidationMemberName =
GetUseLegacyValidationMemberName(ConfigurationManager.AppSettings);
public DataAnnotationsModelValidator(
IEnumerable<ModelValidatorProvider> validatorProviders,
ValidationAttribute attribute)
: base(validatorProviders)
{
if (attribute == null)
{
throw Error.ArgumentNull("attribute");
}
Attribute = attribute;
}
// Internal for testing
internal static bool UseLegacyValidationMemberName
{
get { return _useLegacyValidationMemberName; }
set { _useLegacyValidationMemberName = value; }
}
protected internal ValidationAttribute Attribute { get; private set; }
public override bool IsRequired
{
get { return Attribute is RequiredAttribute; }
}
public override IEnumerable<ModelValidationResult> Validate(ModelMetadata metadata, object container)
{
string memberName;
if (_useLegacyValidationMemberName)
{
// Using member name from a Display or DisplayFormat attribute is generally incorrect. This
// (configuration-controlled) override is provided only for corner cases where strict
// back-compatibility is required.
memberName = metadata.GetDisplayName();
}
else
{
// MemberName expression matches GetDisplayName() except it ignores Display and DisplayName
// attributes. ModelType fallback isn't great and can separate errors and attempted values in the
// ModelState. But, expression matches MVC, avoids a null MemberName, and is mostly back-compatible.
memberName = metadata.PropertyName ?? metadata.ModelType.Name;
}
// Per the WCF RIA Services team, instance can never be null (if you have
// no parent, you pass yourself for the "instance" parameter).
ValidationContext context = new ValidationContext(instance: container ?? metadata.Model)
{
DisplayName = metadata.GetDisplayName(),
MemberName = memberName,
};
ValidationResult result = Attribute.GetValidationResult(metadata.Model, context);
if (result != ValidationResult.Success)
{
// ModelValidationResult.MemberName is used by invoking validators (such as ModelValidationNode) to
// construct the ModelKey for ModelStateDictionary. When validating at type level we want to append the
// returned MemberNames if specified (e.g. person.Address.FirstName). For property validation, the
// ModelKey can be constructed using the ModelMetadata and we should ignore MemberName (we don't want
// (person.Name.Name). However the invoking validator does not have a way to distinguish between these two
// cases. Consequently we'll only set MemberName if this validation returns a MemberName that is different
// from the property being validated.
string errorMemberName = result.MemberNames.FirstOrDefault();
if (String.Equals(errorMemberName, memberName, StringComparison.Ordinal))
{
errorMemberName = null;
}
var validationResult = new ModelValidationResult
{
Message = result.ErrorMessage,
MemberName = errorMemberName
};
return new ModelValidationResult[] { validationResult };
}
return Enumerable.Empty<ModelValidationResult>();
}
// Internal for testing
internal static bool GetUseLegacyValidationMemberName(NameValueCollection appSettings)
{
var useLegacyMemberNameArray = appSettings.GetValues(UseLegacyValidationMemberNameKey);
if (useLegacyMemberNameArray != null &&
useLegacyMemberNameArray.Length > 0)
{
bool useLegacyMemberName;
if (bool.TryParse(useLegacyMemberNameArray[0], out useLegacyMemberName) &&
useLegacyMemberName)
{
return true;
}
}
return false;
}
}
}