diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-08-04 20:13:10 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-09-14 19:03:44 -0400 |
commit | 84ad8b2a92eac9155cada821c57d62a517b958a8 (patch) | |
tree | 5042330ea785c64ac1e45d05bdfb6cd22b098dde | |
parent | 5e8991bfcf7f287f595e858c34b8ac1a92c42b9b (diff) | |
download | SMAPI-84ad8b2a92eac9155cada821c57d62a517b958a8.tar.gz SMAPI-84ad8b2a92eac9155cada821c57d62a517b958a8.tar.bz2 SMAPI-84ad8b2a92eac9155cada821c57d62a517b958a8.zip |
fix manifest error if neither EntryDll nor ContentPackFor are specified (#654)
-rw-r--r-- | docs/technical/web.md | 10 | ||||
-rw-r--r-- | src/SMAPI.Web/Controllers/JsonValidatorController.cs | 26 | ||||
-rw-r--r-- | src/SMAPI.Web/wwwroot/schemas/manifest.json | 3 |
3 files changed, 31 insertions, 8 deletions
diff --git a/docs/technical/web.md b/docs/technical/web.md index 9884fefc..0d2039d8 100644 --- a/docs/technical/web.md +++ b/docs/technical/web.md @@ -29,13 +29,21 @@ format, with some special properties: * The root schema may have a `@documentationURL` field, which is the URL to the user-facing documentation for the format (if any). * Any part of the schema can define an `@errorMessages` field, which specifies user-friendly errors - which override the auto-generated messages. These are indexed by error type. For example: + which override the auto-generated messages. These can be indexed by error type: ```js "pattern": "^[a-zA-Z0-9_.-]+\\.dll$", "@errorMessages": { "pattern": "Invalid value; must be a filename ending with .dll." } ``` + ...or by error type and a regular expression applied to the default message (not recommended + unless the previous form doesn't work, since it's more likely to break in future versions): + ```js + "@errorMessages": { + "oneOf:valid against no schemas": "Missing required field: EntryDll or ContentPackFor.", + "oneOf:valid against more than one schema": "Can't specify both EntryDll or ContentPackFor, they're mutually exclusive." + } + ``` You can also reference these schemas in your JSON file directly using the `$schema` field, for text editors that support schema validation. For example: diff --git a/src/SMAPI.Web/Controllers/JsonValidatorController.cs b/src/SMAPI.Web/Controllers/JsonValidatorController.cs index 7b755d3b..b69a1006 100644 --- a/src/SMAPI.Web/Controllers/JsonValidatorController.cs +++ b/src/SMAPI.Web/Controllers/JsonValidatorController.cs @@ -2,6 +2,7 @@ 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 Microsoft.Extensions.Options; @@ -176,7 +177,7 @@ namespace StardewModdingAPI.Web.Controllers private string GetFlattenedError(ValidationError error, int indent = 0) { // get override error - string message = this.GetOverrideError(error.Schema, error.ErrorType); + string message = this.GetOverrideError(error.Schema, error.ErrorType, error.Message); if (message != null) return message; @@ -232,7 +233,8 @@ 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> - private string GetOverrideError(JSchema schema, ErrorType errorType) + /// <param name="message">The error message.</param> + private string GetOverrideError(JSchema schema, ErrorType errorType, string message) { // get override errors IDictionary<string, string> errors = this.GetExtensionField<Dictionary<string, string>>(schema, "@errorMessages"); @@ -240,10 +242,22 @@ namespace StardewModdingAPI.Web.Controllers return null; errors = new Dictionary<string, string>(errors, StringComparer.InvariantCultureIgnoreCase); - // get matching error - return errors.TryGetValue(errorType.ToString(), out string errorPhrase) - ? errorPhrase - : null; + // 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; + } + + // match by type + if (errors.TryGetValue(errorType.ToString(), out string error)) + return error; + + return null; } /// <summary>Get an extension field from a JSON schema.</summary> diff --git a/src/SMAPI.Web/wwwroot/schemas/manifest.json b/src/SMAPI.Web/wwwroot/schemas/manifest.json index 804eb53d..41d1ec2d 100644 --- a/src/SMAPI.Web/wwwroot/schemas/manifest.json +++ b/src/SMAPI.Web/wwwroot/schemas/manifest.json @@ -133,6 +133,7 @@ ], "additionalProperties": false, "@errorMessages": { - "oneOf": "Can't specify both EntryDll or ContentPackFor, they're mutually exclusive." + "oneOf:valid against no schemas": "Missing required field: EntryDll or ContentPackFor.", + "oneOf:valid against more than one schema": "Can't specify both EntryDll or ContentPackFor, they're mutually exclusive." } } |