Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

codemirror-json-schema

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

codemirror-json-schema - npm Package Compare versions

Comparing version 0.5.1-canary to 0.5.1

cjs/state.d.ts

12

CHANGELOG.md
# codemirror-json-schema
## 0.5.1
### Patch Changes
- [`7ed9e3e`](https://github.com/acao/codemirror-json-schema/commit/7ed9e3e206ec7a47f8f7dde7d2a50a75228ae0be) Thanks [@acao](https://github.com/acao)! - fix required fields validation
## 0.5.0

@@ -7,6 +13,4 @@

- [#63](https://github.com/acao/codemirror-json-schema/pull/63) [`a73c517`](https://github.com/acao/codemirror-json-schema/commit/a73c517722bbe9d37124993117c091e259eb6998) Thanks [@acao](https://github.com/acao)!
- [#63](https://github.com/acao/codemirror-json-schema/pull/63) [`a73c517`](https://github.com/acao/codemirror-json-schema/commit/a73c517722bbe9d37124993117c091e259eb6998) Thanks [@acao](https://github.com/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.
- **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.

@@ -49,4 +53,2 @@

- 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

@@ -53,0 +55,0 @@

@@ -6,2 +6,2 @@ import { JSONSchema7 } from "json-schema";

*/
export declare function jsonSchema(schema: JSONSchema7): import("@codemirror/state").Extension[];
export declare function jsonSchema(schema?: JSONSchema7): import("@codemirror/state").Extension[];

@@ -6,5 +6,6 @@ "use strict";

const view_1 = require("@codemirror/view");
const json_completion_1 = require("./json-completion");
const json_validation_1 = require("./json-validation");
const json_hover_1 = require("./json-hover");
const json_completion_js_1 = require("./json-completion.js");
const json_validation_js_1 = require("./json-validation.js");
const json_hover_js_1 = require("./json-hover.js");
const state_js_1 = require("./state.js");
const lint_1 = require("@codemirror/lint");

@@ -19,9 +20,12 @@ /**

(0, lint_1.linter)((0, lang_json_1.jsonParseLinter)()),
(0, lint_1.linter)((0, json_validation_1.jsonSchemaLinter)(schema)),
(0, lint_1.linter)((0, json_validation_js_1.jsonSchemaLinter)(), {
needsRefresh: json_validation_js_1.handleRefresh,
}),
lang_json_1.jsonLanguage.data.of({
autocomplete: (0, json_completion_1.jsonCompletion)(schema),
autocomplete: (0, json_completion_js_1.jsonCompletion)(),
}),
(0, view_1.hoverTooltip)((0, json_hover_1.jsonSchemaHover)(schema)),
(0, view_1.hoverTooltip)((0, json_hover_js_1.jsonSchemaHover)()),
(0, state_js_1.stateExtensions)(schema),
];
}
exports.jsonSchema = jsonSchema;

@@ -1,7 +0,8 @@

export { jsonCompletion } from "./json-completion";
export { jsonSchemaLinter, type JSONValidationOptions, } from "./json-validation";
export { jsonSchemaHover, type HoverOptions, type FoundCursorData, type CursorData, } from "./json-hover";
export { jsonSchema } from "./bundled";
export type { JSONPointersMap, JSONPointerData, JSONPartialPointerData, } from "./types";
export * from "./utils/parseJSONDocument";
export * from "./utils/jsonPointers";
export { jsonCompletion } from "./json-completion.js";
export { jsonSchemaLinter, type JSONValidationOptions, } from "./json-validation.js";
export { jsonSchemaHover, type HoverOptions, type FoundCursorData, type CursorData, } from "./json-hover.js";
export { jsonSchema } from "./bundled.js";
export type { JSONPointersMap, JSONPointerData, JSONPartialPointerData, } from "./types.js";
export * from "./utils/parseJSONDocument.js";
export * from "./utils/jsonPointers.js";
export * from "./state.js";

@@ -18,11 +18,12 @@ "use strict";

exports.jsonSchema = exports.jsonSchemaHover = exports.jsonSchemaLinter = exports.jsonCompletion = void 0;
var json_completion_1 = require("./json-completion");
Object.defineProperty(exports, "jsonCompletion", { enumerable: true, get: function () { return json_completion_1.jsonCompletion; } });
var json_validation_1 = require("./json-validation");
Object.defineProperty(exports, "jsonSchemaLinter", { enumerable: true, get: function () { return json_validation_1.jsonSchemaLinter; } });
var json_hover_1 = require("./json-hover");
Object.defineProperty(exports, "jsonSchemaHover", { enumerable: true, get: function () { return json_hover_1.jsonSchemaHover; } });
var bundled_1 = require("./bundled");
Object.defineProperty(exports, "jsonSchema", { enumerable: true, get: function () { return bundled_1.jsonSchema; } });
__exportStar(require("./utils/parseJSONDocument"), exports);
__exportStar(require("./utils/jsonPointers"), exports);
var json_completion_js_1 = require("./json-completion.js");
Object.defineProperty(exports, "jsonCompletion", { enumerable: true, get: function () { return json_completion_js_1.jsonCompletion; } });
var json_validation_js_1 = require("./json-validation.js");
Object.defineProperty(exports, "jsonSchemaLinter", { enumerable: true, get: function () { return json_validation_js_1.jsonSchemaLinter; } });
var json_hover_js_1 = require("./json-hover.js");
Object.defineProperty(exports, "jsonSchemaHover", { enumerable: true, get: function () { return json_hover_js_1.jsonSchemaHover; } });
var bundled_js_1 = require("./bundled.js");
Object.defineProperty(exports, "jsonSchema", { enumerable: true, get: function () { return bundled_js_1.jsonSchema; } });
__exportStar(require("./utils/parseJSONDocument.js"), exports);
__exportStar(require("./utils/jsonPointers.js"), exports);
__exportStar(require("./state.js"), exports);

@@ -8,6 +8,6 @@ import { CompletionContext, CompletionResult } from "@codemirror/autocomplete";

export declare class JSONCompletion {
private opts;
private schema;
private opts;
constructor(schema: JSONSchema7, opts: JSONCompletionOptions);
doComplete(ctx: CompletionContext): CompletionResult;
constructor(opts: JSONCompletionOptions);
doComplete(ctx: CompletionContext): CompletionResult | never[];
private applySnippetCompletion;

@@ -38,3 +38,3 @@ private getPropertyCompletions;

*/
export declare function jsonCompletion(schema: JSONSchema7, opts?: JSONCompletionOptions): (ctx: CompletionContext) => CompletionResult;
export declare function jsonCompletion(opts?: JSONCompletionOptions): (ctx: CompletionContext) => CompletionResult | never[];
/**

@@ -44,3 +44,3 @@ * provides a JSON schema enabled autocomplete extension for codemirror and json5

*/
export declare function json5Completion(schema: JSONSchema7, opts?: Omit<JSONCompletionOptions, "mode">): (ctx: CompletionContext) => CompletionResult;
export declare function json5Completion(opts?: Omit<JSONCompletionOptions, "mode">): (ctx: CompletionContext) => CompletionResult | never[];
export {};

@@ -9,8 +9,9 @@ "use strict";

const language_1 = require("@codemirror/language");
const debug_1 = require("./utils/debug");
const node_1 = require("./utils/node");
const debug_js_1 = require("./utils/debug.js");
const node_js_1 = require("./utils/node.js");
const json_schema_library_1 = require("json-schema-library");
const jsonPointers_1 = require("./utils/jsonPointers");
const constants_1 = require("./constants");
const getSchema_1 = __importDefault(require("./utils/schema-lib/getSchema"));
const jsonPointers_js_1 = require("./utils/jsonPointers.js");
const constants_js_1 = require("./constants.js");
const state_js_1 = require("./state.js");
const getSchema_js_1 = __importDefault(require("./utils/schema-lib/getSchema.js"));
function json5PropertyInsertSnippet(rawWord, value) {

@@ -41,8 +42,14 @@ if (rawWord.startsWith('"')) {

class JSONCompletion {
constructor(schema, opts) {
this.schema = schema;
constructor(opts) {
this.opts = opts;
this.schema = null;
}
doComplete(ctx) {
var _a, _b;
this.schema = (0, state_js_1.getJSONSchema)(ctx.state);
if (!this.schema) {
// todo: should we even do anything without schema
// without taking over the existing mode responsibilties?
return [];
}
const result = {

@@ -55,15 +62,15 @@ from: ctx.pos,

const text = ctx.state.doc.sliceString(0);
let node = (0, node_1.getNodeAtPosition)(ctx.state, ctx.pos);
let node = (0, node_js_1.getNodeAtPosition)(ctx.state, ctx.pos);
// position node word prefix (without quotes) for matching
const prefix = ctx.state.sliceDoc(node.from, ctx.pos).replace(/^("|')/, "");
debug_1.debug.log("xxx", "node", node, "prefix", prefix, "ctx", ctx);
debug_js_1.debug.log("xxx", "node", node, "prefix", prefix, "ctx", ctx);
// Only show completions if we are filling out a word or right after the starting quote, or if explicitly requested
if (!((0, node_1.isPrimitiveValueNode)(node) || (0, node_1.isPropertyNameNode)(node)) &&
if (!((0, node_js_1.isPrimitiveValueNode)(node) || (0, node_js_1.isPropertyNameNode)(node)) &&
!ctx.explicit) {
return result;
}
const currentWord = (0, node_1.getWord)(ctx.state.doc, node);
const rawWord = (0, node_1.getWord)(ctx.state.doc, node, false);
const currentWord = (0, node_js_1.getWord)(ctx.state.doc, node);
const rawWord = (0, node_js_1.getWord)(ctx.state.doc, node, false);
// Calculate overwrite range
if (node && ((0, node_1.isPrimitiveValueNode)(node) || (0, node_1.isPropertyNameNode)(node))) {
if (node && ((0, node_js_1.isPrimitiveValueNode)(node) || (0, node_js_1.isPropertyNameNode)(node))) {
result.from = node.from;

@@ -75,5 +82,5 @@ result.to = node.to;

const overwriteStart = ctx.pos - currentWord.length;
debug_1.debug.log("xxx", "overwriteStart after", overwriteStart, "ctx.pos", ctx.pos, "word", word, "currentWord", currentWord, "=>", text[overwriteStart - 1], "..", text[overwriteStart], "..", text);
debug_js_1.debug.log("xxx", "overwriteStart after", overwriteStart, "ctx.pos", ctx.pos, "word", word, "currentWord", currentWord, "=>", text[overwriteStart - 1], "..", text[overwriteStart], "..", text);
result.from =
node.name === constants_1.TOKENS.INVALID ? (_a = word === null || word === void 0 ? void 0 : word.from) !== null && _a !== void 0 ? _a : ctx.pos : overwriteStart;
node.name === constants_js_1.TOKENS.INVALID ? (_a = word === null || word === void 0 ? void 0 : word.from) !== null && _a !== void 0 ? _a : ctx.pos : overwriteStart;
result.to = ctx.pos;

@@ -83,16 +90,16 @@ }

let addValue = true;
if ((0, node_1.isPropertyNameNode)(node)) {
if ((0, node_js_1.isPropertyNameNode)(node)) {
const parent = node.parent;
if (parent) {
// get value node from parent
const valueNode = (0, node_1.getChildValueNode)(parent);
const valueNode = (0, node_js_1.getChildValueNode)(parent);
addValue =
!valueNode ||
(valueNode.name === constants_1.TOKENS.INVALID &&
(valueNode.name === constants_js_1.TOKENS.INVALID &&
valueNode.from - valueNode.to === 0);
debug_1.debug.log("xxx", "addValue", addValue, (0, node_1.getChildValueNode)(parent), node);
debug_js_1.debug.log("xxx", "addValue", addValue, (0, node_js_1.getChildValueNode)(parent), node);
// find object node
node =
(_b = [parent, parent.parent].find((p) => {
if ((p === null || p === void 0 ? void 0 : p.name) === constants_1.TOKENS.OBJECT) {
if ((p === null || p === void 0 ? void 0 : p.name) === constants_js_1.TOKENS.OBJECT) {
return true;

@@ -104,7 +111,7 @@ }

}
debug_1.debug.log("xxx", node, currentWord, ctx);
debug_js_1.debug.log("xxx", node, currentWord, ctx);
// proposals for properties
if (node &&
(node.name === constants_1.TOKENS.OBJECT || node.name === constants_1.TOKENS.JSON_TEXT) &&
(0, node_1.isPropertyNameNode)((0, node_1.getNodeAtPosition)(ctx.state, ctx.pos))) {
(node.name === constants_js_1.TOKENS.OBJECT || node.name === constants_js_1.TOKENS.JSON_TEXT) &&
(0, node_js_1.isPropertyNameNode)((0, node_js_1.getNodeAtPosition)(ctx.state, ctx.pos))) {
// don't suggest keys when the cursor is just before the opening curly brace

@@ -124,4 +131,4 @@ if (node.from === ctx.pos) {

// handle filtering
result.options = Array.from(collector.completions.values()).filter((v) => (0, node_1.stripSurroundingQuotes)(v.label).startsWith(prefix));
debug_1.debug.log("xxx", "result", result, "prefix", prefix, "collector.completions", collector.completions, "reservedKeys", collector.reservedKeys);
result.options = Array.from(collector.completions.values()).filter((v) => (0, node_js_1.stripSurroundingQuotes)(v.label).startsWith(prefix));
debug_js_1.debug.log("xxx", "result", result, "prefix", prefix, "collector.completions", collector.completions, "reservedKeys", collector.reservedKeys);
return result;

@@ -136,7 +143,7 @@ }

// don't suggest properties that are already present
const properties = node.getChildren(constants_1.TOKENS.PROPERTY);
debug_1.debug.log("xxx", "getPropertyCompletions", node, ctx, properties);
const properties = node.getChildren(constants_js_1.TOKENS.PROPERTY);
debug_js_1.debug.log("xxx", "getPropertyCompletions", node, ctx, properties);
properties.forEach((p) => {
const key = (0, node_1.getWord)(ctx.state.doc, p.getChild(constants_1.TOKENS.PROPERTY_NAME));
collector.reserve((0, node_1.stripSurroundingQuotes)(key));
const key = (0, node_js_1.getWord)(ctx.state.doc, p.getChild(constants_js_1.TOKENS.PROPERTY_NAME));
collector.reserve((0, node_js_1.stripSurroundingQuotes)(key));
});

@@ -292,3 +299,3 @@ // TODO: Handle separatorAfter

if (!value || nValueProposals > 1) {
debug_1.debug.log("xxx", "value", value, "nValueProposals", nValueProposals, propertySchema);
debug_js_1.debug.log("xxx", "value", value, "nValueProposals", nValueProposals, propertySchema);
value = "#{}";

@@ -334,4 +341,4 @@ }

let parentKey = undefined;
debug_1.debug.log("xxx", "getValueCompletions", node, ctx);
if (node && (0, node_1.isPrimitiveValueNode)(node)) {
debug_js_1.debug.log("xxx", "getValueCompletions", node, ctx);
if (node && (0, node_js_1.isPrimitiveValueNode)(node)) {
valueNode = node;

@@ -344,11 +351,11 @@ node = node.parent;

}
if (node.name === constants_1.TOKENS.PROPERTY) {
const keyNode = node.getChild(constants_1.TOKENS.PROPERTY_NAME);
if (node.name === constants_js_1.TOKENS.PROPERTY) {
const keyNode = node.getChild(constants_js_1.TOKENS.PROPERTY_NAME);
if (keyNode) {
parentKey = (0, node_1.getWord)(ctx.state.doc, keyNode);
parentKey = (0, node_js_1.getWord)(ctx.state.doc, keyNode);
node = node.parent;
}
}
debug_1.debug.log("xxx", "node", node, "parentKey", parentKey);
if (node && (parentKey !== undefined || node.name === constants_1.TOKENS.ARRAY)) {
debug_js_1.debug.log("xxx", "node", node, "parentKey", parentKey);
if (node && (parentKey !== undefined || node.name === constants_js_1.TOKENS.ARRAY)) {
// Get matching schemas

@@ -360,3 +367,3 @@ const schemas = this.getSchemas(schema, ctx);

}
if (node.name === constants_1.TOKENS.ARRAY && s.items) {
if (node.name === constants_js_1.TOKENS.ARRAY && s.items) {
let c = collector;

@@ -377,3 +384,3 @@ if (s.uniqueItems) {

// get index of next node in array
const foundIdx = (0, node_1.findNodeIndexInArrayNode)(node, valueNode);
const foundIdx = (0, node_js_1.findNodeIndexInArrayNode)(node, valueNode);
if (foundIdx >= 0) {

@@ -529,4 +536,4 @@ arrayIndex = foundIdx;

const draft = new json_schema_library_1.Draft07(this.schema);
let pointer = (0, jsonPointers_1.jsonPointerForPosition)(ctx.state, ctx.pos);
let subSchema = (0, getSchema_1.default)(draft, pointer);
let pointer = (0, jsonPointers_js_1.jsonPointerForPosition)(ctx.state, ctx.pos);
let subSchema = (0, getSchema_js_1.default)(draft, pointer);
// if we don't have a schema for the current pointer, try the parent pointer

@@ -538,5 +545,5 @@ if (!subSchema ||

pointer = pointer.replace(/\/[^/]*$/, "/");
subSchema = (0, getSchema_1.default)(draft, pointer);
subSchema = (0, getSchema_js_1.default)(draft, pointer);
}
debug_1.debug.log("xxx", "pointer..", JSON.stringify(pointer));
debug_js_1.debug.log("xxx", "pointer..", JSON.stringify(pointer));
// For some reason, it returns undefined schema for the root pointer

@@ -547,3 +554,3 @@ if (!pointer || pointer === "/") {

// const subSchema = new Draft07(this.schema).getSchema(pointer);
debug_1.debug.log("xxx", "subSchema..", subSchema);
debug_js_1.debug.log("xxx", "subSchema..", subSchema);
if (this.isJsonError(subSchema)) {

@@ -635,4 +642,4 @@ return [];

*/
function jsonCompletion(schema, opts = {}) {
const completion = new JSONCompletion(schema, opts);
function jsonCompletion(opts = {}) {
const completion = new JSONCompletion(opts);
return function jsonDoCompletion(ctx) {

@@ -647,4 +654,4 @@ return completion.doComplete(ctx);

*/
function json5Completion(schema, opts = {}) {
const completion = new JSONCompletion(schema, Object.assign(Object.assign({}, opts), { mode: "json5" }));
function json5Completion(opts = {}) {
const completion = new JSONCompletion(Object.assign(Object.assign({}, opts), { mode: "json5" }));
return function jsonDoCompletion(ctx) {

@@ -651,0 +658,0 @@ return completion.doComplete(ctx);

import { type EditorView, Tooltip } from "@codemirror/view";
import { type Draft, JsonSchema } from "json-schema-library";
import type { JSONSchema7 } from "json-schema";
import { Side } from "./types";
import { Side } from "./types.js";
export type CursorData = {

@@ -33,7 +32,7 @@ schema?: JsonSchema;

*/
export declare function jsonSchemaHover(schema: JSONSchema7, options?: HoverOptions): (view: EditorView, pos: number, side: Side) => Promise<Tooltip | null>;
export declare function jsonSchemaHover(options?: HoverOptions): (view: EditorView, pos: number, side: Side) => Promise<Tooltip | null>;
export declare class JSONHover {
private opts?;
private schema;
constructor(schema: JSONSchema7, opts?: HoverOptions | undefined);
constructor(opts?: HoverOptions | undefined);
getDataForCursor(view: EditorView, pos: number, side: Side): CursorData | null;

@@ -40,0 +39,0 @@ private formatMessage;

@@ -8,7 +8,8 @@ "use strict";

const json_schema_library_1 = require("json-schema-library");
const jsonPointers_1 = require("./utils/jsonPointers");
const formatting_1 = require("./utils/formatting");
const getSchema_1 = __importDefault(require("./utils/schema-lib/getSchema"));
const debug_1 = require("./utils/debug");
const dom_1 = require("./utils/dom");
const jsonPointers_js_1 = require("./utils/jsonPointers.js");
const formatting_js_1 = require("./utils/formatting.js");
const getSchema_js_1 = __importDefault(require("./utils/schema-lib/getSchema.js"));
const debug_js_1 = require("./utils/debug.js");
const dom_js_1 = require("./utils/dom.js");
const state_js_1 = require("./state.js");
/**

@@ -18,4 +19,4 @@ * provides a JSON schema enabled tooltip extension for codemirror

*/
function jsonSchemaHover(schema, options) {
const hover = new JSONHover(schema, options);
function jsonSchemaHover(options) {
const hover = new JSONHover(options);
return async function jsonDoHover(view, pos, side) {

@@ -27,3 +28,3 @@ return hover.doHover(view, pos, side);

function formatComplexType(schema, type, draft) {
return `${type}: ${(0, formatting_1.joinWithOr)(schema[type].map((s) => {
return `${type}: ${(0, formatting_js_1.joinWithOr)(schema[type].map((s) => {
var _a;

@@ -34,9 +35,16 @@ return (_a = s.type) !== null && _a !== void 0 ? _a : draft.resolveRef(s).type;

class JSONHover {
constructor(schema, opts) {
constructor(opts) {
this.opts = opts;
this.schema = new json_schema_library_1.Draft04(schema);
this.schema = null;
this.opts = Object.assign({ parser: JSON.parse }, this.opts);
}
getDataForCursor(view, pos, side) {
const pointer = (0, jsonPointers_1.jsonPointerForPosition)(view.state, pos, side);
const schema = (0, state_js_1.getJSONSchema)(view.state);
if (!schema) {
// todo: should we even do anything without schema
// without taking over the existing mode responsibilties?
return null;
}
this.schema = new json_schema_library_1.Draft04(schema);
const pointer = (0, jsonPointers_js_1.jsonPointerForPosition)(view.state, pos, side);
let data = undefined;

@@ -52,6 +60,6 @@ // TODO: use the AST tree to return the right hand, data so that we don't have to parse the doc

// if the data is valid, we can infer a type for complex types
let subSchema = (0, getSchema_1.default)(this.schema, pointer, data);
let subSchema = (0, getSchema_js_1.default)(this.schema, pointer, data);
if (subSchema.type === "error" && data !== undefined) {
// if the data is invalid, we won't get the type - try again without the data
subSchema = (0, getSchema_1.default)(this.schema, pointer, undefined);
subSchema = (0, getSchema_js_1.default)(this.schema, pointer, undefined);
if (subSchema.type === "error") {

@@ -66,15 +74,15 @@ return { pointer };

if (message) {
return (0, dom_1.el)("div", { class: "cm6-json-schema-hover" }, [
(0, dom_1.el)("div", {
return (0, dom_js_1.el)("div", { class: "cm6-json-schema-hover" }, [
(0, dom_js_1.el)("div", {
class: "cm6-json-schema-hover--description",
text: message,
}),
(0, dom_1.el)("div", { class: "cm6-json-schema-hover--code-wrapper" }, [
(0, dom_1.el)("code", { class: "cm6-json-schema-hover--code", text: typeInfo }),
(0, dom_js_1.el)("div", { class: "cm6-json-schema-hover--code-wrapper" }, [
(0, dom_js_1.el)("code", { class: "cm6-json-schema-hover--code", text: typeInfo }),
]),
]);
}
return (0, dom_1.el)("div", { class: "cm6-json-schema-hover" }, [
(0, dom_1.el)("div", { class: "cm6-json-schema-hover--code-wrapper" }, [
(0, dom_1.el)("code", { class: "cm6-json-schema-hover--code", text: typeInfo }),
return (0, dom_js_1.el)("div", { class: "cm6-json-schema-hover" }, [
(0, dom_js_1.el)("div", { class: "cm6-json-schema-hover--code-wrapper" }, [
(0, dom_js_1.el)("code", { class: "cm6-json-schema-hover--code", text: typeInfo }),
]),

@@ -98,3 +106,3 @@ ]);

typeInfo = Array.isArray(schema.type)
? (0, formatting_1.joinWithOr)(schema.type)
? (0, formatting_js_1.joinWithOr)(schema.type)
: schema.type;

@@ -136,3 +144,3 @@ }

catch (err) {
debug_1.debug.log(err);
debug_js_1.debug.log(err);
return null;

@@ -139,0 +147,0 @@ }

@@ -1,6 +0,5 @@

import type { EditorView } from "@codemirror/view";
import type { Diagnostic } from "@codemirror/lint";
import type { JSONSchema7 } from "json-schema";
import type { EditorView, ViewUpdate } from "@codemirror/view";
import { type Diagnostic } from "@codemirror/lint";
import { type JsonError } from "json-schema-library";
import { parseJSONDocumentState } from "./utils/parseJSONDocument";
import { parseJSONDocumentState } from "./utils/parseJSONDocument.js";
export type JSONValidationOptions = {

@@ -10,2 +9,3 @@ formatError?: (error: JsonError) => string;

};
export declare const handleRefresh: (vu: ViewUpdate) => boolean;
/**

@@ -15,7 +15,7 @@ * Helper for simpler class instantiaton

*/
export declare function jsonSchemaLinter(schema: JSONSchema7, options?: JSONValidationOptions): (view: EditorView) => Diagnostic[];
export declare function jsonSchemaLinter(options?: JSONValidationOptions): (view: EditorView) => Diagnostic[];
export declare class JSONValidation {
private schema;
private options;
constructor(schema: JSONSchema7, options?: JSONValidationOptions);
constructor(options?: JSONValidationOptions);
private get schemaTitle();

@@ -22,0 +22,0 @@ private rewriteError;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.JSONValidation = exports.jsonSchemaLinter = void 0;
exports.JSONValidation = exports.jsonSchemaLinter = exports.handleRefresh = void 0;
const json_schema_library_1 = require("json-schema-library");
const formatting_1 = require("./utils/formatting");
const parseJSONDocument_1 = require("./utils/parseJSONDocument");
const formatting_js_1 = require("./utils/formatting.js");
const parseJSONDocument_js_1 = require("./utils/parseJSONDocument.js");
const state_js_1 = require("./state.js");
// return an object path that matches with the json-source-map pointer

@@ -21,2 +22,6 @@ const getErrorPath = (error) => {

};
const handleRefresh = (vu) => {
return (vu.startState.field(state_js_1.schemaStateField) !== vu.state.field(state_js_1.schemaStateField));
};
exports.handleRefresh = handleRefresh;
/**

@@ -26,5 +31,5 @@ * Helper for simpler class instantiaton

*/
function jsonSchemaLinter(schema, options) {
const validation = new JSONValidation(schema, options);
return function jsonDoValidation(view) {
function jsonSchemaLinter(options) {
const validation = new JSONValidation(options);
return (view) => {
return validation.doValidation(view);

@@ -35,3 +40,4 @@ };

class JSONValidation {
constructor(schema, options) {
constructor(options) {
this.schema = null;
// rewrite the error message to be more human readable

@@ -41,7 +47,7 @@ this.rewriteError = (error) => {

if (error.code === "one-of-error") {
return `Expected one of ${(0, formatting_1.joinWithOr)((_a = error === null || error === void 0 ? void 0 : error.data) === null || _a === void 0 ? void 0 : _a.errors, (data) => data.data.expected)}`;
return `Expected one of ${(0, formatting_js_1.joinWithOr)((_a = error === null || error === void 0 ? void 0 : error.data) === null || _a === void 0 ? void 0 : _a.errors, (data) => data.data.expected)}`;
}
if (error.code === "type-error") {
return `Expected \`${((_b = error === null || error === void 0 ? void 0 : error.data) === null || _b === void 0 ? void 0 : _b.expected) && Array.isArray((_c = error === null || error === void 0 ? void 0 : error.data) === null || _c === void 0 ? void 0 : _c.expected)
? (0, formatting_1.joinWithOr)((_d = error === null || error === void 0 ? void 0 : error.data) === null || _d === void 0 ? void 0 : _d.expected)
? (0, formatting_js_1.joinWithOr)((_d = error === null || error === void 0 ? void 0 : error.data) === null || _d === void 0 ? void 0 : _d.expected)
: (_e = error === null || error === void 0 ? void 0 : error.data) === null || _e === void 0 ? void 0 : _e.expected}\` but received \`${(_f = error === null || error === void 0 ? void 0 : error.data) === null || _f === void 0 ? void 0 : _f.received}\``;

@@ -52,3 +58,3 @@ }

};
this.options = Object.assign({ jsonParser: parseJSONDocument_1.parseJSONDocumentState }, options);
this.options = Object.assign({ jsonParser: parseJSONDocument_js_1.parseJSONDocumentState }, options);
// TODO: support other versions of json schema.

@@ -59,3 +65,2 @@ // most standard schemas are draft 4 for some reason, probably

// ajv did not support draft 4, so I used json-schema-library
this.schema = new json_schema_library_1.Draft04(schema);
}

@@ -68,2 +73,7 @@ get schemaTitle() {

doValidation(view) {
const schema = (0, state_js_1.getJSONSchema)(view.state);
if (!schema) {
return [];
}
this.schema = new json_schema_library_1.Draft04(schema);
if (!this.schema)

@@ -89,6 +99,7 @@ return [];

// if the error is a property error, use the key position
const isPropertyError = error.name === "NoAdditionalPropertiesError";
const isKeyError = error.name === "NoAdditionalPropertiesError" ||
error.name === "RequiredPropertyError";
acc.push({
from: isPropertyError ? pointer.keyFrom : pointer.valueFrom,
to: isPropertyError ? pointer.keyTo : pointer.valueTo,
from: isKeyError ? pointer.keyFrom : pointer.valueFrom,
to: isKeyError ? pointer.keyTo : pointer.valueTo,
// TODO: create a domnode and replace `` with <code></code>

@@ -101,2 +112,11 @@ // renderMessage: () => error.message,

}
else {
acc.push({
from: 0,
to: 0,
message: this.rewriteError(error),
severity: "error",
source: this.schemaTitle,
});
}
return acc;

@@ -103,0 +123,0 @@ }, []);

@@ -6,2 +6,2 @@ import { JSONSchema7 } from "json-schema";

*/
export declare function json5Schema(schema: JSONSchema7): import("@codemirror/state").Extension[];
export declare function json5Schema(schema?: JSONSchema7): import("@codemirror/state").Extension[];

@@ -6,6 +6,8 @@ "use strict";

const view_1 = require("@codemirror/view");
const json_completion_1 = require("./json-completion");
const json5_validation_1 = require("./json5-validation");
const json5_hover_1 = require("./json5-hover");
const json_completion_js_1 = require("./json-completion.js");
const json5_validation_js_1 = require("./json5-validation.js");
const json5_hover_js_1 = require("./json5-hover.js");
const lint_1 = require("@codemirror/lint");
const state_js_1 = require("./state.js");
const json_validation_js_1 = require("./json-validation.js");
/**

@@ -19,9 +21,12 @@ * Full featured cm6 extension for json5, including `codemirror-json5`

(0, lint_1.linter)((0, codemirror_json5_1.json5ParseLinter)()),
(0, lint_1.linter)((0, json5_validation_1.json5SchemaLinter)(schema)),
(0, lint_1.linter)((0, json5_validation_js_1.json5SchemaLinter)(), {
needsRefresh: json_validation_js_1.handleRefresh,
}),
codemirror_json5_1.json5Language.data.of({
autocomplete: (0, json_completion_1.json5Completion)(schema),
autocomplete: (0, json_completion_js_1.json5Completion)(),
}),
(0, view_1.hoverTooltip)((0, json5_hover_1.json5SchemaHover)(schema)),
(0, view_1.hoverTooltip)((0, json5_hover_js_1.json5SchemaHover)()),
(0, state_js_1.stateExtensions)(schema),
];
}
exports.json5Schema = json5Schema;
import { type EditorView } from "@codemirror/view";
import { type HoverOptions } from "./json-hover";
import { type JSONSchema7 } from "json-schema";
import { Side } from "./types";
import { type HoverOptions } from "./json-hover.js";
import { Side } from "./types.js";
export type JSON5HoverOptions = Exclude<HoverOptions, "mode">;

@@ -10,2 +9,2 @@ /**

*/
export declare function json5SchemaHover(schema: JSONSchema7, options?: JSON5HoverOptions): (view: EditorView, pos: number, side: Side) => Promise<import("@codemirror/view").Tooltip | null>;
export declare function json5SchemaHover(options?: JSON5HoverOptions): (view: EditorView, pos: number, side: Side) => Promise<import("@codemirror/view").Tooltip | null>;

@@ -7,3 +7,3 @@ "use strict";

exports.json5SchemaHover = void 0;
const json_hover_1 = require("./json-hover");
const json_hover_js_1 = require("./json-hover.js");
const json5_1 = __importDefault(require("json5"));

@@ -14,4 +14,4 @@ /**

*/
function json5SchemaHover(schema, options) {
const hover = new json_hover_1.JSONHover(schema, Object.assign(Object.assign({}, options), { parser: json5_1.default.parse }));
function json5SchemaHover(options) {
const hover = new json_hover_js_1.JSONHover(Object.assign(Object.assign({}, options), { parser: json5_1.default.parse }));
return async function jsonDoHover(view, pos, side) {

@@ -18,0 +18,0 @@ return hover.doHover(view, pos, side);

import { EditorView } from "@codemirror/view";
import { type JSONValidationOptions } from "./json-validation";
import type { JSONSchema7 } from "json-schema";
import { type JSONValidationOptions } from "./json-validation.js";
/**

@@ -8,2 +7,2 @@ * Instantiates a JSONValidation instance with the JSON5 mode

*/
export declare function json5SchemaLinter(schema: JSONSchema7, options?: JSONValidationOptions): (view: EditorView) => import("@codemirror/lint").Diagnostic[];
export declare function json5SchemaLinter(options?: JSONValidationOptions): (view: EditorView) => import("@codemirror/lint").Diagnostic[];
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.json5SchemaLinter = void 0;
const json_validation_1 = require("./json-validation");
const parseJSON5Document_1 = require("./utils/parseJSON5Document");
const json_validation_js_1 = require("./json-validation.js");
const parseJSON5Document_js_1 = require("./utils/parseJSON5Document.js");
/**

@@ -10,5 +10,5 @@ * Instantiates a JSONValidation instance with the JSON5 mode

*/
function json5SchemaLinter(schema, options) {
const validation = new json_validation_1.JSONValidation(schema, Object.assign({ jsonParser: parseJSON5Document_1.parseJSON5DocumentState }, options));
return function json5DoLint(view) {
function json5SchemaLinter(options) {
const validation = new json_validation_js_1.JSONValidation(Object.assign({ jsonParser: parseJSON5Document_js_1.parseJSON5DocumentState }, options));
return (view) => {
return validation.doValidation(view);

@@ -15,0 +15,0 @@ };

@@ -1,8 +0,8 @@

export { json5SchemaLinter } from "./json5-validation";
export { json5SchemaHover } from "./json5-hover";
export { json5Completion } from "./json-completion";
export { json5SchemaLinter } from "./json5-validation.js";
export { json5SchemaHover } from "./json5-hover.js";
export { json5Completion } from "./json-completion.js";
/**
* @group Bundled Codemirror Extensions
*/
export { json5Schema } from "./json5-bundled";
export * from "./utils/parseJSON5Document";
export { json5Schema } from "./json5-bundled.js";
export * from "./utils/parseJSON5Document.js";

@@ -19,13 +19,13 @@ "use strict";

// json5
var json5_validation_1 = require("./json5-validation");
Object.defineProperty(exports, "json5SchemaLinter", { enumerable: true, get: function () { return json5_validation_1.json5SchemaLinter; } });
var json5_hover_1 = require("./json5-hover");
Object.defineProperty(exports, "json5SchemaHover", { enumerable: true, get: function () { return json5_hover_1.json5SchemaHover; } });
var json_completion_1 = require("./json-completion");
Object.defineProperty(exports, "json5Completion", { enumerable: true, get: function () { return json_completion_1.json5Completion; } });
var json5_validation_js_1 = require("./json5-validation.js");
Object.defineProperty(exports, "json5SchemaLinter", { enumerable: true, get: function () { return json5_validation_js_1.json5SchemaLinter; } });
var json5_hover_js_1 = require("./json5-hover.js");
Object.defineProperty(exports, "json5SchemaHover", { enumerable: true, get: function () { return json5_hover_js_1.json5SchemaHover; } });
var json_completion_js_1 = require("./json-completion.js");
Object.defineProperty(exports, "json5Completion", { enumerable: true, get: function () { return json_completion_js_1.json5Completion; } });
/**
* @group Bundled Codemirror Extensions
*/
var json5_bundled_1 = require("./json5-bundled");
Object.defineProperty(exports, "json5Schema", { enumerable: true, get: function () { return json5_bundled_1.json5Schema; } });
__exportStar(require("./utils/parseJSON5Document"), exports);
var json5_bundled_js_1 = require("./json5-bundled.js");
Object.defineProperty(exports, "json5Schema", { enumerable: true, get: function () { return json5_bundled_js_1.json5Schema; } });
__exportStar(require("./utils/parseJSON5Document.js"), exports);
import { EditorState, Text } from "@codemirror/state";
import { SyntaxNode } from "@lezer/common";
import { JSONPointersMap, Side } from "../types";
import { JSONPointersMap, Side } from "../types.js";
export type JSONMode = "json4" | "json5";

@@ -5,0 +5,0 @@ export declare function getJsonPointerAt(docText: Text, node: SyntaxNode): string;

@@ -5,4 +5,4 @@ "use strict";

const language_1 = require("@codemirror/language");
const constants_1 = require("../constants");
const node_1 = require("./node");
const constants_js_1 = require("../constants.js");
const node_js_1 = require("./node.js");
// adapted from https://discuss.codemirror.net/t/json-pointer-at-cursor-seeking-implementation-critique/4793/3

@@ -14,12 +14,12 @@ // this could be useful for other things later!

switch (n.parent.name) {
case constants_1.TOKENS.PROPERTY: {
const name = n.parent.getChild(constants_1.TOKENS.PROPERTY_NAME);
case constants_js_1.TOKENS.PROPERTY: {
const name = n.parent.getChild(constants_js_1.TOKENS.PROPERTY_NAME);
if (name) {
path.unshift((0, node_1.getWord)(docText, name).replace(/[/~]/g, (v) => v === "~" ? "~0" : "~1"));
path.unshift((0, node_js_1.getWord)(docText, name).replace(/[/~]/g, (v) => v === "~" ? "~0" : "~1"));
}
break;
}
case constants_1.TOKENS.ARRAY: {
if ((0, node_1.isValueNode)(n)) {
const index = (0, node_1.findNodeIndexInArrayNode)(n.parent, n);
case constants_js_1.TOKENS.ARRAY: {
if ((0, node_js_1.isValueNode)(n)) {
const index = (0, node_js_1.findNodeIndexInArrayNode)(n.parent, n);
path.unshift(`${index}`);

@@ -53,3 +53,3 @@ }

var _a, _b, _c, _d, _e, _f, _g, _h;
if (type.name === "PropertyName") {
if (type.name === "PropertyName" || type.name === "Object") {
const pointer = getJsonPointerAt(state.doc, type.node);

@@ -56,0 +56,0 @@ const { from: keyFrom, to: keyTo } = type.node;

import { SyntaxNode } from "@lezer/common";
import { EditorState, Text } from "@codemirror/state";
import { Side } from "../types";
import { Side } from "../types.js";
export declare const getNodeAtPosition: (state: EditorState, pos: number, side?: Side) => SyntaxNode;

@@ -5,0 +5,0 @@ export declare const stripSurroundingQuotes: (str: string) => string;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.findNodeIndexInArrayNode = exports.getChildValueNode = exports.isPropertyNameNode = exports.isValueNode = exports.isPrimitiveValueNode = exports.isInvalidValueNode = exports.getWord = exports.stripSurroundingQuotes = exports.getNodeAtPosition = void 0;
const constants_1 = require("../constants");
const constants_js_1 = require("../constants.js");
const language_1 = require("@codemirror/language");

@@ -21,13 +21,13 @@ const getNodeAtPosition = (state, pos, side = -1) => {

var _a, _b;
return (node.name === constants_1.TOKENS.INVALID &&
(((_a = node.prevSibling) === null || _a === void 0 ? void 0 : _a.name) === constants_1.TOKENS.PROPERTY_NAME ||
((_b = node.prevSibling) === null || _b === void 0 ? void 0 : _b.name) === constants_1.TOKENS.PROPERTY_COLON));
return (node.name === constants_js_1.TOKENS.INVALID &&
(((_a = node.prevSibling) === null || _a === void 0 ? void 0 : _a.name) === constants_js_1.TOKENS.PROPERTY_NAME ||
((_b = node.prevSibling) === null || _b === void 0 ? void 0 : _b.name) === constants_js_1.TOKENS.PROPERTY_COLON));
};
exports.isInvalidValueNode = isInvalidValueNode;
const isPrimitiveValueNode = (node) => {
return constants_1.PRIMITIVE_TYPES.includes(node.name) || (0, exports.isInvalidValueNode)(node);
return constants_js_1.PRIMITIVE_TYPES.includes(node.name) || (0, exports.isInvalidValueNode)(node);
};
exports.isPrimitiveValueNode = isPrimitiveValueNode;
const isValueNode = (node) => {
return ([...constants_1.PRIMITIVE_TYPES, ...constants_1.COMPLEX_TYPES].includes(node.name) ||
return ([...constants_js_1.PRIMITIVE_TYPES, ...constants_js_1.COMPLEX_TYPES].includes(node.name) ||
(0, exports.isInvalidValueNode)(node));

@@ -38,5 +38,5 @@ };

var _a, _b;
return (node.name === constants_1.TOKENS.PROPERTY_NAME ||
(node.name === constants_1.TOKENS.INVALID &&
(((_a = node.prevSibling) === null || _a === void 0 ? void 0 : _a.name) === constants_1.TOKENS.PROPERTY ||
return (node.name === constants_js_1.TOKENS.PROPERTY_NAME ||
(node.name === constants_js_1.TOKENS.INVALID &&
(((_a = node.prevSibling) === null || _a === void 0 ? void 0 : _a.name) === constants_js_1.TOKENS.PROPERTY ||
((_b = node.prevSibling) === null || _b === void 0 ? void 0 : _b.name) === "{")));

@@ -61,3 +61,3 @@ };

const getArrayNodeChildren = (node) => {
return getChildrenNodes(node).filter((n) => constants_1.PRIMITIVE_TYPES.includes(n.name) || constants_1.COMPLEX_TYPES.includes(n.name));
return getChildrenNodes(node).filter((n) => constants_js_1.PRIMITIVE_TYPES.includes(n.name) || constants_js_1.COMPLEX_TYPES.includes(n.name));
};

@@ -64,0 +64,0 @@ const findNodeIndexInArrayNode = (arrayNode, valueNode) => {

@@ -11,3 +11,3 @@ /**

data: any;
pointers: import("..").JSONPointersMap;
pointers: import("../types.js").JSONPointersMap;
};

@@ -20,3 +20,3 @@ /**

data: any;
pointers: import("..").JSONPointersMap;
pointers: import("../types.js").JSONPointersMap;
};

@@ -13,3 +13,3 @@ "use strict";

const state_1 = require("@codemirror/state");
const jsonPointers_1 = require("./jsonPointers");
const jsonPointers_js_1 = require("./jsonPointers.js");
/**

@@ -26,3 +26,3 @@ * Return parsed data and json5 pointers for a given codemirror EditorState

catch (_a) { }
const pointers = (0, jsonPointers_1.getJsonPointers)(state, "json5");
const pointers = (0, jsonPointers_js_1.getJsonPointers)(state, "json5");
return { data, pointers };

@@ -29,0 +29,0 @@ }

@@ -8,3 +8,3 @@ import { EditorState } from "@codemirror/state";

data: any;
pointers: import("..").JSONPointersMap;
pointers: import("../types.js").JSONPointersMap;
};

@@ -17,3 +17,3 @@ /**

data: any;
pointers: import("..").JSONPointersMap;
pointers: import("../types.js").JSONPointersMap;
};

@@ -6,3 +6,3 @@ "use strict";

const state_1 = require("@codemirror/state");
const jsonPointers_1 = require("./jsonPointers");
const jsonPointers_js_1 = require("./jsonPointers.js");
/**

@@ -19,3 +19,3 @@ * Return parsed data and json pointers for a given codemirror EditorState

catch (_a) { }
const pointers = (0, jsonPointers_1.getJsonPointers)(state, "json4");
const pointers = (0, jsonPointers_js_1.getJsonPointers)(state, "json4");
return { data, pointers };

@@ -22,0 +22,0 @@ }

@@ -8,3 +8,3 @@ "use strict";

const json_schema_library_1 = require("json-schema-library");
const step_1 = __importDefault(require("./step"));
const step_js_1 = __importDefault(require("./step.js"));
const emptyObject = {};

@@ -36,3 +36,3 @@ /**

// @ts-expect-error
schema = (0, step_1.default)(draft, key, schema, data, pointer); // step schema
schema = (0, step_js_1.default)(draft, key, schema, data, pointer); // step schema
if ((0, json_schema_library_1.isJsonError)(schema)) {

@@ -39,0 +39,0 @@ return schema;

@@ -8,5 +8,5 @@ "use strict";

// @ts-expect-error
const errors_1 = __importDefault(require("json-schema-library/dist/module/lib/validation/errors"));
const errors_js_1 = __importDefault(require("json-schema-library/dist/module/lib/validation/errors.js"));
// @ts-expect-error
const createSchemaOf_1 = __importDefault(require("json-schema-library/dist/module/lib/createSchemaOf"));
const createSchemaOf_js_1 = __importDefault(require("json-schema-library/dist/module/lib/createSchemaOf.js"));
const stepType = {

@@ -24,7 +24,7 @@ array: (draft, key, schema, data, pointer) => {

if (schema.items[key] === true) {
return (0, createSchemaOf_1.default)(itemValue);
return (0, createSchemaOf_js_1.default)(itemValue);
}
// @draft >= 7 bool schema, items:[true, false]
if (schema.items[key] === false) {
return errors_1.default.invalidDataError({
return errors_js_1.default.invalidDataError({
key,

@@ -39,3 +39,3 @@ value: itemValue,

if (schema.additionalItems === false) {
return errors_1.default.additionalItemsError({
return errors_js_1.default.additionalItemsError({
key,

@@ -48,3 +48,3 @@ value: itemValue,

schema.additionalItems === undefined) {
return (0, createSchemaOf_1.default)(itemValue);
return (0, createSchemaOf_js_1.default)(itemValue);
}

@@ -59,3 +59,3 @@ if ((0, json_schema_library_1.getTypeOf)(schema.additionalItems) === "object") {

// @todo support additionalItems: {schema}
return (0, createSchemaOf_1.default)(itemValue);
return (0, createSchemaOf_js_1.default)(itemValue);
}

@@ -73,3 +73,3 @@ return new Error(`Invalid array schema for ${key} at ${pointer}`);

if (property === false) {
return errors_1.default.forbiddenPropertyError({
return errors_js_1.default.forbiddenPropertyError({
property: key,

@@ -81,3 +81,3 @@ value: data,

else if (property === true) {
return (0, createSchemaOf_1.default)(data === null || data === void 0 ? void 0 : data[key]);
return (0, createSchemaOf_js_1.default)(data === null || data === void 0 ? void 0 : data[key]);
}

@@ -122,5 +122,5 @@ const targetSchema = draft.resolveRef(property);

(additionalProperties === undefined || additionalProperties === true)) {
return (0, createSchemaOf_1.default)(data[key]);
return (0, createSchemaOf_js_1.default)(data[key]);
}
return errors_1.default.unknownPropertyError({
return errors_js_1.default.unknownPropertyError({
property: key,

@@ -127,0 +127,0 @@ value: data,

import { CompletionContext, CompletionResult } from "@codemirror/autocomplete";
import { JSONSchema7 } from "json-schema";
import { JsonError } from "json-schema-library";
type JSONCompletionOptions = {

@@ -24,2 +26,3 @@ mode?: "json" | "json5";

private getSchemas;
isJsonError(d: JSONSchema7 | JsonError): d is JsonError;
private expandSchemaProperty;

@@ -26,0 +29,0 @@ private getReferenceSchema;

@@ -5,6 +5,7 @@ import { snippetCompletion, } from "@codemirror/autocomplete";

import { findNodeIndexInArrayNode, getChildValueNode, getWord, isPropertyNameNode, isPrimitiveValueNode, stripSurroundingQuotes, getNodeAtPosition, } from "./utils/node.js";
import { getJSONSchema } from "./state.js";
import { Draft07, isJsonError } from "json-schema-library";
import { Draft07 } from "json-schema-library";
import { jsonPointerForPosition } from "./utils/jsonPointers.js";
import { TOKENS } from "./constants.js";
import { getJSONSchema } from "./state.js";
import getSchema from "./utils/schema-lib/getSchema.js";
function json5PropertyInsertSnippet(rawWord, value) {

@@ -516,9 +517,5 @@ if (rawWord.startsWith('"')) {

getSchemas(schema, ctx) {
var _a;
const draft = new Draft07(this.schema);
let pointer = jsonPointerForPosition(ctx.state, ctx.pos);
let subSchema = draft.getSchema({ pointer });
if (isJsonError(subSchema)) {
subSchema = (_a = subSchema.data) === null || _a === void 0 ? void 0 : _a.schema;
}
let subSchema = getSchema(draft, pointer);
// if we don't have a schema for the current pointer, try the parent pointer

@@ -530,3 +527,3 @@ if (!subSchema ||

pointer = pointer.replace(/\/[^/]*$/, "/");
subSchema = draft.getSchema({ pointer });
subSchema = getSchema(draft, pointer);
}

@@ -540,3 +537,3 @@ debug.log("xxx", "pointer..", JSON.stringify(pointer));

debug.log("xxx", "subSchema..", subSchema);
if (!subSchema) {
if (this.isJsonError(subSchema)) {
return [];

@@ -564,2 +561,5 @@ }

}
isJsonError(d) {
return d.type === "error";
}
expandSchemaProperty(property, schema) {

@@ -566,0 +566,0 @@ if (typeof property === "object" && property.$ref) {

@@ -1,4 +0,5 @@

import { Draft04, isJsonError, } from "json-schema-library";
import { Draft04 } from "json-schema-library";
import { jsonPointerForPosition } from "./utils/jsonPointers.js";
import { joinWithOr } from "./utils/formatting.js";
import getSchema from "./utils/schema-lib/getSchema.js";
import { debug } from "./utils/debug.js";

@@ -48,14 +49,9 @@ import { el } from "./utils/dom.js";

// if the data is valid, we can infer a type for complex types
let subSchema = this.schema.getSchema({
pointer,
data,
withSchemaWarning: true,
});
if (isJsonError(subSchema)) {
if (subSchema === null || subSchema === void 0 ? void 0 : subSchema.data.schema["$ref"]) {
subSchema = this.schema.resolveRef(subSchema);
let subSchema = getSchema(this.schema, pointer, data);
if (subSchema.type === "error" && data !== undefined) {
// if the data is invalid, we won't get the type - try again without the data
subSchema = getSchema(this.schema, pointer, undefined);
if (subSchema.type === "error") {
return { pointer };
}
else {
subSchema = subSchema === null || subSchema === void 0 ? void 0 : subSchema.data.schema;
}
}

@@ -73,11 +69,3 @@ return { schema: subSchema, pointer };

el("div", { class: "cm6-json-schema-hover--code-wrapper" }, [
typeInfo.includes("<code>")
? el("div", {
class: "cm6-json-schema-hover--code",
inner: typeInfo,
})
: el("code", {
class: "cm6-json-schema-hover--code",
text: typeInfo,
}),
el("code", { class: "cm6-json-schema-hover--code", text: typeInfo }),
]),

@@ -110,11 +98,2 @@ ]);

}
if (schema.enum) {
typeInfo = `<code>enum</code>: ${joinWithOr(schema.enum)}`;
}
if (schema.format) {
typeInfo += ` <code>format</code>: ${schema.format}`;
}
if (schema.pattern) {
typeInfo += ` <code>pattern</code>: ${schema.pattern}`;
}
if (schema.description) {

@@ -121,0 +100,0 @@ message = schema.description;

import { Draft04 } from "json-schema-library";
import { getJSONSchema, schemaStateField } from "./state.js";
import { joinWithOr } from "./utils/formatting.js";
import { parseJSONDocumentState } from "./utils/parseJSONDocument.js";
import { el } from "./utils/dom.js";
import { getJSONSchema, schemaStateField } from "./state.js";
// return an object path that matches with the json-source-map pointer

@@ -38,20 +37,12 @@ const getErrorPath = (error) => {

this.rewriteError = (error) => {
var _a, _b, _c, _d, _e;
const errorData = error === null || error === void 0 ? void 0 : error.data;
const errors = errorData === null || errorData === void 0 ? void 0 : errorData.errors;
if (error.code === "one-of-error" && (errors === null || errors === void 0 ? void 0 : errors.length)) {
return `Expected one of ${joinWithOr(errors, (data) => data.data.expected)}`;
var _a, _b, _c, _d, _e, _f;
if (error.code === "one-of-error") {
return `Expected one of ${joinWithOr((_a = error === null || error === void 0 ? void 0 : error.data) === null || _a === void 0 ? void 0 : _a.errors, (data) => data.data.expected)}`;
}
if (error.code === "type-error") {
return `Expected <code>${((_a = error === null || error === void 0 ? void 0 : error.data) === null || _a === void 0 ? void 0 : _a.expected) && Array.isArray((_b = error === null || error === void 0 ? void 0 : error.data) === null || _b === void 0 ? void 0 : _b.expected)
? joinWithOr((_c = error === null || error === void 0 ? void 0 : error.data) === null || _c === void 0 ? void 0 : _c.expected)
: (_d = error === null || error === void 0 ? void 0 : error.data) === null || _d === void 0 ? void 0 : _d.expected}</code> but received <code>${(_e = error === null || error === void 0 ? void 0 : error.data) === null || _e === void 0 ? void 0 : _e.received}</code>`;
return `Expected \`${((_b = error === null || error === void 0 ? void 0 : error.data) === null || _b === void 0 ? void 0 : _b.expected) && Array.isArray((_c = error === null || error === void 0 ? void 0 : error.data) === null || _c === void 0 ? void 0 : _c.expected)
? joinWithOr((_d = error === null || error === void 0 ? void 0 : error.data) === null || _d === void 0 ? void 0 : _d.expected)
: (_e = error === null || error === void 0 ? void 0 : error.data) === null || _e === void 0 ? void 0 : _e.expected}\` but received \`${(_f = error === null || error === void 0 ? void 0 : error.data) === null || _f === void 0 ? void 0 : _f.received}\``;
}
const message = error.message
// don't mention root object
.replaceAll("in `#` ", "")
.replaceAll("/", ".")
.replaceAll("#.", "")
// replace backticks with <code> tags
.replaceAll(/`([^`]*)`/gm, "<code>$1</code>");
const message = error.message.replaceAll("#/", "").replaceAll("/", ".");
return message;

@@ -67,4 +58,4 @@ };

get schemaTitle() {
var _a, _b, _c;
return (_c = (_b = (_a = this.schema) === null || _a === void 0 ? void 0 : _a.getSchema()) === null || _b === void 0 ? void 0 : _b.title) !== null && _c !== void 0 ? _c : "json-schema";
var _a;
return (_a = this.schema.getSchema().title) !== null && _a !== void 0 ? _a : "json-schema";
}

@@ -98,13 +89,10 @@ // validate using view as the linter extension signature requires

// if the error is a property error, use the key position
const isPropertyError = error.name === "NoAdditionalPropertiesError";
const errorString = this.rewriteError(error);
const isKeyError = error.name === "NoAdditionalPropertiesError" ||
error.name === "RequiredPropertyError";
acc.push({
from: isPropertyError ? pointer.keyFrom : pointer.valueFrom,
to: isPropertyError ? pointer.keyTo : pointer.valueTo,
message: errorString,
renderMessage: () => {
const dom = el("div", {});
dom.innerHTML = errorString;
return dom;
},
from: isKeyError ? pointer.keyFrom : pointer.valueFrom,
to: isKeyError ? pointer.keyTo : pointer.valueTo,
// TODO: create a domnode and replace `` with <code></code>
// renderMessage: () => error.message,
message: this.rewriteError(error),
severity: "error",

@@ -114,2 +102,11 @@ source: this.schemaTitle,

}
else {
acc.push({
from: 0,
to: 0,
message: this.rewriteError(error),
severity: "error",
source: this.schemaTitle,
});
}
return acc;

@@ -116,0 +113,0 @@ }, []);

@@ -6,2 +6,2 @@ import { JSONSchema7 } from "json-schema";

*/
export declare function json5Schema(schema?: JSONSchema7): any[];
export declare function json5Schema(schema?: JSONSchema7): import("@codemirror/state").Extension[];
export const debug = {
log: (...args) => {
if (process.env.NODE_ENV !== "development") {
if ("production" !== "development") {
return;

@@ -5,0 +5,0 @@ }

@@ -1,3 +0,3 @@

type Attributes = "class" | "text" | "id" | "role" | "aria-label" | "inner";
type Attributes = "class" | "text" | "id" | "role" | "aria-label";
export declare function el(tagName: string, attributes: Partial<Record<Attributes, string>>, children?: HTMLElement[]): HTMLElement;
export {};

@@ -15,6 +15,2 @@ // export function el(tagName: string, className?: string) {

}
if (k === "inner") {
e.innerHTML = v;
return;
}
e.setAttribute(k, v);

@@ -21,0 +17,0 @@ });

@@ -6,3 +6,3 @@ // a little english-centric utility

let data = arr.map((err, i) => {
const result = `<code>` + (getPath ? getPath(err) : err) + `</code>`;
const result = `\`` + (getPath ? JSON.stringify(getPath(err)) : err) + `\``;
if (i === arr.length - 1)

@@ -9,0 +9,0 @@ return "or " + result;

@@ -46,3 +46,3 @@ import { syntaxTree } from "@codemirror/language";

var _a, _b, _c, _d, _e, _f, _g, _h;
if (type.name === "PropertyName") {
if (type.name === "PropertyName" || type.name === "Object") {
const pointer = getJsonPointerAt(state.doc, type.node);

@@ -49,0 +49,0 @@ const { from: keyFrom, to: keyTo } = type.node;

import { SyntaxNode } from "@lezer/common";
import { EditorState, Text } from "@codemirror/state";
import { Side } from "../types.js";
export declare const getNodeAtPosition: (state: EditorState, pos: number, side?: Side) => any;
export declare const getNodeAtPosition: (state: EditorState, pos: number, side?: Side) => SyntaxNode;
export declare const stripSurroundingQuotes: (str: string) => string;

@@ -11,3 +11,3 @@ export declare const getWord: (doc: Text, node: SyntaxNode | null, stripQuotes?: boolean) => string;

export declare const isPropertyNameNode: (node: SyntaxNode) => boolean;
export declare const getChildValueNode: (node: SyntaxNode) => any;
export declare const getChildValueNode: (node: SyntaxNode) => SyntaxNode | undefined;
export declare const findNodeIndexInArrayNode: (arrayNode: SyntaxNode, valueNode: SyntaxNode) => number;

@@ -66,3 +66,3 @@ [codemirror-json-schema](../README.md) / index

[bundled.ts:15](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/bundled.ts#L15)
[bundled.ts:15](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/bundled.ts#L15)

@@ -101,3 +101,3 @@ ## Codemirror Extensions

[json-completion.ts:811](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/json-completion.ts#L811)
[json-completion.ts:811](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/json-completion.ts#L811)

@@ -138,3 +138,3 @@ ---

[json-hover.ts:39](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/json-hover.ts#L39)
[json-hover.ts:39](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/json-hover.ts#L39)

@@ -173,3 +173,3 @@ ---

[json-validation.ts:42](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/json-validation.ts#L42)
[json-validation.ts:42](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/json-validation.ts#L42)

@@ -197,3 +197,3 @@ ## Utilities

[utils/jsonPointers.ts:56](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/utils/jsonPointers.ts#L56)
[utils/jsonPointers.ts:57](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/utils/jsonPointers.ts#L57)

@@ -222,3 +222,3 @@ ---

[utils/jsonPointers.ts:44](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/utils/jsonPointers.ts#L44)
[utils/jsonPointers.ts:45](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/utils/jsonPointers.ts#L45)

@@ -250,3 +250,3 @@ ---

[utils/parseJSONDocument.ts:23](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/utils/parseJSONDocument.ts#L23)
[utils/parseJSONDocument.ts:23](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/utils/parseJSONDocument.ts#L23)

@@ -278,3 +278,3 @@ ---

[utils/parseJSONDocument.ts:9](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/utils/parseJSONDocument.ts#L9)
[utils/parseJSONDocument.ts:9](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/utils/parseJSONDocument.ts#L9)

@@ -299,3 +299,3 @@ ## Functions

[state.ts:25](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/state.ts#L25)
[state.ts:25](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/state.ts#L25)

@@ -321,3 +321,3 @@ ---

[utils/jsonPointers.ts:12](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/utils/jsonPointers.ts#L12)
[utils/jsonPointers.ts:12](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/utils/jsonPointers.ts#L12)

@@ -342,3 +342,3 @@ ---

[state.ts:29](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/state.ts#L29)
[state.ts:29](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/state.ts#L29)

@@ -364,3 +364,3 @@ ---

[state.ts:19](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/state.ts#L19)
[state.ts:19](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/state.ts#L19)

@@ -382,3 +382,3 @@ ## Type Aliases

[json-hover.ts:13](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/json-hover.ts#L13)
[json-hover.ts:13](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/json-hover.ts#L13)

@@ -393,3 +393,3 @@ ---

[json-hover.ts:15](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/json-hover.ts#L15)
[json-hover.ts:15](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/json-hover.ts#L15)

@@ -412,3 +412,3 @@ ---

[json-hover.ts:19](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/json-hover.ts#L19)
[json-hover.ts:19](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/json-hover.ts#L19)

@@ -423,3 +423,3 @@ ---

[utils/jsonPointers.ts:8](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/utils/jsonPointers.ts#L8)
[utils/jsonPointers.ts:8](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/utils/jsonPointers.ts#L8)

@@ -441,3 +441,3 @@ ---

[types.ts:4](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/types.ts#L4)
[types.ts:4](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/types.ts#L4)

@@ -461,3 +461,3 @@ ---

[types.ts:9](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/types.ts#L9)
[types.ts:9](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/types.ts#L9)

@@ -472,3 +472,3 @@ ---

[types.ts:18](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/types.ts#L18)
[types.ts:18](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/types.ts#L18)

@@ -490,3 +490,3 @@ ---

[json-validation.ts:25](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/json-validation.ts#L25)
[json-validation.ts:25](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/json-validation.ts#L25)

@@ -501,2 +501,2 @@ ## Variables

[state.ts:6](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/state.ts#L6)
[state.ts:6](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/state.ts#L6)

@@ -42,3 +42,3 @@ [codemirror-json-schema](../README.md) / json5

[json5-bundled.ts:16](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/json5-bundled.ts#L16)
[json5-bundled.ts:16](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/json5-bundled.ts#L16)

@@ -77,3 +77,3 @@ ## Codemirror Extensions

[json-completion.ts:822](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/json-completion.ts#L822)
[json-completion.ts:822](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/json-completion.ts#L822)

@@ -114,3 +114,3 @@ ---

[json5-hover.ts:13](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/json5-hover.ts#L13)
[json5-hover.ts:13](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/json5-hover.ts#L13)

@@ -149,3 +149,3 @@ ---

[json5-validation.ts:12](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/json5-validation.ts#L12)
[json5-validation.ts:12](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/json5-validation.ts#L12)

@@ -177,3 +177,3 @@ ## Utilities

[utils/parseJSON5Document.ts:28](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/utils/parseJSON5Document.ts#L28)
[utils/parseJSON5Document.ts:28](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/utils/parseJSON5Document.ts#L28)

@@ -205,2 +205,2 @@ ---

[utils/parseJSON5Document.ts:14](https://github.com/acao/codemirror-json-schema/blob/a73c517/src/utils/parseJSON5Document.ts#L14)
[utils/parseJSON5Document.ts:14](https://github.com/acao/codemirror-json-schema/blob/7ed9e3e/src/utils/parseJSON5Document.ts#L14)
{
"name": "codemirror-json-schema",
"license": "MIT",
"version": "0.5.1-canary",
"version": "0.5.1",
"description": "Codemirror 6 extensions that provide full JSONSchema support for `@codemirror/lang-json` and `codemirror-json5`",

@@ -59,3 +59,3 @@ "contributors": [

"json-schema": "^0.4.0",
"json-schema-library": "^9.1.2"
"json-schema-library": "^8.0.0"
},

@@ -110,2 +110,2 @@ "optionalDependencies": {

}
}
}

@@ -32,3 +32,3 @@ Codemirror 6 extensions that provide full [JSON Schema](https://json-schema.org/) support for `@codemirror/lang-json` & `codemirror-json5` language modes

- 0.5.0 - this breaking change _does not_ effect users using `jsonSchema()` or `json5Schema()` modes, but those using the "custom path".
- 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.

@@ -35,0 +35,0 @@ ### json4

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc