jsonValidation.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. (function (factory) {
  6. if (typeof module === "object" && typeof module.exports === "object") {
  7. var v = factory(require, exports);
  8. if (v !== undefined) module.exports = v;
  9. }
  10. else if (typeof define === "function" && define.amd) {
  11. define(["require", "exports", "./jsonSchemaService", "../jsonLanguageTypes", "vscode-nls", "../utils/objects"], factory);
  12. }
  13. })(function (require, exports) {
  14. "use strict";
  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;
  27. }
  28. JSONValidation.prototype.configure = function (raw) {
  29. if (raw) {
  30. this.validationEnabled = raw.validate !== false;
  31. this.commentSeverity = raw.allowComments ? undefined : jsonLanguageTypes_1.DiagnosticSeverity.Error;
  32. }
  33. };
  34. JSONValidation.prototype.doValidation = function (textDocument, jsonDocument, documentSettings, schema) {
  35. var _this = this;
  36. if (!this.validationEnabled) {
  37. return this.promise.resolve([]);
  38. }
  39. var diagnostics = [];
  40. var added = {};
  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);
  47. }
  48. };
  49. var getDiagnostics = function (schema) {
  50. var trailingCommaSeverity = (documentSettings === null || documentSettings === void 0 ? void 0 : documentSettings.trailingCommas) ? toDiagnosticSeverity(documentSettings.trailingCommas) : jsonLanguageTypes_1.DiagnosticSeverity.Error;
  51. var commentSeverity = (documentSettings === null || documentSettings === void 0 ? void 0 : documentSettings.comments) ? 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;
  54. if (schema) {
  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));
  62. }
  63. else {
  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));
  66. }
  67. }
  68. else if (schemaValidation) {
  69. var semanticErrors = jsonDocument.validate(textDocument, schema.schema, schemaValidation);
  70. if (semanticErrors) {
  71. semanticErrors.forEach(addProblem);
  72. }
  73. }
  74. if (schemaAllowsComments(schema.schema)) {
  75. commentSeverity = undefined;
  76. }
  77. if (schemaAllowsTrailingCommas(schema.schema)) {
  78. trailingCommaSeverity = undefined;
  79. }
  80. }
  81. for (var _i = 0, _a = jsonDocument.syntaxErrors; _i < _a.length; _i++) {
  82. var p = _a[_i];
  83. if (p.code === jsonLanguageTypes_1.ErrorCode.TrailingComma) {
  84. if (typeof trailingCommaSeverity !== 'number') {
  85. continue;
  86. }
  87. p.severity = trailingCommaSeverity;
  88. }
  89. addProblem(p);
  90. }
  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));
  95. });
  96. }
  97. return diagnostics;
  98. };
  99. if (schema) {
  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);
  103. });
  104. }
  105. return this.jsonSchemaService.getSchemaForResource(textDocument.uri, jsonDocument).then(function (schema) {
  106. return getDiagnostics(schema);
  107. });
  108. };
  109. return JSONValidation;
  110. }());
  111. exports.JSONValidation = JSONValidation;
  112. var idCounter = 0;
  113. function schemaAllowsComments(schemaRef) {
  114. if (schemaRef && typeof schemaRef === 'object') {
  115. if (objects_1.isBoolean(schemaRef.allowComments)) {
  116. return schemaRef.allowComments;
  117. }
  118. if (schemaRef.allOf) {
  119. for (var _i = 0, _a = schemaRef.allOf; _i < _a.length; _i++) {
  120. var schema = _a[_i];
  121. var allow = schemaAllowsComments(schema);
  122. if (objects_1.isBoolean(allow)) {
  123. return allow;
  124. }
  125. }
  126. }
  127. }
  128. return undefined;
  129. }
  130. function schemaAllowsTrailingCommas(schemaRef) {
  131. if (schemaRef && typeof schemaRef === 'object') {
  132. if (objects_1.isBoolean(schemaRef.allowTrailingCommas)) {
  133. return schemaRef.allowTrailingCommas;
  134. }
  135. var deprSchemaRef = schemaRef;
  136. if (objects_1.isBoolean(deprSchemaRef['allowsTrailingCommas'])) { // deprecated
  137. return deprSchemaRef['allowsTrailingCommas'];
  138. }
  139. if (schemaRef.allOf) {
  140. for (var _i = 0, _a = schemaRef.allOf; _i < _a.length; _i++) {
  141. var schema = _a[_i];
  142. var allow = schemaAllowsTrailingCommas(schema);
  143. if (objects_1.isBoolean(allow)) {
  144. return allow;
  145. }
  146. }
  147. }
  148. }
  149. return undefined;
  150. }
  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;
  156. }
  157. return undefined;
  158. }
  159. });