Skip to content

Commit

Permalink
add: support for additionalItems in getChildSchemaSelection
Browse files Browse the repository at this point in the history
  • Loading branch information
sagold committed Dec 3, 2024
1 parent f846574 commit f154564
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
19 changes: 16 additions & 3 deletions lib/getChildSchemaSelection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Draft } from "./draft";
import { isJsonError, JsonError, JsonSchema } from "./types";
import { isObject } from "./utils/isObject";

/**
* Returns a list of possible child-schemas for the given property key. In case of a oneOf selection, multiple schemas
Expand All @@ -20,17 +21,29 @@ export default function getChildSchemaSelection(
return schema.oneOf.map((item: JsonSchema) => draft.createNode(item).resolveRef().schema);
}
if (schema.items?.oneOf) {
return schema.items.oneOf.map(
(item: JsonSchema) => draft.createNode(item).resolveRef().schema
);
return schema.items.oneOf.map((item: JsonSchema) => draft.createNode(item).resolveRef().schema);
}

// array.items[] found
if (Array.isArray(schema.items) && schema.items.length > +property) {
return [draft.step(draft.createNode(schema), property, {}).schema];
}

// array.items[] exceeded (or undefined), but additionalItems specified
if (schema.additionalItems && !isObject(schema.items)) {
return [draft.createNode(schema.additionalItems).resolveRef().schema];
}

// array.items[] exceeded
if (Array.isArray(schema.items) && schema.items.length <= +property) {
return [];
}

const node = draft.step(draft.createNode(schema), property, {});
if (isJsonError(node)) {
const error: JsonError = node;
return error;
}

return [node.schema];
}
55 changes: 54 additions & 1 deletion test/unit/getChildSchemaSelection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from "chai";
import { JsonEditor as Core } from "../../lib/jsoneditor";
import getChildSchemaSelection from "../../lib/getChildSchemaSelection";

describe("getChildSchemaSelection", () => {
describe.only("getChildSchemaSelection", () => {
let draft: Core;
before(() => (draft = new Core()));

Expand Down Expand Up @@ -68,4 +68,57 @@ describe("getChildSchemaSelection", () => {
expect(result).to.have.length(2);
expect(result).to.deep.eq([{ type: "string" }, { type: "number" }]);
});

describe("additionalItems", () => {
it("should return additionalItem schema", () => {
draft.setSchema({
type: "array",
additionalItems: { id: "number", type: "number", default: 2 }
});

const result = getChildSchemaSelection(draft, 1, draft.getSchema());

expect(result).to.have.length(1);
expect(result).to.deep.eq([{ id: "number", type: "number", default: 2 }]);
});

it("should return additionalItem schema when items-list is exceeded", () => {
draft.setSchema({
type: "array",
items: [{ type: "string" }],
additionalItems: { id: "number", type: "number", default: 2 }
});

const result = getChildSchemaSelection(draft, 1, draft.getSchema());

expect(result).to.have.length(1);
expect(result).to.deep.eq([{ id: "number", type: "number", default: 2 }]);
});

it("should return items-schema instead of additionalItems if item is defined", () => {
draft.setSchema({
type: "array",
items: [{ type: "string" }, { type: "string" }],
additionalItems: { id: "number", type: "number", default: 2 }
});

const result = getChildSchemaSelection(draft, 1, draft.getSchema());

expect(result).to.have.length(1);
expect(result).to.deep.eq([{ type: "string" }]);
});

it("should not return additionalItems if item-schema is object", () => {
draft.setSchema({
type: "array",
items: { type: "string" },
additionalItems: { id: "number", type: "number", default: 2 }
});

const result = getChildSchemaSelection(draft, 1, draft.getSchema());

expect(result).to.have.length(1);
expect(result).to.deep.eq([{ type: "string" }]);
});
});
});

0 comments on commit f154564

Please sign in to comment.