Skip to content

Commit 0aaf308

Browse files
authored
upgrade json schema lib, replace backticks with code blocks, no value context bug (#64)
1 parent 6e8b088 commit 0aaf308

15 files changed

+186
-337
lines changed

.changeset/nasty-islands-help.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"codemirror-json-schema": minor
3+
---
4+
5+
**Breaking Change**: replaces backticks with `<code>` blocks in hover and completion! This just seemed to make more sense.
6+
7+
- upgrade `json-schema-library` to the latest 8.x with patch fixes, remove "forked" pointer step logic
8+
- after autocompleting a property, when there is empty value, provide full autocomplete options
9+
- as noted in the breaking change notice, all psuedo-markdown backtick ``delimiters are replaced with`<code>`

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ dist
33
cjs
44
/public
55
coverage
6+
cjs

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
### Minor Changes
1212

13-
- [#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.
13+
- [#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)!
14+
15+
- **breaking change**: only impacts those following the "custom usage" approach, it _does not_ effect users using the high level, "bundled" `jsonSchema()` or `json5Schema()` modes.
1416

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

@@ -49,6 +51,8 @@
4951
];
5052
```
5153

54+
- 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.
55+
5256
## 0.4.5
5357

5458
### Patch Changes

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"@types/json-schema": "^7.0.12",
5757
"@types/node": "^20.4.2",
5858
"json-schema": "^0.4.0",
59-
"json-schema-library": "^8.0.0"
59+
"json-schema-library": "^9.1.2"
6060
},
6161
"optionalDependencies": {
6262
"@codemirror/lang-json": "^6.0.1",
@@ -73,17 +73,17 @@
7373
"devDependencies": {
7474
"@changesets/cli": "^2.26.2",
7575
"@codemirror/autocomplete": "^6.8.1",
76+
"@codemirror/basic-setup": "^0.20.0",
7677
"@codemirror/commands": "^6.2.4",
77-
"@codemirror/theme-one-dark": "^6.1.2",
78-
"@evilmartians/lefthook": "^1.4.6",
79-
"@vitest/coverage-v8": "^0.34.6",
80-
"codemirror": "^6.0.1",
8178
"@codemirror/language": "^6.8.0",
8279
"@codemirror/lint": "^6.4.0",
8380
"@codemirror/state": "^6.2.1",
81+
"@codemirror/theme-one-dark": "^6.1.2",
8482
"@codemirror/view": "^6.14.1",
85-
"@codemirror/basic-setup": "^0.20.0",
83+
"@evilmartians/lefthook": "^1.4.6",
8684
"@lezer/common": "^1.0.3",
85+
"@vitest/coverage-v8": "^0.34.6",
86+
"codemirror": "^6.0.1",
8787
"codemirror-json5": "^1.0.3",
8888
"happy-dom": "^10.3.2",
8989
"json5": "^2.2.3",

pnpm-lock.yaml

Lines changed: 51 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/json-hover.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ describe("JSONHover#getHoverTexts", () => {
7575
);
7676
expect(hoverTexts).toEqual({
7777
message: "an example oneOf",
78-
typeInfo: "oneOf: `string`, `array`, or `boolean`",
78+
typeInfo:
79+
"oneOf: <code>string</code>, <code>array</code>, or <code>boolean</code>",
7980
});
8081
});
8182
it("should provide oneOf texts with valid values", async () => {
@@ -86,7 +87,8 @@ describe("JSONHover#getHoverTexts", () => {
8687
);
8788
expect(hoverTexts).toEqual({
8889
message: "an example oneOf",
89-
typeInfo: "oneOf: `string`, `array`, or `boolean`",
90+
typeInfo:
91+
"oneOf: <code>string</code>, <code>array</code>, or <code>boolean</code>",
9092
});
9193
});
9294
});

src/__tests__/json-validation.spec.ts

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,52 @@ const getErrors = (jsonString: string, schema?: JSONSchema7) => {
1515
});
1616
return new JSONValidation().doValidation(view);
1717
};
18+
19+
const common = {
20+
severity: "error" as Diagnostic["severity"],
21+
source: "json-schema",
22+
};
23+
1824
const expectErrors = (
1925
jsonString: string,
2026
errors: [from: number | undefined, to: number | undefined, message: string][],
2127
schema?: JSONSchema7
2228
) => {
23-
expect(getErrors(jsonString, schema)).toEqual(
29+
const filteredErrors = getErrors(jsonString, schema).map(
30+
({ renderMessage, ...error }) => error
31+
);
32+
expect(filteredErrors).toEqual(
2433
errors.map(([from, to, message]) => ({ ...common, from, to, message }))
2534
);
2635
};
2736

28-
const common = {
29-
severity: "error" as Diagnostic["severity"],
30-
source: "json-schema",
31-
};
32-
3337
describe("json-validation", () => {
3438
it("should provide range for a value error", () => {
3539
expectErrors('{"foo": 123}', [
36-
[8, 11, "Expected `string` but received `number`"],
40+
[8, 11, "Expected <code>string</code> but received <code>number</code>"],
3741
]);
3842
});
3943
it("should provide range for an unknown key error", () => {
4044
expectErrors('{"foo": "example", "bar": 123}', [
41-
[19, 24, "Additional property `bar` in `#` is not allowed"],
45+
[19, 24, "Additional property <code>bar</code> is not allowed"],
4246
]);
4347
});
4448
it("should not handle invalid json", () => {
4549
expectErrors('{"foo": "example" "bar": 123}', [
46-
[undefined, undefined, "Expected `object` but received `null`"],
50+
[
51+
undefined,
52+
undefined,
53+
"Expected <code>object</code> but received <code>null</code>",
54+
],
4755
]);
4856
});
49-
it("should provide range for invalid multline json", () => {
57+
it("should provide range for invalid multiline json", () => {
5058
expectErrors(
5159
`{
5260
"foo": "example",
5361
"bar": "something else"
5462
}`,
55-
[[32, 37, "Additional property `bar` in `#` is not allowed"]]
63+
[[32, 37, "Additional property <code>bar</code> is not allowed"]]
5664
);
5765
});
5866
it("should provide formatted error message when required fields are missing", () => {
@@ -61,7 +69,13 @@ describe("json-validation", () => {
6169
"foo": "example",
6270
"object": {}
6371
}`,
64-
[[46, 48, "The required property `foo` is missing at `object`"]],
72+
[
73+
[
74+
46,
75+
48,
76+
"The required property <code>foo</code> is missing at <code>object</code>",
77+
],
78+
],
6579
testSchema2
6680
);
6781
});
@@ -72,7 +86,13 @@ describe("json-validation", () => {
7286
"object": { "foo": "true" },
7387
"oneOfEg": 123
7488
}`,
75-
[[80, 83, 'Expected one of `"string"`, `"array"`, or `"boolean"`']],
89+
[
90+
[
91+
80,
92+
83,
93+
"Expected one of <code>string</code>, <code>array</code>, or <code>boolean</code>",
94+
],
95+
],
7696
testSchema2
7797
);
7898
});
@@ -83,7 +103,7 @@ describe("json-validation", () => {
83103
"object": { "foo": "true" },
84104
"oneOfEg2": 123
85105
}`,
86-
[[81, 84, 'Expected one of `"string"` or `"array"`']],
106+
[[81, 84, "Expected one of <code>string</code> or <code>array</code>"]],
87107
testSchema2
88108
);
89109
});

src/json-completion.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ import {
1717
stripSurroundingQuotes,
1818
getNodeAtPosition,
1919
} from "./utils/node.js";
20-
import { Draft07, JsonError } from "json-schema-library";
20+
import { getJSONSchema } from "./state.js";
21+
import { Draft07, isJsonError } from "json-schema-library";
2122
import { jsonPointerForPosition } from "./utils/jsonPointers.js";
2223
import { TOKENS } from "./constants.js";
23-
import { getJSONSchema } from "./state.js";
24-
import getSchema from "./utils/schema-lib/getSchema.js";
2524

2625
function json5PropertyInsertSnippet(rawWord: string, value: string) {
2726
if (rawWord.startsWith('"')) {
@@ -688,7 +687,10 @@ export class JSONCompletion {
688687
): JSONSchema7Definition[] {
689688
const draft = new Draft07(this.schema!);
690689
let pointer = jsonPointerForPosition(ctx.state, ctx.pos);
691-
let subSchema = getSchema(draft, pointer);
690+
let subSchema = draft.getSchema({ pointer });
691+
if (isJsonError(subSchema)) {
692+
subSchema = subSchema.data?.schema;
693+
}
692694
// if we don't have a schema for the current pointer, try the parent pointer
693695
if (
694696
!subSchema ||
@@ -697,7 +699,7 @@ export class JSONCompletion {
697699
subSchema.type === "undefined"
698700
) {
699701
pointer = pointer.replace(/\/[^/]*$/, "/");
700-
subSchema = getSchema(draft, pointer);
702+
subSchema = draft.getSchema({ pointer });
701703
}
702704

703705
debug.log("xxx", "pointer..", JSON.stringify(pointer));
@@ -708,8 +710,7 @@ export class JSONCompletion {
708710
}
709711
// const subSchema = new Draft07(this.schema).getSchema(pointer);
710712
debug.log("xxx", "subSchema..", subSchema);
711-
712-
if (this.isJsonError(subSchema)) {
713+
if (!subSchema) {
713714
return [];
714715
}
715716

@@ -735,10 +736,6 @@ export class JSONCompletion {
735736
return [subSchema as JSONSchema7];
736737
}
737738

738-
isJsonError(d: JSONSchema7 | JsonError): d is JsonError {
739-
return d.type === "error";
740-
}
741-
742739
private expandSchemaProperty(
743740
property: JSONSchema7Definition,
744741
schema: JSONSchema7

0 commit comments

Comments
 (0)