summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Controllers/JsonValidatorController.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-08-06 00:52:47 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-09-14 19:05:54 -0400
commite51638948f4355c27d6b3f02d637d4ed754ccb73 (patch)
tree9362b5f90619d4c9d92d8a7e1ae77af82fce08d1 /src/SMAPI.Web/Controllers/JsonValidatorController.cs
parent22480d25b9ba15df8cd26e788cd4a2d73f002a0d (diff)
downloadSMAPI-e51638948f4355c27d6b3f02d637d4ed754ccb73.tar.gz
SMAPI-e51638948f4355c27d6b3f02d637d4ed754ccb73.tar.bz2
SMAPI-e51638948f4355c27d6b3f02d637d4ed754ccb73.zip
add support for @value token in custom schema errors (#654)
Diffstat (limited to 'src/SMAPI.Web/Controllers/JsonValidatorController.cs')
-rw-r--r--src/SMAPI.Web/Controllers/JsonValidatorController.cs64
1 files changed, 41 insertions, 23 deletions
diff --git a/src/SMAPI.Web/Controllers/JsonValidatorController.cs b/src/SMAPI.Web/Controllers/JsonValidatorController.cs
index 9ded9c0d..cc2a7add 100644
--- a/src/SMAPI.Web/Controllers/JsonValidatorController.cs
+++ b/src/SMAPI.Web/Controllers/JsonValidatorController.cs
@@ -181,7 +181,7 @@ namespace StardewModdingAPI.Web.Controllers
private string GetFlattenedError(ValidationError error, int indent = 0)
{
// get override error
- string message = this.GetOverrideError(error.Schema, error.ErrorType, error.Message);
+ string message = this.GetOverrideError(error);
if (message != null)
return message;
@@ -235,33 +235,37 @@ namespace StardewModdingAPI.Web.Controllers
}
/// <summary>Get an override error from the JSON schema, if any.</summary>
- /// <param name="schema">The schema or subschema that raised the error.</param>
- /// <param name="errorType">The error type.</param>
- /// <param name="message">The error message.</param>
- private string GetOverrideError(JSchema schema, ErrorType errorType, string message)
+ /// <param name="error">The schema validation error.</param>
+ private string GetOverrideError(ValidationError error)
{
- // get override errors
- IDictionary<string, string> errors = this.GetExtensionField<Dictionary<string, string>>(schema, "@errorMessages");
- if (errors == null)
- return null;
- errors = new Dictionary<string, string>(errors, StringComparer.InvariantCultureIgnoreCase);
-
- // match error by type and message
- foreach (var pair in errors)
+ string GetRawOverrideError()
{
- if (!pair.Key.Contains(":"))
- continue;
+ // 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.InvariantCultureIgnoreCase);
+
+ // match error by type and message
+ foreach (var pair in errors)
+ {
+ if (!pair.Key.Contains(":"))
+ continue;
- string[] parts = pair.Key.Split(':', 2);
- if (parts[0].Equals(errorType.ToString(), StringComparison.InvariantCultureIgnoreCase) && Regex.IsMatch(message, parts[1]))
- return pair.Value;
- }
+ string[] parts = pair.Key.Split(':', 2);
+ if (parts[0].Equals(error.ErrorType.ToString(), StringComparison.InvariantCultureIgnoreCase) && Regex.IsMatch(error.Message, parts[1]))
+ return pair.Value;
+ }
- // match by type
- if (errors.TryGetValue(errorType.ToString(), out string error))
- return error;
+ // match by type
+ if (errors.TryGetValue(error.ErrorType.ToString(), out string message))
+ return message;
- return null;
+ return null;
+ }
+
+ return GetRawOverrideError()
+ ?.Replace("@value", this.FormatValue(error.Value));
}
/// <summary>Get an extension field from a JSON schema.</summary>
@@ -281,5 +285,19 @@ namespace StardewModdingAPI.Web.Controllers
return default;
}
+
+ /// <summary>Format a schema value for display.</summary>
+ /// <param name="value">The value to format.</param>
+ private string FormatValue(object value)
+ {
+ switch (value)
+ {
+ case List<string> list:
+ return string.Join(", ", list);
+
+ default:
+ return value?.ToString() ?? "null";
+ }
+ }
}
}