Rom1
Rom1

Reputation: 386

Monaco editor default json uri schema

I'm using monaco editor to edit JSON and I would like to set a custom diagnostic option. I'm trying that https://microsoft.github.io/monaco-editor/playground.html#extending-language-services-configure-json-defaults

// Configures two JSON schemas, with references.

var jsonCode = [
    '{',
    '    "p1": "v3",',
    '    "p2": false',
    "}"
].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,
    schemas: [{
        uri: "http://myserver/foo-schema.json", // id of the first schema
        fileMatch: [modelUri.toString()], // associate with our model
        schema: {
            type: "object",
            properties: {
                p1: {
                    enum: ["v1", "v2"]
                },
                p2: {
                    $ref: "http://myserver/bar-schema.json" // reference the second schema
                }
            }
        }
    }, {
        uri: "http://myserver/bar-schema.json", // id of the second schema
        schema: {
            type: "object",
            properties: {
                q1: {
                    enum: ["x1", "x2"]
                }
            }
        }
    }]
});

monaco.editor.create(document.getElementById("container"), {
    model: model
});

Where does uri: "http://myserver/foo-schema.json" come from ? I just want to use default JSON schema. Not my own.

Setting uri like this works : uri: "http://localhost:4200/assets/monaco-editor/min/vs/language/json/jsonMode.js",

But is there a clean way to set this value ? Maybe uri value for JSON is available somewhere ? I searched through monaco.languages.json.jsonDefaults but I did not find anything.

Upvotes: 6

Views: 5324

Answers (1)

mwag
mwag

Reputation: 4055

"http://myserver/foo-schema.json" is an arbitrary value-- you can make it anything you want. It only matters if you are also using enableSchemaRequest-- in which case it should point to the location that you want the schema to be fetched from-- but you're not doing that, so that doesn't matter. In fact, everything related to this URI is irrelevant to what you are trying to do, if I'm understanding your intent correctly.

When you say "I just want to use default JSON Schema, Not my own", I think what you mean to say is that you just want to ensure that it is valid JSON, right? Because, there is no such thing as "default JSON Schema"-- by definition, it is defined by you-- but there is such a thing as a formal definition of what JSON is (JSON Schema, on the other hand, assumes that you are already starting with valid JSON, and allows you to then define a schema that your (valid) JSON must conform to).

Assuming you just want to ensure it is valid JSON (but you don't care that the json conform to some custom schema), setting the language to 'json' is all you need to do and your code can be as simple as:

var myBadJSONText = '{this is not : "JSON"}'

monaco.editor.create(document.getElementById('container'), {
    language: 'json',
    value: myBadJSONText
});

which running in the Monaco playground gives you:

Upvotes: 3

Related Questions