-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathjsonValidation.ts
More file actions
173 lines (155 loc) · 6.59 KB
/
jsonValidation.ts
File metadata and controls
173 lines (155 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { JSONSchemaService, ResolvedSchema } from './jsonSchemaService';
import { JSONDocument } from '../parser/jsonParser';
import { TextDocument, ErrorCode, PromiseConstructor, LanguageSettings, DocumentLanguageSettings, SeverityLevel, Diagnostic, DiagnosticSeverity, Range, JSONLanguageStatus } from '../jsonLanguageTypes';
import * as l10n from '@vscode/l10n';
import { JSONSchemaRef, JSONSchema } from '../jsonSchema';
import { isBoolean } from '../utils/objects';
export class JSONValidation {
private jsonSchemaService: JSONSchemaService;
private promise: PromiseConstructor;
private validationEnabled: boolean | undefined;
private commentSeverity: DiagnosticSeverity | undefined;
public constructor(jsonSchemaService: JSONSchemaService, promiseConstructor: PromiseConstructor) {
this.jsonSchemaService = jsonSchemaService;
this.promise = promiseConstructor;
this.validationEnabled = true;
}
public configure(raw: LanguageSettings | undefined): void {
if (raw) {
this.validationEnabled = raw.validate !== false;
this.commentSeverity = raw.allowComments ? undefined : DiagnosticSeverity.Error;
}
}
public doValidation(textDocument: TextDocument, jsonDocument: JSONDocument, documentSettings?: DocumentLanguageSettings, schema?: JSONSchema): PromiseLike<Diagnostic[]> {
if (!this.validationEnabled) {
return this.promise.resolve([]);
}
const diagnostics: Diagnostic[] = [];
const added: { [signature: string]: boolean } = {};
const addProblem = (problem: Diagnostic) => {
// remove duplicated messages
const signature = problem.range.start.line + ' ' + problem.range.start.character + ' ' + problem.message;
if (!added[signature]) {
added[signature] = true;
diagnostics.push(problem);
}
};
const getDiagnostics = (schema: ResolvedSchema | undefined) => {
let trailingCommaSeverity = documentSettings?.trailingCommas ? toDiagnosticSeverity(documentSettings.trailingCommas) : DiagnosticSeverity.Error;
let commentSeverity = documentSettings?.comments ? toDiagnosticSeverity(documentSettings.comments) : this.commentSeverity;
let schemaValidation = documentSettings?.schemaValidation ? toDiagnosticSeverity(documentSettings.schemaValidation) : DiagnosticSeverity.Warning;
let schemaRequest = documentSettings?.schemaRequest ? toDiagnosticSeverity(documentSettings.schemaRequest) : DiagnosticSeverity.Warning;
if (schema) {
const addSchemaProblem = (errorMessage: string, errorCode: ErrorCode) => {
if (jsonDocument.root && schemaRequest) {
const astRoot = jsonDocument.root;
const property = astRoot.type === 'object' ? astRoot.properties[0] : undefined;
if (property && property.keyNode.value === '$schema') {
const node = property.valueNode || property;
const range = Range.create(textDocument.positionAt(node.offset), textDocument.positionAt(node.offset + node.length));
addProblem(Diagnostic.create(range, errorMessage, schemaRequest, errorCode));
} else {
const range = Range.create(textDocument.positionAt(astRoot.offset), textDocument.positionAt(astRoot.offset + 1));
addProblem(Diagnostic.create(range, errorMessage, schemaRequest, errorCode));
}
}
};
if (schema.errors.length) {
addSchemaProblem(schema.errors[0].message, schema.errors[0].code);
} else if (schemaValidation) {
for (const warning of schema.warnings) {
addSchemaProblem(warning.message, warning.code);
}
const semanticErrors = jsonDocument.validate(textDocument, schema.schema, schemaValidation, documentSettings?.schemaDraft);
if (semanticErrors) {
semanticErrors.forEach(addProblem);
}
}
if (schemaAllowsComments(schema.schema)) {
commentSeverity = undefined;
}
if (schemaAllowsTrailingCommas(schema.schema)) {
trailingCommaSeverity = undefined;
}
}
for (const p of jsonDocument.syntaxErrors) {
if (p.code === ErrorCode.TrailingComma) {
if (typeof trailingCommaSeverity !== 'number') {
continue;
}
p.severity = trailingCommaSeverity;
}
addProblem(p);
}
if (typeof commentSeverity === 'number') {
const message = l10n.t('Comments are not permitted in JSON.');
jsonDocument.comments.forEach(c => {
addProblem(Diagnostic.create(c, message, commentSeverity, ErrorCode.CommentNotPermitted));
});
}
return diagnostics;
};
if (schema) {
const uri = schema.id || ('schemaservice://untitled/' + idCounter++);
const handle = this.jsonSchemaService.registerExternalSchema({ uri, schema });
return handle.getResolvedSchema().then(resolvedSchema => {
return getDiagnostics(resolvedSchema);
});
}
return this.jsonSchemaService.getSchemaForResource(textDocument.uri, jsonDocument).then(schema => {
return getDiagnostics(schema);
});
}
public getLanguageStatus(textDocument: TextDocument, jsonDocument: JSONDocument): JSONLanguageStatus {
return { schemas: this.jsonSchemaService.getSchemaURIsForResource(textDocument.uri, jsonDocument) };
}
}
let idCounter = 0;
function schemaAllowsComments(schemaRef: JSONSchemaRef): boolean | undefined {
if (schemaRef && typeof schemaRef === 'object') {
if (isBoolean(schemaRef.allowComments)) {
return schemaRef.allowComments;
}
if (schemaRef.allOf) {
for (const schema of schemaRef.allOf) {
const allow = schemaAllowsComments(schema);
if (isBoolean(allow)) {
return allow;
}
}
}
}
return undefined;
}
function schemaAllowsTrailingCommas(schemaRef: JSONSchemaRef): boolean | undefined {
if (schemaRef && typeof schemaRef === 'object') {
if (isBoolean(schemaRef.allowTrailingCommas)) {
return schemaRef.allowTrailingCommas;
}
const deprSchemaRef = schemaRef as any;
if (isBoolean(deprSchemaRef['allowsTrailingCommas'])) { // deprecated
return deprSchemaRef['allowsTrailingCommas'];
}
if (schemaRef.allOf) {
for (const schema of schemaRef.allOf) {
const allow = schemaAllowsTrailingCommas(schema);
if (isBoolean(allow)) {
return allow;
}
}
}
}
return undefined;
}
function toDiagnosticSeverity(severityLevel: SeverityLevel | undefined): DiagnosticSeverity | undefined {
switch (severityLevel) {
case 'error': return DiagnosticSeverity.Error;
case 'warning': return DiagnosticSeverity.Warning;
case 'ignore': return undefined;
}
return undefined;
}