-
Notifications
You must be signed in to change notification settings - Fork 105
/
ValidationError.cs
189 lines (164 loc) · 6.37 KB
/
ValidationError.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#region License
// Copyright (c) Newtonsoft. All Rights Reserved.
// License: https://raw.github.com/JamesNK/Newtonsoft.Json.Schema/master/LICENSE.md
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Schema.Infrastructure;
namespace Newtonsoft.Json.Schema
{
/// <summary>
/// Represents a JSON Schema validation error.
/// </summary>
[DebuggerDisplay("{GetExtendedMessage(),nq}")]
public class ValidationError : IJsonLineInfo
{
internal IList<ValidationError>? _childErrors;
private IFormattable? _formattable;
private string? _message;
private string? _extendedMessage;
/// <summary>
/// Gets the message describing the error that occurred.
/// </summary>
/// <value>The message describing the error that occurred.</value>
public string Message
{
get
{
EnsureMessages();
return _message;
}
}
/// <summary>
/// Gets the line number indicating where the error occurred.
/// </summary>
/// <value>The line number indicating where the error occurred.</value>
public int LineNumber { get; private set; }
/// <summary>
/// Gets the line position indicating where the error occurred.
/// </summary>
/// <value>The line position indicating where the error occurred.</value>
public int LinePosition { get; private set; }
/// <summary>
/// Gets the path to the JSON where the error occurred.
/// </summary>
/// <value>The path to the JSON where the error occurred.</value>
public string Path { get; private set; } = default!;
/// <summary>
/// Gets the JSON value when the error occurred.
/// </summary>
/// <value>The JSON value when the error occurred.</value>
public object? Value { get; private set; }
/// <summary>
/// Gets the <see cref="JSchema"/> that generated the error.
/// </summary>
/// <value>The <see cref="JSchema"/> that generated the error.</value>
[JsonIgnore]
public JSchema Schema { get; private set; } = default!;
/// <summary>
/// Gets the ID of the <see cref="JSchema"/> that generated the error, relative to the root schema.
/// </summary>
/// <value>The path of the <see cref="JSchema"/> that generated the error, relative to the root schema.</value>
public Uri? SchemaId { get; internal set; }
/// <summary>
/// Gets the base URI of the <see cref="JSchema"/> that generated the error.
/// </summary>
/// <value>The base URI of the <see cref="JSchema"/> that generated the error.</value>
public Uri? SchemaBaseUri { get; private set; }
/// <summary>
/// Gets the <see cref="ErrorType"/> that generated the error.
/// </summary>
/// <value>The <see cref="ErrorType"/> that generated the error.</value>
[JsonConverter(typeof(StringEnumConverter))]
public ErrorType ErrorType { get; private set; }
/// <summary>
/// Gets the <see cref="ValidationError"/>'s child errors.
/// </summary>
/// <value>The <see cref="ValidationError"/>'s child errors.</value>
public IList<ValidationError> ChildErrors
{
get
{
if (_childErrors == null)
{
_childErrors = new List<ValidationError>();
}
return _childErrors;
}
}
bool IJsonLineInfo.HasLineInfo()
{
return (LineNumber > 0 && LinePosition > 0);
}
[MemberNotNull(nameof(_message))]
[MemberNotNull(nameof(_extendedMessage))]
private void EnsureMessages()
{
if (_formattable != null)
{
BuildMessages();
_formattable = null;
}
ValidationUtils.Assert(_message != null);
ValidationUtils.Assert(_extendedMessage != null);
}
[MemberNotNull(nameof(_message))]
[MemberNotNull(nameof(_extendedMessage))]
private void BuildMessages()
{
ValidationUtils.Assert(_formattable != null);
if (_formattable is FormattableString formattableString)
{
string format = formattableString.Format;
object[] args = formattableString.GetArguments();
StringBuilder sb = new StringBuilder(format.Length + (args.Length * 8));
if (args.Length > 0)
{
sb.AppendFormat(CultureInfo.InvariantCulture, format, args);
}
else
{
sb.Append(format);
}
_message = sb.ToString();
_extendedMessage = JSchemaException.FormatMessage(this, Path, sb);
}
else
{
string message = _formattable.ToString(null, CultureInfo.InvariantCulture);
_message = message;
_extendedMessage = JSchemaException.FormatMessage(this, Path, message);
}
_formattable = null;
}
internal string GetExtendedMessage()
{
EnsureMessages();
return _extendedMessage;
}
internal static ValidationError CreateValidationError(IFormattable message, ErrorType errorType, JSchema schema, Uri? schemaId, object? value, IList<ValidationError>? childErrors, IJsonLineInfo? lineInfo, string path)
{
ValidationError error = new ValidationError();
error._formattable = message;
error._childErrors = childErrors;
error.ErrorType = errorType;
error.Path = path;
if (lineInfo != null)
{
error.LineNumber = lineInfo.LineNumber;
error.LinePosition = lineInfo.LinePosition;
}
error.Schema = schema;
error.SchemaId = schemaId;
error.SchemaBaseUri = schema.BaseUri;
error.Value = value;
return error;
}
}
}