Error messaging is one of the powerful features of the monaco editor that works alright for most json schema features. However, when using anyOf (or oneOf) to define multiple acceptable values for a property error messaging assumes that the schema uniquely maps to the property first defined in the anyOf array.
Screenshot:
In the above example, the issue is that the schema defines both rectangle and square as acceptable values for the p1.shape property but the error messaging insists that the value must be rectangle.
This is in contrast to enums for which it works perfectly as shown in the example below, but for complex schema that requires either anyOf or oneOf support, it falls short:
// JSON schema with anyOf having issues with eror messaging.
var jsonCode = ["{", ' "p1": {', ' "shape" : ""',' },','"p2" : ""', "}"].join("\n");
var modelUri = monaco.Uri.parse("a://b/foo.json"); // a made up unique URI for our model
var model = monaco.editor.createModel(jsonCode, "json", modelUri);
// configure the JSON language support with schemas and schema associations
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
validate: true,
schemaValidation: "error",
schemas: [
{
uri: "http://myserver/foo-schema.json", // id of the first schema
fileMatch: [modelUri.toString()], // associate with our model
schema: {
properties: {
p1: {
anyOf: [
{$ref: "http://myserver/rectangle.json"},
{$ref: "http://myserver/square.json"}
],
},
p2: {
type: "string",
enum: ["square", "rectangle"]
},
},
},
},
{
uri: "http://myserver/rectangle.json", // id of the second schema
schema: {
type: "object",
properties: {
"shape" : {
type: "string",
const: "rectangle"
}
},
},
},
{
uri: "http://myserver/square.json", // id of the second schema
schema: {
type: "object",
properties: {
shape: {
type: "string",
const: "square"
},
},
},
},
],
});
monaco.editor.create(document.getElementById("container"), {model: model});
One can solve this problem by defining a custom errorMessage property in each of the schema, but that creates another issue documented in #205.
Error messaging is one of the powerful features of the monaco editor that works alright for most json schema features. However, when using
anyOf
(oroneOf
) to define multiple acceptable values for a property error messaging assumes that the schema uniquely maps to the property first defined in theanyOf
array.Screenshot:
In the above example, the issue is that the schema defines both
rectangle
andsquare
as acceptable values for thep1.shape
property but the error messaging insists that the value must berectangle
.This is in contrast to enums for which it works perfectly as shown in the example below, but for complex schema that requires either
anyOf
oroneOf
support, it falls short:Appendix
Code for Monaco playground:
One can solve this problem by defining a custom
errorMessage
property in each of the schema, but that creates another issue documented in #205.