1 /*---------------------------------------------------------------------------------------------
2 * Copyright (c) Microsoft Corporation. All rights reserved.
3 * Licensed under the MIT License. See License.txt in the project root for license information.
4 *--------------------------------------------------------------------------------------------*/
6 if (typeof module === "object" && typeof module.exports === "object") {
7 var v = factory(require, exports);
8 if (v !== undefined) module.exports = v;
10 else if (typeof define === "function" && define.amd) {
11 define(["require", "exports", "./jsonSchemaService", "../jsonLanguageTypes", "vscode-nls", "../utils/objects"], factory);
13 })(function (require, exports) {
15 Object.defineProperty(exports, "__esModule", { value: true });
16 exports.JSONValidation = void 0;
17 var jsonSchemaService_1 = require("./jsonSchemaService");
18 var jsonLanguageTypes_1 = require("../jsonLanguageTypes");
19 var nls = require("vscode-nls");
20 var objects_1 = require("../utils/objects");
21 var localize = nls.loadMessageBundle();
22 var JSONValidation = /** @class */ (function () {
23 function JSONValidation(jsonSchemaService, promiseConstructor) {
24 this.jsonSchemaService = jsonSchemaService;
25 this.promise = promiseConstructor;
26 this.validationEnabled = true;
28 JSONValidation.prototype.configure = function (raw) {
30 this.validationEnabled = raw.validate !== false;
31 this.commentSeverity = raw.allowComments ? undefined : jsonLanguageTypes_1.DiagnosticSeverity.Error;
34 JSONValidation.prototype.doValidation = function (textDocument, jsonDocument, documentSettings, schema) {
36 if (!this.validationEnabled) {
37 return this.promise.resolve([]);
41 var addProblem = function (problem) {
42 // remove duplicated messages
43 var signature = problem.range.start.line + ' ' + problem.range.start.character + ' ' + problem.message;
44 if (!added[signature]) {
45 added[signature] = true;
46 diagnostics.push(problem);
49 var getDiagnostics = function (schema) {
50 var trailingCommaSeverity = documentSettings ? toDiagnosticSeverity(documentSettings.trailingCommas) : jsonLanguageTypes_1.DiagnosticSeverity.Error;
51 var commentSeverity = documentSettings ? toDiagnosticSeverity(documentSettings.comments) : _this.commentSeverity;
52 var schemaValidation = (documentSettings === null || documentSettings === void 0 ? void 0 : documentSettings.schemaValidation) ? toDiagnosticSeverity(documentSettings.schemaValidation) : jsonLanguageTypes_1.DiagnosticSeverity.Warning;
53 var schemaRequest = (documentSettings === null || documentSettings === void 0 ? void 0 : documentSettings.schemaRequest) ? toDiagnosticSeverity(documentSettings.schemaRequest) : jsonLanguageTypes_1.DiagnosticSeverity.Warning;
55 if (schema.errors.length && jsonDocument.root && schemaRequest) {
56 var astRoot = jsonDocument.root;
57 var property = astRoot.type === 'object' ? astRoot.properties[0] : undefined;
58 if (property && property.keyNode.value === '$schema') {
59 var node = property.valueNode || property;
60 var range = jsonLanguageTypes_1.Range.create(textDocument.positionAt(node.offset), textDocument.positionAt(node.offset + node.length));
61 addProblem(jsonLanguageTypes_1.Diagnostic.create(range, schema.errors[0], schemaRequest, jsonLanguageTypes_1.ErrorCode.SchemaResolveError));
64 var range = jsonLanguageTypes_1.Range.create(textDocument.positionAt(astRoot.offset), textDocument.positionAt(astRoot.offset + 1));
65 addProblem(jsonLanguageTypes_1.Diagnostic.create(range, schema.errors[0], schemaRequest, jsonLanguageTypes_1.ErrorCode.SchemaResolveError));
68 else if (schemaValidation) {
69 var semanticErrors = jsonDocument.validate(textDocument, schema.schema, schemaValidation);
71 semanticErrors.forEach(addProblem);
74 if (schemaAllowsComments(schema.schema)) {
75 commentSeverity = undefined;
77 if (schemaAllowsTrailingCommas(schema.schema)) {
78 trailingCommaSeverity = undefined;
81 for (var _i = 0, _a = jsonDocument.syntaxErrors; _i < _a.length; _i++) {
83 if (p.code === jsonLanguageTypes_1.ErrorCode.TrailingComma) {
84 if (typeof trailingCommaSeverity !== 'number') {
87 p.severity = trailingCommaSeverity;
91 if (typeof commentSeverity === 'number') {
92 var message_1 = localize('InvalidCommentToken', 'Comments are not permitted in JSON.');
93 jsonDocument.comments.forEach(function (c) {
94 addProblem(jsonLanguageTypes_1.Diagnostic.create(c, message_1, commentSeverity, jsonLanguageTypes_1.ErrorCode.CommentNotPermitted));
100 var id = schema.id || ('schemaservice://untitled/' + idCounter++);
101 return this.jsonSchemaService.resolveSchemaContent(new jsonSchemaService_1.UnresolvedSchema(schema), id, {}).then(function (resolvedSchema) {
102 return getDiagnostics(resolvedSchema);
105 return this.jsonSchemaService.getSchemaForResource(textDocument.uri, jsonDocument).then(function (schema) {
106 return getDiagnostics(schema);
109 return JSONValidation;
111 exports.JSONValidation = JSONValidation;
113 function schemaAllowsComments(schemaRef) {
114 if (schemaRef && typeof schemaRef === 'object') {
115 if (objects_1.isBoolean(schemaRef.allowComments)) {
116 return schemaRef.allowComments;
118 if (schemaRef.allOf) {
119 for (var _i = 0, _a = schemaRef.allOf; _i < _a.length; _i++) {
121 var allow = schemaAllowsComments(schema);
122 if (objects_1.isBoolean(allow)) {
130 function schemaAllowsTrailingCommas(schemaRef) {
131 if (schemaRef && typeof schemaRef === 'object') {
132 if (objects_1.isBoolean(schemaRef.allowTrailingCommas)) {
133 return schemaRef.allowTrailingCommas;
135 var deprSchemaRef = schemaRef;
136 if (objects_1.isBoolean(deprSchemaRef['allowsTrailingCommas'])) { // deprecated
137 return deprSchemaRef['allowsTrailingCommas'];
139 if (schemaRef.allOf) {
140 for (var _i = 0, _a = schemaRef.allOf; _i < _a.length; _i++) {
142 var allow = schemaAllowsTrailingCommas(schema);
143 if (objects_1.isBoolean(allow)) {
151 function toDiagnosticSeverity(severityLevel) {
152 switch (severityLevel) {
153 case 'error': return jsonLanguageTypes_1.DiagnosticSeverity.Error;
154 case 'warning': return jsonLanguageTypes_1.DiagnosticSeverity.Warning;
155 case 'ignore': return undefined;