/
DataValidationResults.cs
173 lines (149 loc) · 5.5 KB
/
DataValidationResults.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using DevZest.Data.Primitives;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections;
namespace DevZest.Data
{
/// <summary>
/// Contains static methods to handle <see cref="IDataValidationResults"/> objects.
/// </summary>
public static class DataValidationResults
{
private sealed class EmptyResults : IDataValidationResults
{
public static EmptyResults Singleton = new EmptyResults();
private EmptyResults()
{
}
public DataValidationResult this[int index]
{
get { throw new ArgumentOutOfRangeException(nameof(index)); }
}
public IDataValidationErrors this[DataRow key]
{
get { throw new ArgumentOutOfRangeException(nameof(key)); }
}
public int Count
{
get { return 0; }
}
public bool IsSealed
{
get { return true; }
}
public IDataValidationResults Seal()
{
return this;
}
public IDataValidationResults Add(DataValidationResult validationEntry)
{
IDataValidationResults result = new KeyedCollection();
return result.Add(validationEntry);
}
public bool Contains(DataRow dataRow)
{
dataRow.VerifyNotNull(nameof(dataRow));
return false;
}
public bool TryGetValue(DataRow key, out IDataValidationErrors value)
{
value = null;
return false;
}
IEnumerator IEnumerable.GetEnumerator()
{
yield break;
}
IEnumerator<DataValidationResult> IEnumerable<DataValidationResult>.GetEnumerator()
{
yield break;
}
public override string ToString()
{
return this.ToJsonString(true);
}
}
private sealed class KeyedCollection : KeyedCollection<DataRow, DataValidationResult>, IDataValidationResults
{
protected override DataRow GetKeyForItem(DataValidationResult item)
{
return item.DataRow;
}
public bool IsSealed { get; private set; }
IDataValidationErrors IDataValidationResults.this[DataRow dataRow]
{
get { return base[dataRow].Errors; }
}
public IDataValidationResults Seal()
{
IsSealed = true;
return this;
}
public override string ToString()
{
return this.ToJsonString(true);
}
IDataValidationResults IDataValidationResults.Add(DataValidationResult value)
{
if (value.IsEmpty)
throw new ArgumentException("", nameof(value));
if (!IsSealed)
{
base.Add(value);
return this;
}
var result = new KeyedCollection();
foreach (var entry in this)
result.Add(entry);
result.Add(value);
return result;
}
public bool TryGetValue(DataRow dataRow, out IDataValidationErrors value)
{
if (Contains(dataRow))
{
value = this[dataRow].Errors;
return true;
}
else
{
value = null;
return false;
}
}
}
/// <summary>
/// Gets an empty <see cref="IDataValidationResults"/> dictionary.
/// </summary>
public static IDataValidationResults Empty
{
get { return EmptyResults.Singleton; }
}
/// <summary>
/// Deserializes JSON string into <see cref="IDataValidationResults"/> object.
/// </summary>
/// <param name="dataSet">The DataSet which contains data for validation.</param>
/// <param name="json">The JSON string.</param>
/// <param name="customizer">The customizer for deserialization.</param>
/// <returns>The deserialized object.</returns>
public static IDataValidationResults ParseJson(DataSet dataSet, string json, IJsonCustomizer customizer = null)
{
var jsonReader = JsonReader.Create(json, customizer);
var result = jsonReader.ParseDataValidationResults(dataSet);
jsonReader.ExpectEof();
return result;
}
/// <summary>
/// Serializes <see cref="IDataValidationResults"/> dictionary into JSON string.
/// </summary>
/// <param name="validationResults">The <see cref="IDataValidationResults"/> dictionary.</param>
/// <param name="isPretty">Determines whether serialized JSON string should be indented.</param>
/// <param name="customizer">The customizer for JSON serialization.</param>
/// <returns>The serialized JSON string.</returns>
public static string ToJsonString(this IDataValidationResults validationResults, bool isPretty, IJsonCustomizer customizer = null)
{
return JsonWriter.Create(customizer).Write(validationResults).ToString(isPretty);
}
}
}