Skip to content

Commit c7d811a

Browse files
committed
prepare 5.7.0
1 parent 1e06466 commit c7d811a

File tree

7 files changed

+326
-345
lines changed

7 files changed

+326
-345
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
5.7.0 / 2026-01-12
2+
================
3+
* the error code for `SchemaResolveError` is now 0x10000 plus the error code returned by the request service.
4+
15
5.6.0 / 2025-05-28
26
================
37
* added `Schema.enumSortTexts` and `Schema.enumDetails` to control the sort order and presentation of suggestions for enums

package-lock.json

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

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vscode-json-languageservice",
3-
"version": "5.6.4",
3+
"version": "5.7.0",
44
"description": "Language service for JSON",
55
"main": "./lib/umd/jsonLanguageService.js",
66
"typings": "./lib/umd/jsonLanguageService",
@@ -17,12 +17,12 @@
1717
"devDependencies": {
1818
"@types/mocha": "^10.0.10",
1919
"@types/node": "22.x",
20-
"@typescript-eslint/eslint-plugin": "^8.48.0",
21-
"@typescript-eslint/parser": "^8.48.0",
22-
"eslint": "^9.39.1",
20+
"@typescript-eslint/eslint-plugin": "^8.53.0",
21+
"@typescript-eslint/parser": "^8.53.0",
22+
"eslint": "^9.39.2",
2323
"json-schema-test-suite": "https://github.com/json-schema-org/JSON-Schema-Test-Suite.git#69acf52990b004240839ae19b4bec8fb01d50876",
24-
"mocha": "^11.7.4",
25-
"rimraf": "^6.1.0",
24+
"mocha": "^11.7.5",
25+
"rimraf": "^6.1.2",
2626
"typescript": "^5.9.3"
2727
},
2828
"dependencies": {

src/jsonLanguageTypes.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,12 @@ export enum ErrorCode {
6161
DuplicateKey = 0x208,
6262
CommentNotPermitted = 0x209,
6363
PropertyKeysMustBeDoublequoted = 0x210,
64-
SchemaResolveError = 0x300,
65-
SchemaUnsupportedFeature = 0x301
64+
SchemaUnsupportedFeature = 0x301,
65+
SchemaResolveError = 0x10000,
66+
}
67+
68+
export function isSchemaResolveError(code: number): boolean {
69+
return code >= ErrorCode.SchemaResolveError;
6670
}
6771

6872
export type ASTNode = ObjectASTNode | PropertyASTNode | ArrayASTNode | StringASTNode | NumberASTNode | BooleanASTNode | NullASTNode;
@@ -203,7 +207,8 @@ export interface WorkspaceContextService {
203207
}
204208
/**
205209
* The schema request service is used to fetch schemas. If successful, returns a resolved promise with the content of the schema.
206-
* In case of an error, returns a rejected promise with a displayable error string.
210+
* In case of an error, returns a rejected promise with an Error object. If the type is of form { message: string, code: number }, the
211+
* error code will be used for diagnostics.
207212
*/
208213
export interface SchemaRequestService {
209214
(uri: string): PromiseLike<string>;

src/services/jsonSchemaService.ts

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { JSONSchema, JSONSchemaMap, JSONSchemaRef } from '../jsonSchema';
88
import { URI } from 'vscode-uri';
99
import * as Strings from '../utils/strings';
1010
import { asSchema, getSchemaDraftFromId, JSONDocument, normalizeId } from '../parser/jsonParser';
11-
import { SchemaRequestService, WorkspaceContextService, PromiseConstructor, MatchingSchema, TextDocument, SchemaConfiguration, SchemaDraft } from '../jsonLanguageTypes';
11+
import { SchemaRequestService, WorkspaceContextService, PromiseConstructor, MatchingSchema, TextDocument, SchemaConfiguration, SchemaDraft, ErrorCode } from '../jsonLanguageTypes';
1212

1313
import * as l10n from '@vscode/l10n';
1414
import { createRegex } from '../utils/glob';
@@ -181,21 +181,27 @@ class SchemaHandle implements ISchemaHandle {
181181

182182
export class UnresolvedSchema {
183183
public schema: JSONSchema;
184-
public errors: string[];
184+
public errors: Diagnostic[];
185185

186-
constructor(schema: JSONSchema, errors: string[] = []) {
186+
constructor(schema: JSONSchema, errors: Diagnostic[] = []) {
187187
this.schema = schema;
188188
this.errors = errors;
189189
}
190190
}
191191

192+
export type Diagnostic = { message: string; code: ErrorCode }
193+
194+
function toDiagnostic(message: string, code: ErrorCode): Diagnostic {
195+
return { message, code };
196+
}
197+
192198
export class ResolvedSchema {
193199
public readonly schema: JSONSchema;
194-
public readonly errors: string[];
195-
public readonly warnings: string[];
200+
public readonly errors: Diagnostic[];
201+
public readonly warnings: Diagnostic[];
196202
public readonly schemaDraft: SchemaDraft | undefined;
197203

198-
constructor(schema: JSONSchema, errors: string[] = [], warnings: string[] = [], schemaDraft: SchemaDraft | undefined) {
204+
constructor(schema: JSONSchema, errors: Diagnostic[] = [], warnings: Diagnostic[] = [], schemaDraft: SchemaDraft | undefined) {
199205
this.schema = schema;
200206
this.errors = errors;
201207
this.warnings = warnings;
@@ -388,51 +394,60 @@ export class JSONSchemaService implements IJSONSchemaService {
388394
public loadSchema(url: string): PromiseLike<UnresolvedSchema> {
389395
if (!this.requestService) {
390396
const errorMessage = l10n.t('Unable to load schema from \'{0}\'. No schema request service available', toDisplayString(url));
391-
return this.promise.resolve(new UnresolvedSchema(<JSONSchema>{}, [errorMessage]));
397+
return this.promise.resolve(new UnresolvedSchema(<JSONSchema>{}, [toDiagnostic(errorMessage, ErrorCode.SchemaResolveError)]));
392398
}
393399
return this.requestService(url).then(
394400
content => {
395401
if (!content) {
396402
const errorMessage = l10n.t('Unable to load schema from \'{0}\': No content.', toDisplayString(url));
397-
return new UnresolvedSchema(<JSONSchema>{}, [errorMessage]);
403+
return new UnresolvedSchema(<JSONSchema>{}, [toDiagnostic(errorMessage, ErrorCode.SchemaResolveError)]);
398404
}
399405
const errors = [];
400406
if (content.charCodeAt(0) === 65279) {
401-
errors.push(l10n.t('Problem reading content from \'{0}\': UTF-8 with BOM detected, only UTF 8 is allowed.', toDisplayString(url)));
407+
errors.push(toDiagnostic(l10n.t('Problem reading content from \'{0}\': UTF-8 with BOM detected, only UTF 8 is allowed.', toDisplayString(url)), ErrorCode.SchemaResolveError));
402408
content = content.trimStart();
403409
}
404410

405411
let schemaContent: JSONSchema = {};
406412
const jsonErrors: Json.ParseError[] = [];
407413
schemaContent = Json.parse(content, jsonErrors);
408414
if (jsonErrors.length) {
409-
errors.push(l10n.t('Unable to parse content from \'{0}\': Parse error at offset {1}.', toDisplayString(url), jsonErrors[0].offset));
415+
errors.push(toDiagnostic(l10n.t('Unable to parse content from \'{0}\': Parse error at offset {1}.', toDisplayString(url), jsonErrors[0].offset), ErrorCode.SchemaResolveError));
410416
}
411417
return new UnresolvedSchema(schemaContent, errors);
412418
},
413419
(error: any) => {
414-
let errorMessage = error.toString() as string;
415-
const errorSplit = error.toString().split('Error: ');
416-
if (errorSplit.length > 1) {
417-
// more concise error message, URL and context are attached by caller anyways
418-
errorMessage = errorSplit[1];
420+
let { message, code } = error;
421+
if (typeof message !== 'string') {
422+
let errorMessage = error.toString() as string;
423+
const errorSplit = error.toString().split('Error: ');
424+
if (errorSplit.length > 1) {
425+
// more concise error message, URL and context are attached by caller anyways
426+
errorMessage = errorSplit[1];
427+
}
428+
if (Strings.endsWith(errorMessage, '.')) {
429+
errorMessage = errorMessage.substr(0, errorMessage.length - 1);
430+
}
431+
message = errorMessage;
419432
}
420-
if (Strings.endsWith(errorMessage, '.')) {
421-
errorMessage = errorMessage.substr(0, errorMessage.length - 1);
433+
let errorCode = ErrorCode.SchemaResolveError;
434+
if (typeof code === 'number' && code < 0x10000) {
435+
errorCode += code;
422436
}
423-
return new UnresolvedSchema(<JSONSchema>{}, [l10n.t('Unable to load schema from \'{0}\': {1}.', toDisplayString(url), errorMessage)]);
437+
const errorMessage = l10n.t('Unable to load schema from \'{0}\': {1}.', toDisplayString(url), message);
438+
return new UnresolvedSchema(<JSONSchema>{}, [toDiagnostic(errorMessage, errorCode)]);
424439
}
425440
);
426441
}
427442

428443
public resolveSchemaContent(schemaToResolve: UnresolvedSchema, handle: SchemaHandle): PromiseLike<ResolvedSchema> {
429444

430-
const resolveErrors: string[] = schemaToResolve.errors.slice(0);
445+
const resolveErrors: Diagnostic[] = schemaToResolve.errors.slice(0);
431446
const schema = schemaToResolve.schema;
432447

433448
const schemaDraft = schema.$schema ? getSchemaDraftFromId(schema.$schema) : undefined;
434449
if (schemaDraft === SchemaDraft.v3) {
435-
return this.promise.resolve(new ResolvedSchema({}, [l10n.t("Draft-03 schemas are not supported.")], [], schemaDraft));
450+
return this.promise.resolve(new ResolvedSchema({}, [toDiagnostic(l10n.t("Draft-03 schemas are not supported."), ErrorCode.SchemaUnsupportedFeature)], [], schemaDraft));
436451
}
437452

438453
let usesUnsupportedFeatures = new Set();
@@ -482,7 +497,8 @@ export class JSONSchemaService implements IJSONSchemaService {
482497
if (section) {
483498
merge(target, section);
484499
} else {
485-
resolveErrors.push(l10n.t('$ref \'{0}\' in \'{1}\' can not be resolved.', refSegment || '', sourceHandle.uri));
500+
const message = l10n.t('$ref \'{0}\' in \'{1}\' can not be resolved.', refSegment || '', sourceHandle.uri)
501+
resolveErrors.push(toDiagnostic(message, ErrorCode.SchemaResolveError));
486502
}
487503
};
488504

@@ -496,7 +512,8 @@ export class JSONSchemaService implements IJSONSchemaService {
496512
parentHandle.dependencies.add(uri);
497513
if (unresolvedSchema.errors.length) {
498514
const loc = refSegment ? uri + '#' + refSegment : uri;
499-
resolveErrors.push(l10n.t('Problems loading reference \'{0}\': {1}', loc, unresolvedSchema.errors[0]));
515+
const errorMessage = l10n.t('Problems loading reference \'{0}\': {1}', loc, unresolvedSchema.errors[0].message);
516+
resolveErrors.push(toDiagnostic(errorMessage, unresolvedSchema.errors[0].code));
500517
}
501518
mergeRef(node, unresolvedSchema.schema, referencedHandle, refSegment);
502519
return resolveRefs(node, unresolvedSchema.schema, referencedHandle);
@@ -543,7 +560,7 @@ export class JSONSchemaService implements IJSONSchemaService {
543560
const anchor = isString(id) && id.charAt(0) === '#' ? id.substring(1) : next.$anchor;
544561
if (anchor) {
545562
if (result.has(anchor)) {
546-
resolveErrors.push(l10n.t('Duplicate anchor declaration: \'{0}\'', anchor));
563+
resolveErrors.push(toDiagnostic(l10n.t('Duplicate anchor declaration: \'{0}\'', anchor), ErrorCode.SchemaResolveError));
547564
} else {
548565
result.set(anchor, next);
549566
}
@@ -558,9 +575,9 @@ export class JSONSchemaService implements IJSONSchemaService {
558575
return result;
559576
};
560577
return resolveRefs(schema, schema, handle).then(_ => {
561-
let resolveWarnings: string[] = [];
578+
let resolveWarnings: Diagnostic[] = [];
562579
if (usesUnsupportedFeatures.size) {
563-
resolveWarnings.push(l10n.t('The schema uses meta-schema features ({0}) that are not yet supported by the validator.', Array.from(usesUnsupportedFeatures.keys()).join(', ')));
580+
resolveWarnings.push(toDiagnostic(l10n.t('The schema uses meta-schema features ({0}) that are not yet supported by the validator.', Array.from(usesUnsupportedFeatures.keys()).join(', ')), ErrorCode.SchemaUnsupportedFeature));
564581
}
565582
return new ResolvedSchema(schema, resolveErrors, resolveWarnings, schemaDraft);
566583
});

src/services/jsonValidation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class JSONValidation {
2525
this.validationEnabled = true;
2626
}
2727

28-
public configure(raw: LanguageSettings) {
28+
public configure(raw: LanguageSettings | undefined): void {
2929
if (raw) {
3030
this.validationEnabled = raw.validate !== false;
3131
this.commentSeverity = raw.allowComments ? undefined : DiagnosticSeverity.Error;
@@ -68,10 +68,10 @@ export class JSONValidation {
6868
}
6969
};
7070
if (schema.errors.length) {
71-
addSchemaProblem(schema.errors[0], ErrorCode.SchemaResolveError);
71+
addSchemaProblem(schema.errors[0].message, schema.errors[0].code);
7272
} else if (schemaValidation) {
7373
for (const warning of schema.warnings) {
74-
addSchemaProblem(warning, ErrorCode.SchemaUnsupportedFeature);
74+
addSchemaProblem(warning.message, warning.code);
7575
}
7676
const semanticErrors = jsonDocument.validate(textDocument, schema.schema, schemaValidation, documentSettings?.schemaDraft);
7777
if (semanticErrors) {

src/test/schema.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { promises as fs } from 'fs';
1010
import * as url from 'url';
1111
import * as path from 'path';
1212
import { getLanguageService, JSONSchema, SchemaRequestService, TextDocument, MatchingSchema, LanguageService } from '../jsonLanguageService';
13-
import { DiagnosticSeverity, SchemaConfiguration } from '../jsonLanguageTypes';
13+
import { DiagnosticSeverity, ErrorCode, SchemaConfiguration } from '../jsonLanguageTypes';
1414

1515
function toDocument(text: string, config?: Parser.JSONDocumentConfig, uri = 'foo://bar/file.json'): { textDoc: TextDocument, jsonDoc: Parser.JSONDocument } {
1616

@@ -1079,8 +1079,7 @@ suite('JSON Schema', () => {
10791079
service.clearExternalSchemas();
10801080

10811081
resolvedSchema = await service.getSchemaForResource('main.bar');
1082-
assert.strictEqual(resolvedSchema?.errors.length, 1);
1083-
assert.strictEqual(resolvedSchema?.errors[0], "Problems loading reference 'http://myschemastore/myschemafoo': Unable to load schema from 'http://myschemastore/myschemafoo': Resource not found.");
1082+
assert.deepStrictEqual(resolvedSchema?.errors, [{ message: "Problems loading reference 'http://myschemastore/myschemafoo': Unable to load schema from 'http://myschemastore/myschemafoo': Resource not found.", code: ErrorCode.SchemaResolveError }]);
10841083

10851084
service.clearExternalSchemas();
10861085
service.registerExternalSchema({ uri: id2, schema: schema2 });

0 commit comments

Comments
 (0)