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

Enforce array schema #151

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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/hot-maps-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"codemirror-json-schema": patch
---

Fixed validation bugs: single objects incorrectly passed array schemas, invalid YAML caused errors after root-level change(now skipped if unparseable), and added tests ensuring non-array values(object, boolean, string, number) are correctly rejected.
7 changes: 7 additions & 0 deletions src/features/__tests__/__fixtures__/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,10 @@ export const wrappedTestSchemaConditionalPropertiesOnSameObject = {
},
required: ["original"],
} as JSONSchema7;

export const testSchemaArrayOfObjects = {
type: "array",
items: {
type: "object",
},
} as JSONSchema7;
75 changes: 69 additions & 6 deletions src/features/__tests__/json-validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import type { Diagnostic } from "@codemirror/lint";
import { describe, it, expect } from "vitest";
import { EditorView } from "@codemirror/view";

import { testSchema, testSchema2 } from "./__fixtures__/schemas";
import {
testSchema,
testSchema2,
testSchemaArrayOfObjects,
} from "./__fixtures__/schemas";
import { JSONMode } from "../../types";
import { getExtensions } from "./__helpers__/index";
import { MODES } from "../../constants";

const getErrors = (
jsonString: string,
mode: JSONMode,
schema?: JSONSchema7
schema?: JSONSchema7,
) => {
const view = new EditorView({
doc: jsonString,
Expand All @@ -30,13 +34,13 @@ const expectErrors = (
jsonString: string,
errors: [from: number | undefined, to: number | undefined, message: string][],
mode: JSONMode,
schema?: JSONSchema7
schema?: JSONSchema7,
) => {
const filteredErrors = getErrors(jsonString, mode, schema).map(
({ renderMessage, ...error }) => error
({ renderMessage, ...error }) => error,
);
expect(filteredErrors).toEqual(
errors.map(([from, to, message]) => ({ ...common, from, to, message }))
errors.map(([from, to, message]) => ({ ...common, from, to, message })),
);
};

Expand Down Expand Up @@ -145,6 +149,65 @@ describe("json-validation", () => {
],
schema: testSchema2,
},
{
name: "reject a single object when schema expects an array",
mode: MODES.JSON,
doc: '{ "name": "John" }',
errors: [
{
from: 0,
to: 0,
message: "Expected `array` but received `object`",
},
],
schema: testSchemaArrayOfObjects,
},
{
name: "reject a boolean when schema expects an array",
mode: MODES.JSON,
doc: "true",
errors: [
{
from: 0,
to: 0,
message: "Expected `array` but received `boolean`",
},
],
schema: testSchemaArrayOfObjects,
},
{
name: "reject a string when schema expects an array",
mode: MODES.JSON,
doc: '"example"',
errors: [
{
from: 0,
to: 0,
message: "Expected `array` but received `string`",
},
],
schema: testSchemaArrayOfObjects,
},
{
name: "reject a number when schema expects an array",
mode: MODES.JSON,
doc: "123",
errors: [
{
from: 0,
to: 0,
message: "Expected `array` but received `number`",
},
],
schema: testSchemaArrayOfObjects,
},
{
name: "can handle an array of objects",
mode: MODES.JSON,
doc: '[{"name": "John"}, {"name": "Jane"}]',
errors: [],
schema: testSchemaArrayOfObjects,
},
];
it.each([
...jsonSuite,
Expand Down Expand Up @@ -360,7 +423,7 @@ oneOfEg2: 123
doc,
errors.map((error) => [error.from, error.to, error.message]),
mode,
schema
schema,
);
});
});
7 changes: 5 additions & 2 deletions src/features/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class JSONValidation {
if (error.code === "one-of-error" && errors?.length) {
return `Expected one of ${joinWithOr(
errors,
(data) => data.data.expected
(data) => data.data.expected,
)}`;
}
if (error.code === "type-error") {
Expand Down Expand Up @@ -119,6 +119,8 @@ export class JSONValidation {
if (!text?.length) return [];

const json = this.parser(view.state);
// skip validation if parsing fails
if (json.data == null) return [];

let errors: JsonError[] = [];
try {
Expand Down Expand Up @@ -147,7 +149,8 @@ export class JSONValidation {
const pointer = json.pointers.get(errorPath) as JSONPointerData;
if (
error.name === "MaxPropertiesError" ||
error.name === "MinPropertiesError"
error.name === "MinPropertiesError" ||
errorPath === "" // root level type errors
) {
pushRoot();
} else if (pointer) {
Expand Down