Provide a usage example in the readme. Fixes #39

This commit is contained in:
Martin Aeschlimann 2020-11-12 10:49:38 +01:00
Родитель f07b3902c5
Коммит 9d41ca8362
4 изменённых файлов: 66 добавлений и 4 удалений

Просмотреть файл

@ -2,6 +2,7 @@
.github/
build/
lib/**/test/
lib/**/example/
lib/**/*.js.map
lib/*/*/*.d.ts
src/

61
src/example/sample.ts Normal file
Просмотреть файл

@ -0,0 +1,61 @@
import { getLanguageService, JSONSchema, SchemaRequestService, TextDocument, MatchingSchema } from '../jsonLanguageService';
async function main() {
const jsonContentUri = 'foo://server/example.data.json';
const jsonContent =
`{
"name": 12
"country": "Ireland"
}`;
const jsonSchemaUri = "foo://server/data.schema.json";
const jsonSchema = {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"country": {
"type": "string",
"enum": ["Ireland", "Iceland"]
}
}
};
const textDocument = TextDocument.create(jsonContentUri, 'json', 1, jsonContent);
const jsonLanguageService = getLanguageService({
schemaRequestService: (uri) => {
if (uri === jsonSchemaUri) {
return Promise.resolve(JSON.stringify(jsonSchema));
}
return Promise.reject(`Unabled to load schema at ${uri}`);
}
});
// associate `*.data.json` with the `foo://server/data.schema.json` schema
jsonLanguageService.configure({ allowComments: false, schemas: [{ fileMatch: ["*.data.json"], uri: jsonSchemaUri }] });
const jsonDocument = jsonLanguageService.parseJSONDocument(textDocument);
const diagnostics = await jsonLanguageService.doValidation(textDocument, jsonDocument);
console.log('Validation results:', diagnostics.map(d => `[line ${d.range.start.line}] ${d.message}`));
/*
* > Validation results: [
* > '[line 1] Incorrect type. Expected "string".',
* > '[line 2] Expected comma'
* > ]
*/
const competionResult = await jsonLanguageService.doComplete(textDocument, { line: 2, character: 18 }, jsonDocument);
console.log('Completion proposals:', competionResult?.items.map(i => `${i.label}`));
/*
* Completion proposals: [ '"Ireland"', '"Iceland"' ]
*/
}
main();

Просмотреть файл

@ -163,8 +163,8 @@ export interface WorkspaceContextService {
resolveRelativePath(relativePath: string, resource: string): string;
}
/**
* The schema request service is used to fetch schemas. The result should the schema file comment, or,
* in case of an error, a displayable error string
* The schema request service is used to fetch schemas. If successful, returns a resolved promise with the content of the schema.
* In case of an error, returns a rejected promise with a displayable error string.
*/
export interface SchemaRequestService {
(uri: string): Thenable<string>;

Просмотреть файл

@ -9,7 +9,7 @@ import { JSONDocument } from '../parser/jsonParser';
import { TextDocument, ErrorCode, PromiseConstructor, Thenable, LanguageSettings, DocumentLanguageSettings, SeverityLevel, Diagnostic, DiagnosticSeverity, Range } from '../jsonLanguageTypes';
import * as nls from 'vscode-nls';
import { JSONSchemaRef, JSONSchema } from '../jsonSchema';
import { isDefined, isBoolean } from '../utils/objects';
import { isBoolean } from '../utils/objects';
const localize = nls.loadMessageBundle();
@ -29,7 +29,7 @@ export class JSONValidation {
public configure(raw: LanguageSettings) {
if (raw) {
this.validationEnabled = raw.validate;
this.validationEnabled = raw.validate !== false;
this.commentSeverity = raw.allowComments ? undefined : DiagnosticSeverity.Error;
}
}