-
Notifications
You must be signed in to change notification settings - Fork 1
Add Diagnostic provider to Schema Validation, improve JSON Pointer handling and add Dialect support #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add Diagnostic provider to Schema Validation, improve JSON Pointer handling and add Dialect support #7
Changes from all commits
0e84906
2f56f06
040ce28
b4bd2fa
2e71f68
9604708
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,67 @@ | ||||||
| import { Diagnostic, DiagnosticSeverity } from "vscode-languageserver"; | ||||||
| import { TextDocument } from "vscode-languageserver-textdocument"; | ||||||
| import * as jsonc from "jsonc-parser"; | ||||||
| import { validate } from "@hyperjump/json-schema-errors"; | ||||||
| import { pointerSegments } from "@hyperjump/json-pointer"; | ||||||
|
|
||||||
| import type { ErrorObject } from "@hyperjump/json-schema-errors"; | ||||||
| import type { DiagnosticsProvider } from "./Diagnostics.ts"; | ||||||
|
|
||||||
| export class SchemaValidation implements DiagnosticsProvider { | ||||||
| async getDiagnostics(textDocument: TextDocument) { | ||||||
| const text = textDocument.getText(); | ||||||
| const parseErrors: jsonc.ParseError[] = []; | ||||||
|
|
||||||
| const tree = jsonc.parseTree(text, parseErrors); | ||||||
| const schemaDiagnostics: Diagnostic[] = []; | ||||||
|
|
||||||
| const schemaNode = tree ? jsonc.findNodeAtLocation(tree, ["$schema"]) : undefined; | ||||||
| const schemaUri = schemaNode?.value; | ||||||
|
|
||||||
| // skip schema validation if there are syntax errors hence the parseError.length check | ||||||
| if (schemaUri && parseErrors.length === 0) { | ||||||
| let instance = JSON.parse(text); | ||||||
| const result = await validate(schemaUri, instance); | ||||||
|
|
||||||
| if (!result.valid) { | ||||||
| const errors = result.errors; | ||||||
| errors.forEach((error) => { | ||||||
| const node = tree ? findNodeByPointer(tree, error.instanceLocation) : undefined; | ||||||
|
|
||||||
| if (node) { | ||||||
| schemaDiagnostics.push({ | ||||||
| severity: DiagnosticSeverity.Error, | ||||||
| range: { | ||||||
| start: textDocument.positionAt(node.offset), | ||||||
| end: textDocument.positionAt(node.offset + node.length) | ||||||
| }, | ||||||
| message: formatError(error), | ||||||
| source: "json-language-server" | ||||||
| }); | ||||||
| } | ||||||
| }); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return schemaDiagnostics; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| const findNodeByPointer = (node: jsonc.Node, pointer: string) => { | ||||||
| if (pointer === "#") { | ||||||
| return node; | ||||||
| } | ||||||
| const segments = [...pointerSegments(pointer.slice(1))]; | ||||||
| const path = segments.map((s) => /^\d+$/.test(s) ? parseInt(s) : s); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't quite right. Consider, {
"42": ["foo"]
}Given the pointer, So, you won't be able to use |
||||||
| return jsonc.findNodeAtLocation(node, path); | ||||||
| }; | ||||||
|
|
||||||
| const formatError = (error: ErrorObject, depth = 0): string => { | ||||||
| let msg = error.message; | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's best to avoid abbreviations.
Suggested change
|
||||||
| if (error.alternatives && error.alternatives.length > 0) { | ||||||
| const indent = " ".repeat(depth + 1); | ||||||
| const lines = error.alternatives.flatMap((alt) => alt.map((subErr) => `${indent}- ${formatError(subErr, depth + 1)}`)); | ||||||
| msg += `:\n${lines.join("\n")}`; | ||||||
| } | ||||||
| return msg; | ||||||
| }; | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remember that
pointeris a URI and could have URI escaped characters that need to be decoded.pointerSegmentswill only handle JSON Pointer escaped characters, not URI escaped characters.