Détail du package

codemirror-json-schema

acao117.4kMIT0.8.1

Codemirror 6 extensions that provide full JSONSchema support for @codemirror/lang-json and codemirror-json5

codemirror, codemirror6, jsonschema, jsonschema-validation

readme

Codemirror 6 extensions that provide full JSON Schema support for @codemirror/lang-json & codemirror-json5 language modes

npm

screenshot of the examples with json4 and json5 support enabled

Features

This is now a full-featured library for json schema for json, json5 and yaml as cm6 extensions!

  • ✅ lint validation messages from json schema
  • ✅ autocompletion with insert text from json schema
  • ✅ hover tooltips
  • ✅ dynamic, per-editor-instance schemas using codemirror StateField and linting refresh
  • ✅ markdown rendering for schema.description and custom formatHover and formatError configuration

Resources

Usage

To give you as much flexibility as possible, everything codemirror related is a peer or optional dependency

Based on whether you want to support json4, json5 or both, you will need to install the relevant language mode for our library to use.

Breaking Changes:

  • 0.7.0 - this version introduces markdown rendering in place of returning html strings, so any usage of formatHover and/or formatError configuration will be passed to markdown-it which doesn't handle html by default.
  • 0.5.0 - this breaking change only impacts those following the "custom usage" approach, it does not effect users using the high level, "bundled" jsonSchema() or json5Schema() modes. See the custom usages below to learn how to use the new stateExtensions and handleRefresh exports.

json4

with auto-install-peers true or similar:

npm install --save @codemirror/lang-json codemirror-json-schema

without auto-install-peers true:

npm install --save @codemirror/lang-json codemirror-json-schema @codemirror/language @codemirror/lint @codemirror/view @codemirror/state @lezer/common

Minimal Usage

This sets up @codemirror/lang-json and our extension for you. If you'd like to have more control over the related configurations, see custom usage below

import { EditorState } from "@codemirror/state";
import { jsonSchema } from "codemirror-json-schema";

const schema = {
  type: "object",
  properties: {
    example: {
      type: "boolean",
    },
  },
};

const json5State = EditorState.create({
  doc: "{ example: true }",
  extensions: [jsonSchema(schema)],
});

Custom Usage

This approach allows you to configure the json mode and parse linter, as well as our linter, hovers, etc more specifically.

import { EditorState } from "@codemirror/state";
import { linter } from "@codemirror/lint";
import { hoverTooltip } from "@codemirror/view";
import { json, jsonParseLinter, jsonLanguage } from "@codemirror/lang-json";

import {
  jsonSchemaLinter,
  jsonSchemaHover,
  jsonCompletion,
  stateExtensions,
  handleRefresh
} from "codemirror-json-schema";

const schema = {
  type: "object",
  properties: {
    example: {
      type: "boolean",
    },
  },
};

const state = EditorState.create({
  doc: `{ "example": true }`,
  extensions: [
    json(),
    linter(jsonParseLinter(), {
      // default is 750ms
      delay: 300
    }),
    linter(jsonSchemaLinter(), {
      needsRefresh: handleRefresh,
    }),
    jsonLanguage.data.of({
      autocomplete: jsonCompletion(),
    }),
    hoverTooltip(jsonSchemaHover()),
    stateExtensions(schema)
  ];
})

json5

with auto-install-peers true or similar:

npm install --save codemirror-json5 codemirror-json-schema

without auto-install-peers true:

npm install --save codemirror-json5 codemirror-json-schema @codemirror/language @codemirror/lint @codemirror/view @codemirror/state @lezer/common

Minimal Usage

This sets up codemirror-json5 mode for you. If you'd like to have more control over the related configurations, see custom usage below

import { EditorState } from "@codemirror/state";
import { json5Schema } from "codemirror-json-schema/json5";

const schema = {
  type: "object",
  properties: {
    example: {
      type: "boolean",
    },
  },
};

const json5State = EditorState.create({
  doc: `{
    example: true,
    // json5 is awesome!
  }`,
  extensions: [json5Schema(schema)],
});

Custom Usage

This approach allows you to configure the json5 mode and parse linter, as well as our linter, hovers, etc more specifically.

import { EditorState } from "@codemirror/state";
import { linter } from "@codemirror/lint";
import { json5, json5ParseLinter, json5Language } from "codemirror-json5";
import {
  json5SchemaLinter,
  json5SchemaHover,
  json5Completion,
} from "codemirror-json-schema/json5";
import { stateExtensions, handleRefresh } from "codemirror-json-schema";

const schema = {
  type: "object",
  properties: {
    example: {
      type: "boolean",
    },
  },
};

const json5State = EditorState.create({
  doc: `{
    example: true,
    // json5 is awesome!
  }`,
  extensions: [
    json5(),
    linter(json5ParseLinter(), {
      // the default linting delay is 750ms
      delay: 300,
    }),
    linter(
      json5SchemaLinter({
        needsRefresh: handleRefresh,
      })
    ),
    hoverTooltip(json5SchemaHover()),
    json5Language.data.of({
      autocomplete: json5Completion(),
    }),
    stateExtensions(schema),
  ],
});

Dynamic Schema

If you want to, you can provide schema dynamically, in several ways. This works the same for either json or json5, using the underlying codemirror 6 StateFields, via the updateSchema method export.

In this example

  • the initial schema state is empty
  • schema is loaded dynamically based on user input
  • the linting refresh will be handled automatically, because it's built into our bundled jsonSchema() and json5Schema() modes
import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";

import { json5Schema } from "codemirror-json-schema/json5";

import { updateSchema } from "codemirror-json-schema";

const json5State = EditorState.create({
  doc: `{
    example: true,
    // json5 is awesome!
  }`,
  // note: you can still provide initial
  // schema when creating state
  extensions: [json5Schema()],
});

const editor = new EditorView({ state: json5State });

const schemaSelect = document.getElementById("schema-selection");

schemaSelect!.onchange = async (e) => {
  const val = e.target!.value!;
  if (!val) {
    return;
  }
  // parse the remote schema spec to json
  const data = await (
    await fetch(`https://json.schemastore.org/${val}`)
  ).json();
  // this will update the schema state field, in an editor specific way
  updateSchema(editor, data);
};

if you are using the "custom path" with this approach, you will need to configure linting refresh as well:

import { linter } from "@codemirror/lint";
import { json5SchemaLinter } from "codemirror-json-schema/json5";
import { handleRefresh } from "codemirror-json-schema";

const state = EditorState.create({
  // ...
  extensions: [
    linter(json5SchemaLinter(), {
      needsRefresh: handleRefresh,
    })
  ];
}

Current Constraints:

  • currently only tested with standard schemas using json4 spec. results may vary
  • doesn't place cursor inside known insert text yet
  • currently you can only override the texts and rendering of a hover. we plan to add the same for validation errors and autocomplete

Inspiration

monaco-json and monaco-yaml both provide json schema features for json, cson and yaml, and we want the nascent codemirror 6 to have them as well!

Also, json5 is slowly growing in usage, and it needs full language support for the browser!

changelog

codemirror-json-schema

0.8.1

Patch Changes

  • #151 d360a86 Thanks @skrabe! - Fixed validation bugs: single objects incorrectly passed array schemas, invalid YAML caused errors after root-level change(now skipped if unparseable), and added tests ensuring non-array values(object, boolean, string, number) are correctly rejected.

0.8.0

Minor Changes

0.7.9

Patch Changes

0.7.8

Patch Changes

0.7.7

Patch Changes

0.7.6

Patch Changes

  • #115 c8d2594 Thanks @acao! - set @codemirror/autocomplete as an optional peer, at a fix version for a bug with curly braces

0.7.5

Patch Changes

  • #112 ccffa61 Thanks @acao! - fixes bundling - remove .js imports and remains as moduleResolution: 'Node' to match cm6

0.7.4

Patch Changes

0.7.3

Patch Changes

0.7.2

Patch Changes

0.7.1

Patch Changes

0.7.0

Minor Changes

  • #85 c694451 Thanks @imolorhe! - Added YAML support, switched back to markdown for messages, provide markdown rendering, and fix some autocompletion issues

0.6.1

Patch Changes

0.6.0

Minor Changes

  • #64 0aaf308 Thanks @acao! - Breaking Change: replaces backticks with <code> blocks in hover and completion! This just seemed to make more sense.

    • upgrade json-schema-library to the latest 8.x with patch fixes, remove "forked" pointer step logic
    • after autocompleting a property, when there is empty value, provide full autocomplete options
    • as noted in the breaking change notice, all psuedo-markdown backtick ``delimiters are replaced with<code>

0.5.1

Patch Changes

0.5.0

Minor Changes

  • #63 a73c517 Thanks @acao!

  • breaking change: only impacts those following the "custom usage" approach, it does not effect users using the high level, "bundled" jsonSchema() or json5Schema() modes.

    Previously, we ask you to pass schema to each of the linter, completion and hover extensions.

    Now, we ask you to use these new exports to instantiate your schema like this, with stateExtensions(schema) as a new extension, and the only one that you pass schema to, like so:

    import type { JSONSchema7 } from "json-schema";
    import { json, jsonLanguage, jsonParseLinter } from "@codemirror/lang-json";
    import { hoverTooltip } from "@codemirror/view";
    import { linter } from "@codemirror/lint";
    
    import {
      jsonCompletion,
      handleRefresh,
      jsonSchemaLinter,
      jsonSchemaHover,
      stateExtensions,
    } from "codemirror-json-schema";
    
    import schema from "./myschema.json";
    
    // ...
    extensions: [
      json(),
      linter(jsonParseLinter()),
      linter(jsonSchemaLinter(), {
        needsRefresh: handleRefresh,
      }),
      jsonLanguage.data.of({
        autocomplete: jsonCompletion(),
      }),
      hoverTooltip(jsonSchemaHover()),
      // this is where we pass the schema!
      // very important!!!!
      stateExtensions(schema),
    ];
  • upgrade to use full .js import paths for NodeNext compatibility, however not all of our dependencies are compatible with this mode, thus we continue using the legacy nodeResolution strategy.

0.4.5

Patch Changes

  • #70 4c9ca0a Thanks @acao! - Fix vulnerability message for json-schema type dependency

0.4.4

Patch Changes

0.4.3

Patch Changes

0.4.2

Patch Changes

  • 14a26f8 Thanks @acao! - fix nested json4 completion bug (#55)

    • fix #54, expand properties inside nested objects as expected in json4
    • always advance cursor after property completions
    • add more test coverage

0.4.1

Patch Changes

0.4.0

Minor Changes

0.3.2

Patch Changes

0.3.1

Patch Changes

  • #37 1220706 Thanks @acao! - - fix hover on undefined schema props

    • configure above: true for the hover tooltip, to have vscode-like behavior, and prevent z-index clash with completion on smaller viewports
  • #36 23e5721 Thanks @imolorhe! - fixed autocompletion in object roots, etc, for json4 and json5

0.3.0

Minor Changes

  • d4cfe11: improve autocompletion with support for allOf, anyOf, oneOf

0.2.3

Patch Changes

  • 69ab7be: Fix bug on p/npm/yarn install with postinstall

0.2.2

Patch Changes

  • 4e80f37: hover bugs with complex types #26

0.2.1

Patch Changes

  • 0b34915: fix: hover format for anyOf

0.2.0

Minor Changes

  • 3a578e9: move everything codemirror related to a peer dependency. see readme for new install instructions

0.1.2

Patch Changes

  • d17f63f: fix readme

0.1.1

Patch Changes

  • 7f5af9d: Add formatting for complex types - oneOf, anyOf, allOf on hover

0.1.0

Minor Changes

  • 26bda14: add json5 support, simpler exports