Skip to content

Commit

Permalink
Handle generic validation error (#103)
Browse files Browse the repository at this point in the history
Currently we don't return validation errors when we don't have valid
corresponding pointers. However we wouldn't always have valid pointers
e.g. if the data isn't a valid JSON object (see
#98)

Here I just `pushRoot` in those cases. We can consider adding the
default root `#` pointer but the effect will be the same (i.e. by
default all entries will _at least_ match the root pointer and so will
set the range to the root)
  • Loading branch information
imolorhe authored May 30, 2024
1 parent 5e95143 commit da7f368
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
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,
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[]);
}, []);
}
}

0 comments on commit da7f368

Please sign in to comment.