/
DataValidationResult.cs
76 lines (69 loc) · 2.7 KB
/
DataValidationResult.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
using DevZest.Data.Primitives;
using System;
namespace DevZest.Data
{
/// <summary>
/// Encapsulates DataRow and its validation errors.
/// </summary>
public struct DataValidationResult
{
/// <summary>
/// Initializes a new instance of <see cref="DataValidationResult"/>.
/// </summary>
/// <param name="dataRow">The DataRow.</param>
/// <param name="errors">The validation errors of the DataRow.</param>
public DataValidationResult(DataRow dataRow, IDataValidationErrors errors)
{
dataRow.VerifyNotNull(nameof(dataRow));
errors.VerifyNotNull(nameof(errors));
if (errors.Count == 0)
throw new ArgumentException(DiagnosticMessages.ValidationEntry_EmptyMessages, nameof(errors));
DataRow = dataRow;
Errors = errors.Seal();
}
/// <summary>
/// Gets the DataRow.
/// </summary>
public readonly DataRow DataRow;
/// <summary>
/// Gets the validation errors of the DataRow.
/// </summary>
public readonly IDataValidationErrors Errors;
/// <summary>
/// Gets a value indicating whether this struct is empty.
/// </summary>
public bool IsEmpty
{
get { return DataRow == null; }
}
/// <summary>
/// Serializes this data validation result into JSON.
/// </summary>
/// <param name="isPretty">Determines whether serialized JSON should be indented.</param>
/// <param name="customizer">The customizer for JSON serialization.</param>
/// <returns></returns>
public string ToJsonString(bool isPretty, IJsonCustomizer customizer = null)
{
return JsonWriter.Create(customizer).Write(this).ToString(isPretty);
}
/// <summary>
/// Deserializes JSON string into <see cref="DataValidationResult"/>
/// </summary>
/// <param name="dataSet">The DataSet which contains the data for validation.</param>
/// <param name="json">The input JSON string.</param>
/// <param name="customizer">The customizer for JSON deserialization.</param>
/// <returns>The deserialized object.</returns>
public static DataValidationResult ParseJson(DataSet dataSet, string json, IJsonCustomizer customizer = null)
{
var jsonReader = JsonReader.Create(json, customizer);
var result = jsonReader.ParseDataValidationResult(dataSet);
jsonReader.ExpectEof();
return result;
}
/// <inheritdoc />
public override string ToString()
{
return ToJsonString(true);
}
}
}