fix manifest error if neither EntryDll nor ContentPackFor are specified (#654)
This commit is contained in:
parent
5e8991bfcf
commit
84ad8b2a92
|
@ -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:
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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."
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue