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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
#nullable disable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using StardewModdingAPI.Web.Framework;
using StardewModdingAPI.Web.Framework.Storage;
using StardewModdingAPI.Web.ViewModels.JsonValidator;
namespace StardewModdingAPI.Web.Controllers
{
/// <summary>Provides a web UI for validating JSON schemas.</summary>
internal class JsonValidatorController : Controller
{
/*********
** Fields
*********/
/// <summary>Provides access to raw data storage.</summary>
private readonly IStorageProvider Storage;
/// <summary>The supported JSON schemas (names indexed by ID).</summary>
private readonly IDictionary<string, string> SchemaFormats = new Dictionary<string, string>
{
["none"] = "None",
["manifest"] = "SMAPI: manifest",
["i18n"] = "SMAPI: translations (i18n)",
["content-patcher"] = "Content Patcher"
};
/// <summary>The schema ID to use if none was specified.</summary>
private string DefaultSchemaID = "none";
/// <summary>A token in an error message which indicates that the child errors should be displayed instead.</summary>
private readonly string TransparentToken = "$transparent";
/*********
** Public methods
*********/
/***
** Constructor
***/
/// <summary>Construct an instance.</summary>
/// <param name="storage">Provides access to raw data storage.</param>
public JsonValidatorController(IStorageProvider storage)
{
this.Storage = storage;
}
/***
** Web UI
***/
/// <summary>Render the schema validator UI.</summary>
/// <param name="schemaName">The schema name with which to validate the JSON, or 'edit' to return to the edit screen.</param>
/// <param name="id">The stored file ID.</param>
/// <param name="operation">The operation to perform for the selected log ID. This can be 'edit', 'renew', or any other value to view.</param>
[HttpGet]
[Route("json")]
[Route("json/{schemaName}")]
[Route("json/{schemaName}/{id}")]
[Route("json/{schemaName}/{id}/{operation}")]
public async Task<ViewResult> Index(string schemaName = null, string id = null, string operation = null)
{
// parse arguments
schemaName = this.NormalizeSchemaName(schemaName);
operation = operation?.Trim().ToLower();
bool hasId = !string.IsNullOrWhiteSpace(id);
bool isEditView = !hasId || operation == "edit";
bool renew = operation == "renew";
// build result model
var result = this.GetModel(id, schemaName, isEditView);
if (!hasId)
return this.View("Index", result);
// fetch raw JSON
StoredFileInfo file = await this.Storage.GetAsync(id, renew);
if (string.IsNullOrWhiteSpace(file.Content))
return this.View("Index", result.SetUploadError("The JSON file seems to be empty."));
result.SetContent(file.Content, expiry: file.Expiry, uploadWarning: file.Warning);
// skip parsing if we're going to the edit screen
if (isEditView)
return this.View("Index", result);
// parse JSON
JToken parsed;
{
// load raw JSON
var settings = new JsonLoadSettings
{
DuplicatePropertyNameHandling = DuplicatePropertyNameHandling.Error,
CommentHandling = CommentHandling.Load
};
try
{
parsed = JToken.Parse(file.Content, settings);
}
catch (JsonReaderException ex)
{
return this.View("Index", result.AddErrors(new JsonValidatorErrorModel(ex.LineNumber, ex.Path, ex.Message, ErrorType.None)));
}
// format JSON
string formatted = parsed.ToString(Formatting.Indented);
result.SetContent(formatted, expiry: file.Expiry, uploadWarning: file.Warning);
parsed = JToken.Parse(formatted); // update line number references
}
// skip if no schema selected
if (schemaName == "none")
return this.View("Index", result);
// load schema
JSchema schema;
{
FileInfo schemaFile = this.FindSchemaFile(schemaName);
if (schemaFile == null)
return this.View("Index", result.SetParseError($"Invalid schema '{schemaName}'."));
schema = JSchema.Parse(System.IO.File.ReadAllText(schemaFile.FullName));
}
// get format doc URL
result.FormatUrl = this.GetExtensionField<string>(schema, "@documentationUrl");
// validate JSON
parsed.IsValid(schema, out IList<ValidationError> rawErrors);
var errors = rawErrors
.SelectMany(this.GetErrorModels)
.ToArray();
return this.View("Index", result.AddErrors(errors));
}
/***
** JSON
***/
/// <summary>Save raw JSON data.</summary>
[HttpPost, AllowLargePosts]
[Route("json")]
public async Task<ActionResult> PostAsync(JsonValidatorRequestModel request)
{
if (request == null)
return this.View("Index", this.GetModel(null, null, isEditView: true).SetUploadError("The request seems to be invalid."));
// normalize schema name
string schemaName = this.NormalizeSchemaName(request.SchemaName);
// get raw text
string input = request.Content;
if (string.IsNullOrWhiteSpace(input))
return this.View("Index", this.GetModel(null, schemaName, isEditView: true).SetUploadError("The JSON file seems to be empty."));
// upload file
UploadResult result = await this.Storage.SaveAsync(input);
if (!result.Succeeded)
return this.View("Index", this.GetModel(result.ID, schemaName, isEditView: true).SetContent(input, null).SetUploadError(result.UploadError));
// redirect to view
return this.Redirect(this.Url.PlainAction("Index", "JsonValidator", new { schemaName, id = result.ID }));
}
/*********
** Private methods
*********/
/// <summary>Build a JSON validator model.</summary>
/// <param name="pasteID">The stored file ID.</param>
/// <param name="schemaName">The schema name with which the JSON was validated.</param>
/// <param name="isEditView">Whether to show the edit view.</param>
private JsonValidatorModel GetModel(string pasteID, string schemaName, bool isEditView)
{
return new JsonValidatorModel(pasteID, schemaName, this.SchemaFormats, isEditView);
}
/// <summary>Get a normalized schema name, or the <see cref="DefaultSchemaID"/> if blank.</summary>
/// <param name="schemaName">The raw schema name to normalize.</param>
private string NormalizeSchemaName(string schemaName)
{
schemaName = schemaName?.Trim().ToLower();
return !string.IsNullOrWhiteSpace(schemaName)
? schemaName
: this.DefaultSchemaID;
}
/// <summary>Get the schema file given its unique ID.</summary>
/// <param name="id">The schema ID.</param>
private FileInfo FindSchemaFile(string id)
{
// normalize ID
id = id?.Trim().ToLower();
if (string.IsNullOrWhiteSpace(id))
return null;
// get matching file
DirectoryInfo schemaDir = new(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "schemas"));
foreach (FileInfo file in schemaDir.EnumerateFiles("*.json"))
{
if (file.Name.Equals($"{id}.json"))
return file;
}
return null;
}
/// <summary>Get view models representing a schema validation error and any child errors.</summary>
/// <param name="error">The error to represent.</param>
private IEnumerable<JsonValidatorErrorModel> GetErrorModels(ValidationError error)
{
// skip through transparent errors
if (this.IsTransparentError(error))
{
foreach (var model in error.ChildErrors.SelectMany(this.GetErrorModels))
yield return model;
yield break;
}
// get message
string message = this.GetOverrideError(error);
if (message == null || message == this.TransparentToken)
message = this.FlattenErrorMessage(error);
// build model
yield return new JsonValidatorErrorModel(error.LineNumber, error.Path, message, error.ErrorType);
}
/// <summary>Get a flattened, human-readable message for a schema validation error and any child errors.</summary>
/// <param name="error">The error to represent.</param>
/// <param name="indent">The indentation level to apply for inner errors.</param>
private string FlattenErrorMessage(ValidationError error, int indent = 0)
{
// get override
string message = this.GetOverrideError(error);
if (message != null && message != this.TransparentToken)
return message;
// skip through transparent errors
if (this.IsTransparentError(error))
error = error.ChildErrors[0];
// get friendly representation of main error
message = error.Message;
switch (error.ErrorType)
{
case ErrorType.Const:
message = $"Invalid value. Found '{error.Value}', but expected '{error.Schema.Const}'.";
break;
case ErrorType.Enum:
message = $"Invalid value. Found '{error.Value}', but expected one of '{string.Join("', '", error.Schema.Enum)}'.";
break;
case ErrorType.Required:
message = $"Missing required fields: {string.Join(", ", (List<string>)error.Value)}.";
break;
}
// add inner errors
foreach (ValidationError childError in error.ChildErrors)
message += "\n" + "".PadLeft(indent * 2, ' ') + $"==> {childError.Path}: " + this.FlattenErrorMessage(childError, indent + 1);
return message;
}
/// <summary>Get whether a validation error should be omitted in favor of its child errors in user-facing error messages.</summary>
/// <param name="error">The error to check.</param>
private bool IsTransparentError(ValidationError error)
{
if (!error.ChildErrors.Any())
return false;
string @override = this.GetOverrideError(error);
return
@override == this.TransparentToken
|| (error.ErrorType == ErrorType.Then && @override == null);
}
/// <summary>Get an override error from the JSON schema, if any.</summary>
/// <param name="error">The schema validation error.</param>
private string GetOverrideError(ValidationError error)
{
string GetRawOverrideError()
{
// get override errors
IDictionary<string, string> errors = this.GetExtensionField<Dictionary<string, string>>(error.Schema, "@errorMessages");
if (errors == null)
return null;
errors = new Dictionary<string, string>(errors, StringComparer.OrdinalIgnoreCase);
// match error by type and message
foreach ((string target, string errorMessage) in errors)
{
if (!target.Contains(":"))
continue;
string[] parts = target.Split(':', 2);
if (parts[0].Equals(error.ErrorType.ToString(), StringComparison.OrdinalIgnoreCase) && Regex.IsMatch(error.Message, parts[1]))
return errorMessage?.Trim();
}
// match by type
return errors.TryGetValue(error.ErrorType.ToString(), out string message)
? message?.Trim()
: null;
}
return GetRawOverrideError()
?.Replace("@value", this.FormatValue(error.Value));
}
/// <summary>Get an extension field from a JSON schema.</summary>
/// <typeparam name="T">The field type.</typeparam>
/// <param name="schema">The schema whose extension fields to search.</param>
/// <param name="key">The case-insensitive field key.</param>
private T GetExtensionField<T>(JSchema schema, string key)
{
foreach ((string curKey, JToken value) in schema.ExtensionData)
{
if (curKey.Equals(key, StringComparison.OrdinalIgnoreCase))
return value.ToObject<T>();
}
return default;
}
/// <summary>Format a schema value for display.</summary>
/// <param name="value">The value to format.</param>
private string FormatValue(object value)
{
return value switch
{
List<string> list => string.Join(", ", list),
_ => value?.ToString() ?? "null"
};
}
}
}
|