diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-08-06 03:52:32 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-09-14 19:06:19 -0400 |
commit | 807868f4404d850c29ad8ba4cd69beb9f08cecc6 (patch) | |
tree | d27da5208e684fc42b980e18b27334a62bb47657 /src/SMAPI.Web | |
parent | 674ceea74e74c5b0f432534dba981b5066ea7630 (diff) | |
download | SMAPI-807868f4404d850c29ad8ba4cd69beb9f08cecc6.tar.gz SMAPI-807868f4404d850c29ad8ba4cd69beb9f08cecc6.tar.bz2 SMAPI-807868f4404d850c29ad8ba4cd69beb9f08cecc6.zip |
add support for transparent schema errors with multiple child errors (#654)
Diffstat (limited to 'src/SMAPI.Web')
-rw-r--r-- | src/SMAPI.Web/Controllers/JsonValidatorController.cs | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/src/SMAPI.Web/Controllers/JsonValidatorController.cs b/src/SMAPI.Web/Controllers/JsonValidatorController.cs index cd0a6439..4f234449 100644 --- a/src/SMAPI.Web/Controllers/JsonValidatorController.cs +++ b/src/SMAPI.Web/Controllers/JsonValidatorController.cs @@ -46,6 +46,9 @@ namespace StardewModdingAPI.Web.Controllers /// <summary>The schema ID to use if none was specified.</summary> private string DefaultSchemaID = "manifest"; + /// <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 @@ -124,7 +127,7 @@ namespace StardewModdingAPI.Web.Controllers // validate JSON parsed.IsValid(schema, out IList<ValidationError> rawErrors); var errors = rawErrors - .Select(this.GetErrorModel) + .SelectMany(this.GetErrorModels) .ToArray(); return this.View("Index", result.AddErrors(errors)); } @@ -205,21 +208,25 @@ namespace StardewModdingAPI.Web.Controllers return null; } - /// <summary>Get a flattened representation representation of a schema validation error and any child errors.</summary> + /// <summary>Get view models representing a schema validation error and any child errors.</summary> /// <param name="error">The error to represent.</param> - private JsonValidatorErrorModel GetErrorModel(ValidationError error) + private IEnumerable<JsonValidatorErrorModel> GetErrorModels(ValidationError error) { // skip through transparent errors - while (this.GetOverrideError(error) == "$transparent" && error.ChildErrors.Count == 1) - error = error.ChildErrors[0]; + if (this.GetOverrideError(error) == this.TransparentToken && error.ChildErrors.Any()) + { + foreach (var model in error.ChildErrors.SelectMany(this.GetErrorModels)) + yield return model; + yield break; + } // get message string message = this.GetOverrideError(error); - if (message == null) + if (message == null || message == this.TransparentToken) message = this.FlattenErrorMessage(error); // build model - return new JsonValidatorErrorModel(error.LineNumber, error.Path, message, error.ErrorType); + 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> @@ -229,11 +236,11 @@ namespace StardewModdingAPI.Web.Controllers { // get override string message = this.GetOverrideError(error); - if (message != null) + if (message != null && message != this.TransparentToken) return message; // skip through transparent errors - while (this.GetOverrideError(error) == "$transparent" && error.ChildErrors.Count == 1) + while (this.GetOverrideError(error) == this.TransparentToken && error.ChildErrors.Count == 1) error = error.ChildErrors[0]; // get friendly representation of main error |