-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidateObjectAttribute.cs
27 lines (22 loc) · 1 KB
/
ValidateObjectAttribute.cs
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
using System.Collections.Generic;
using System.Linq;
namespace System.ComponentModel.DataAnnotations
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class ValidateObjectAttribute : ValidationAttribute
{
public ValidateObjectAttribute()
{
}
public override bool RequiresValidationContext => true;
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var validationResults = new List<ValidationResult>();
if (Validator.TryValidateObject(value, new ValidationContext(value), validationResults, validateAllProperties: true))
return ValidationResult.Success;
var message = string.Join(", ", validationResults.Select(vr => vr.ErrorMessage));
var mn = validationResults.SelectMany(vr => vr.MemberNames);
return new ValidationResult(message, mn);
}
}
}