From e51638948f4355c27d6b3f02d637d4ed754ccb73 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 6 Aug 2019 00:52:47 -0400 Subject: add support for @value token in custom schema errors (#654) --- .../Controllers/JsonValidatorController.cs | 64 ++++++++++++++-------- 1 file changed, 41 insertions(+), 23 deletions(-) (limited to 'src') 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 } /// Get an override error from the JSON schema, if any. - /// The schema or subschema that raised the error. - /// The error type. - /// The error message. - private string GetOverrideError(JSchema schema, ErrorType errorType, string message) + /// The schema validation error. + private string GetOverrideError(ValidationError error) { - // get override errors - IDictionary errors = this.GetExtensionField>(schema, "@errorMessages"); - if (errors == null) - return null; - errors = new Dictionary(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 errors = this.GetExtensionField>(error.Schema, "@errorMessages"); + if (errors == null) + return null; + errors = new Dictionary(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)); } /// Get an extension field from a JSON schema. @@ -281,5 +285,19 @@ namespace StardewModdingAPI.Web.Controllers return default; } + + /// Format a schema value for display. + /// The value to format. + private string FormatValue(object value) + { + switch (value) + { + case List list: + return string.Join(", ", list); + + default: + return value?.ToString() ?? "null"; + } + } } } -- cgit