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 *--------------------------------------------------------------------------------------------*/
5 import { UnresolvedSchema } from './jsonSchemaService';
6 import { ErrorCode, Diagnostic, DiagnosticSeverity, Range } from '../jsonLanguageTypes';
7 import * as nls from 'vscode-nls';
8 import { isBoolean } from '../utils/objects';
9 var localize = nls.loadMessageBundle();
10 var JSONValidation = /** @class */ (function () {
11 function JSONValidation(jsonSchemaService, promiseConstructor) {
12 this.jsonSchemaService = jsonSchemaService;
13 this.promise = promiseConstructor;
14 this.validationEnabled = true;
16 JSONValidation.prototype.configure = function (raw) {
18 this.validationEnabled = raw.validate !== false;
19 this.commentSeverity = raw.allowComments ? undefined : DiagnosticSeverity.Error;
22 JSONValidation.prototype.doValidation = function (textDocument, jsonDocument, documentSettings, schema) {
24 if (!this.validationEnabled) {
25 return this.promise.resolve([]);
29 var addProblem = function (problem) {
30 // remove duplicated messages
31 var signature = problem.range.start.line + ' ' + problem.range.start.character + ' ' + problem.message;
32 if (!added[signature]) {
33 added[signature] = true;
34 diagnostics.push(problem);
37 var getDiagnostics = function (schema) {
38 var trailingCommaSeverity = documentSettings ? toDiagnosticSeverity(documentSettings.trailingCommas) : DiagnosticSeverity.Error;
39 var commentSeverity = documentSettings ? toDiagnosticSeverity(documentSettings.comments) : _this.commentSeverity;
40 var schemaValidation = (documentSettings === null || documentSettings === void 0 ? void 0 : documentSettings.schemaValidation) ? toDiagnosticSeverity(documentSettings.schemaValidation) : DiagnosticSeverity.Warning;
41 var schemaRequest = (documentSettings === null || documentSettings === void 0 ? void 0 : documentSettings.schemaRequest) ? toDiagnosticSeverity(documentSettings.schemaRequest) : DiagnosticSeverity.Warning;
43 if (schema.errors.length && jsonDocument.root && schemaRequest) {
44 var astRoot = jsonDocument.root;
45 var property = astRoot.type === 'object' ? astRoot.properties[0] : undefined;
46 if (property && property.keyNode.value === '$schema') {
47 var node = property.valueNode || property;
48 var range = Range.create(textDocument.positionAt(node.offset), textDocument.positionAt(node.offset + node.length));
49 addProblem(Diagnostic.create(range, schema.errors[0], schemaRequest, ErrorCode.SchemaResolveError));
52 var range = Range.create(textDocument.positionAt(astRoot.offset), textDocument.positionAt(astRoot.offset + 1));
53 addProblem(Diagnostic.create(range, schema.errors[0], schemaRequest, ErrorCode.SchemaResolveError));
56 else if (schemaValidation) {
57 var semanticErrors = jsonDocument.validate(textDocument, schema.schema, schemaValidation);
59 semanticErrors.forEach(addProblem);
62 if (schemaAllowsComments(schema.schema)) {
63 commentSeverity = undefined;
65 if (schemaAllowsTrailingCommas(schema.schema)) {
66 trailingCommaSeverity = undefined;
69 for (var _i = 0, _a = jsonDocument.syntaxErrors; _i < _a.length; _i++) {
71 if (p.code === ErrorCode.TrailingComma) {
72 if (typeof trailingCommaSeverity !== 'number') {
75 p.severity = trailingCommaSeverity;
79 if (typeof commentSeverity === 'number') {
80 var message_1 = localize('InvalidCommentToken', 'Comments are not permitted in JSON.');
81 jsonDocument.comments.forEach(function (c) {
82 addProblem(Diagnostic.create(c, message_1, commentSeverity, ErrorCode.CommentNotPermitted));
88 var id = schema.id || ('schemaservice://untitled/' + idCounter++);
89 return this.jsonSchemaService.resolveSchemaContent(new UnresolvedSchema(schema), id, {}).then(function (resolvedSchema) {
90 return getDiagnostics(resolvedSchema);
93 return this.jsonSchemaService.getSchemaForResource(textDocument.uri, jsonDocument).then(function (schema) {
94 return getDiagnostics(schema);
97 return JSONValidation;
99 export { JSONValidation };
101 function schemaAllowsComments(schemaRef) {
102 if (schemaRef && typeof schemaRef === 'object') {
103 if (isBoolean(schemaRef.allowComments)) {
104 return schemaRef.allowComments;
106 if (schemaRef.allOf) {
107 for (var _i = 0, _a = schemaRef.allOf; _i < _a.length; _i++) {
109 var allow = schemaAllowsComments(schema);
110 if (isBoolean(allow)) {
118 function schemaAllowsTrailingCommas(schemaRef) {
119 if (schemaRef && typeof schemaRef === 'object') {
120 if (isBoolean(schemaRef.allowTrailingCommas)) {
121 return schemaRef.allowTrailingCommas;
123 var deprSchemaRef = schemaRef;
124 if (isBoolean(deprSchemaRef['allowsTrailingCommas'])) { // deprecated
125 return deprSchemaRef['allowsTrailingCommas'];
127 if (schemaRef.allOf) {
128 for (var _i = 0, _a = schemaRef.allOf; _i < _a.length; _i++) {
130 var allow = schemaAllowsTrailingCommas(schema);
131 if (isBoolean(allow)) {
139 function toDiagnosticSeverity(severityLevel) {
140 switch (severityLevel) {
141 case 'error': return DiagnosticSeverity.Error;
142 case 'warning': return DiagnosticSeverity.Warning;
143 case 'ignore': return undefined;