forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidatableObjectAdapter.cs
More file actions
61 lines (54 loc) · 2.3 KB
/
Copy pathValidatableObjectAdapter.cs
File metadata and controls
61 lines (54 loc) · 2.3 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
// 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.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Http.Metadata;
using System.Web.Http.Properties;
namespace System.Web.Http.Validation.Validators
{
public class ValidatableObjectAdapter : ModelValidator
{
public ValidatableObjectAdapter(IEnumerable<ModelValidatorProvider> validatorProviders)
: base(validatorProviders)
{
}
public override IEnumerable<ModelValidationResult> Validate(ModelMetadata metadata, object container)
{
// NOTE: Container is never used here, because IValidatableObject doesn't give you
// any way to get access to your container.
object model = metadata.Model;
if (model == null)
{
return Enumerable.Empty<ModelValidationResult>();
}
IValidatableObject validatable = model as IValidatableObject;
if (validatable == null)
{
throw Error.InvalidOperation(SRResources.ValidatableObjectAdapter_IncompatibleType, model.GetType());
}
ValidationContext validationContext = new ValidationContext(validatable, null, null);
return ConvertResults(validatable.Validate(validationContext));
}
private static IEnumerable<ModelValidationResult> ConvertResults(IEnumerable<ValidationResult> results)
{
foreach (ValidationResult result in results)
{
if (result != ValidationResult.Success)
{
if (result.MemberNames == null || !result.MemberNames.Any())
{
yield return new ModelValidationResult { Message = result.ErrorMessage };
}
else
{
foreach (string memberName in result.MemberNames)
{
yield return new ModelValidationResult { Message = result.ErrorMessage, MemberName = memberName };
}
}
}
}
}
}
}