forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataMemberModelValidatorProvider.cs
More file actions
49 lines (44 loc) · 2.05 KB
/
Copy pathDataMemberModelValidatorProvider.cs
File metadata and controls
49 lines (44 loc) · 2.05 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
// 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.Runtime.Serialization;
using System.Web.Http.Internal;
using System.Web.Http.Metadata;
using System.Web.Http.Validation.Validators;
namespace System.Web.Http.Validation.Providers
{
/// <summary>
/// This <see cref="ModelValidatorProvider"/> provides a required ModelValidator for members marked as [DataMember(IsRequired=true)].
/// </summary>
public class DataMemberModelValidatorProvider : AssociatedValidatorProvider
{
protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, IEnumerable<ModelValidatorProvider> validatorProviders, IEnumerable<Attribute> attributes)
{
// Types cannot be required; only properties can
if (metadata.ContainerType == null || String.IsNullOrEmpty(metadata.PropertyName))
{
return Enumerable.Empty<ModelValidator>();
}
if (IsRequiredDataMember(metadata.ContainerType, attributes))
{
return new[] { new RequiredMemberModelValidator(validatorProviders) };
}
return Enumerable.Empty<ModelValidator>();
}
internal static bool IsRequiredDataMember(Type containerType, IEnumerable<Attribute> attributes)
{
DataMemberAttribute dataMemberAttribute = attributes.OfType<DataMemberAttribute>().FirstOrDefault();
if (dataMemberAttribute != null)
{
// isDataContract == true iff the container type has at least one DataContractAttribute
bool isDataContract = TypeDescriptorHelper.Get(containerType).GetAttributes().OfType<DataContractAttribute>().Any();
if (isDataContract && dataMemberAttribute.IsRequired)
{
return true;
}
}
return false;
}
}
}