Skip to content
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

Handle generic validation error #103

Merged
merged 5 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/weak-bobcats-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"codemirror-json-schema": patch
---

Handle generic validation error
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
export { jsonCompletion } from "./json-completion.js";
export {
jsonCompletion,
JSONCompletion,
type JSONCompletionOptions,
} from "./json-completion.js";

export {
jsonSchemaLinter,
JSONValidation,
type JSONValidationOptions,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome, this takes care of another ticket i think

handleRefresh,
} from "./json-validation.js";

export {
jsonSchemaHover,
JSONHover,
type HoverOptions,
type FoundCursorData,
type CursorData,
Expand Down
19 changes: 11 additions & 8 deletions src/json-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { Draft04, type Draft, type JsonError } from "json-schema-library";

import { getJSONSchema, schemaStateField } from "./state.js";
import { joinWithOr } from "./utils/formatting.js";
import { JSONMode, JSONPointerData } from "./types.js";
import { JSONMode, JSONPointerData, RequiredPick } from "./types.js";
import { parseJSONDocumentState } from "./utils/parseJSONDocument.js";
import { RequiredPick } from "./types.js";
import { el } from "./utils/dom.js";
import { renderMarkdown } from "./utils/markdown.js";
import { MODES } from "./constants.js";
import { parseYAMLDocumentState } from "./utils/parse-yaml-document.js";
import { parseJSON5DocumentState } from "./utils/parseJSON5Document.js";
import { debug } from "./utils/debug.js";

const getDefaultParser = (mode: JSONMode): typeof parseJSONDocumentState => {
switch (mode) {
Expand Down Expand Up @@ -96,7 +96,7 @@ export class JSONValidation {
const errors = errorData?.errors as string[];
if (error.code === "one-of-error" && errors?.length) {
return `Expected one of ${joinWithOr(
errors as string[],
errors,
(data) => data.data.expected
)}`;
}
Expand All @@ -110,6 +110,7 @@ export class JSONValidation {
const message = error.message
// don't mention root object
.replaceAll("in `#` ", "")
.replaceAll("at `#`", "")
.replaceAll("/", ".")
.replaceAll("#.", "");
return message;
Expand All @@ -135,9 +136,10 @@ export class JSONValidation {
try {
errors = this.schema.validate(json.data);
} catch {}
debug.log("xxx", "validation errors", errors, json.data);
if (!errors.length) return [];
// reduce() because we want to filter out errors that don't have a pointer
return errors.reduce((acc, error) => {
return errors.reduce<Diagnostic[]>((acc, error) => {
const pushRoot = () => {
const errorString = this.rewriteError(error);
acc.push({
Expand All @@ -156,12 +158,11 @@ export class JSONValidation {
const errorPath = getErrorPath(error);
const pointer = json.pointers.get(errorPath) as JSONPointerData;
if (
error.name === "MaxPropertiesError" ??
error.name === "MaxPropertiesError" ||
error.name === "MinPropertiesError"
) {
pushRoot();
}
if (pointer) {
} else if (pointer) {
// if the error is a property error, use the key position
const isKeyError = positionalErrors.includes(error.name);
const errorString = this.rewriteError(error);
Expand All @@ -182,8 +183,10 @@ export class JSONValidation {
source: this.schemaTitle,
});
}
} else {
pushRoot();
}
return acc;
}, [] as Diagnostic[]);
}, []);
}
}
Loading