-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComponentModel Data Annotation Validator Attributes.linq
145 lines (120 loc) · 4.5 KB
/
ComponentModel Data Annotation Validator Attributes.linq
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
<Query Kind="Program">
<Reference><RuntimeDirectory>\System.ComponentModel.DataAnnotations.dll</Reference>
<Reference><RuntimeDirectory>\System.Configuration.dll</Reference>
<Namespace>System.ComponentModel.DataAnnotations</Namespace>
</Query>
/*
Basic example of DataAnotations
IValidatableObject.Validate allows for validation outside of the contract annotations
*/
void Main()
{
var x = new StayDates()
{
MarketId = -1,
MarketSegmentId = -1,
SegmentUnitId = -1,
UnitId = -1,
SegmentDates = new List<UserQuery.SegmentDates>()
{
new SegmentDates() {
DateType = 1,
StartDate = DateTime.Parse("1/1/2016"),
EndDate = DateTime.Parse("1/11/2016")
},
new SegmentDates() {
DateType = 2,
StartDate = DateTime.Parse("11/11/2016"),
EndDate = DateTime.Parse("1/11/2016")
},
new SegmentDates() {
DateType = 1,
StartDate = DateTime.Parse("11/11/2016")
},
new SegmentDates() {
DateType = -1,
StartDate = DateTime.Parse("1/11/2016"),
EndDate = DateTime.Parse("1/15/2016")
}
}
};
var context = new ValidationContext(x);
var results = new List<ValidationResult>();
Validator.TryValidateObject(x, context, results, true); //Validates Fields
Validator.TryValidateObject(x, context, results, false); //Validates IValidateObject
results.Dump("Validation Errors");
}
public class StayDates : IValidatableObject
{
public StayDates()
{
SegmentDates = new List<SegmentDates>();
}
[Required(ErrorMessage = "The {0} is required.")]
[Range(1, Int32.MaxValue, ErrorMessage = "The {0} must be a valid Id.")]
public int MarketId { get; set; }
[Required(ErrorMessage = "The {0} is required.")]
[Range(1, Int32.MaxValue, ErrorMessage = "The {0} must be a valid Id.")]
public int MarketSegmentId { get; set; }
public int? SegmentUnitId { get; set; }
[Required(ErrorMessage = "The {0} is required.")]
[Range(1, Int32.MaxValue, ErrorMessage = "The {0} must be a valid Id.")]
public int UnitId { get; set; }
public List<SegmentDates> SegmentDates { get; set; }
public List<string> ErrorMessages { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var results = new List<ValidationResult>();
if (SegmentUnitId != null && SegmentUnitId.HasValue & SegmentUnitId.Value <= 0)
{
results.Add(new ValidationResult("When updating a Segment Unit, the SegmentUnitId must be a valid Id.", new string[] { "SegmentUnitId" }));
}
var duplicateDateRanges = SegmentDates.GroupBy(g => new { g.StartDate, g.EndDate }).Where(w => w.Count() > 1).Select(s => s.ToList());
if (duplicateDateRanges != null && duplicateDateRanges.Any())
{
results.Add(new ValidationResult(
"Could not create a new Channel Inventory Segment record. Duplicate date ranges are not allowed.", new string[] {"SegmentDates"}));
foreach (var duplicateList in duplicateDateRanges)
{
duplicateList.Select(dupe =>
string.Format("Duplicate: {0}:{1}-{2}",
dupe.DateType,
dupe.StartDate.ToString("d"),
dupe.EndDate.ToString("d"))).ToList().ForEach(s => results.Add(new ValidationResult(s)));
;
}
}
this.SegmentDates.ForEach(sd =>
{
var context = new ValidationContext(sd, validationContext.ServiceContainer, validationContext.Items);
Validator.TryValidateObject(sd, context, results, true); //Validates IValidatableObject
Validator.TryValidateObject(sd, context, results, false); //Validates Fields
}
);
return results;
}
}
public class SegmentDates : IValidatableObject
{
[Required(ErrorMessage = "The {0} is required.")]
[Range(1, Int32.MaxValue, ErrorMessage = "The {0} must be a valid Id.")]
public int DateType { get; set; }
[Required(ErrorMessage = "The {0} is required.")]
[Range(typeof(DateTime),"1/1/1901","12/31/9999", ErrorMessage = "The {0} is required.")]
public DateTime StartDate { get; set; }
[Required(ErrorMessage = "The {0} is required.")]
[Range(typeof(DateTime), "1/1/1901", "12/31/9999", ErrorMessage = "The {0} is required.")]
public DateTime EndDate { get; set; }
public List<string> ErrorMessages { get; set; }
public SegmentDates()
{
ErrorMessages = new List<string>();
}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (StartDate >= EndDate)
{
yield return new ValidationResult(string.Format("The StartDate ({0}) must be before the specified EndDate ({1}).", StartDate.ToString("d"), EndDate.ToString("d")), new List<string>() { "StartDate", "EndDate" });
}
}
}