Skip to content

Commit

Permalink
fix: add missing child selection of additionalItems: boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
sagold committed Dec 3, 2024
1 parent 03c91bb commit 0b7c747
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/jsonSchemaLibrary.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion dist/module/lib/getChildSchemaSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export default function getChildSchemaSelection(draft, property, schema = draft.
}
// array.items[] exceeded (or undefined), but additionalItems specified
if (schema.additionalItems && !isObject(schema.items)) {
return [draft.createNode(schema.additionalItems).resolveRef().schema];
// we fallback to a string if no schema is defined - might be subject for configuration
const additionalSchema = schema.additionalItems === true ? { type: "string" } : schema.additionalItems;
return [draft.createNode(additionalSchema).resolveRef().schema];
}
// array.items[] exceeded
if (Array.isArray(schema.items) && schema.items.length <= +property) {
Expand Down
5 changes: 4 additions & 1 deletion lib/getChildSchemaSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export default function getChildSchemaSelection(

// array.items[] exceeded (or undefined), but additionalItems specified
if (schema.additionalItems && !isObject(schema.items)) {
return [draft.createNode(schema.additionalItems).resolveRef().schema];
// we fallback to a string if no schema is defined - might be subject for configuration
const additionalSchema: JsonSchema =
schema.additionalItems === true ? { type: "string" } : schema.additionalItems;
return [draft.createNode(additionalSchema).resolveRef().schema];
}

// array.items[] exceeded
Expand Down
37 changes: 37 additions & 0 deletions test/unit/getChildSchemaSelection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,43 @@ describe("getChildSchemaSelection", () => {
});

describe("additionalItems", () => {
it("should return empty list if additionalItems is false", () => {
draft.setSchema({
type: "array",
items: [{ type: "string" }],
additionalItems: false
});

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

expect(result).to.have.length(0);
});

it("should return empty list if additionalItems is undefined", () => {
draft.setSchema({
type: "array",
items: [{ type: "string" }],
additionalItems: undefined
});

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

expect(result).to.have.length(0);
});

it("should return string-schema if additionalItems is true", () => {
draft.setSchema({
type: "array",
items: [{ type: "string" }],
additionalItems: true
});

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

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

it("should return additionalItem schema", () => {
draft.setSchema({
type: "array",
Expand Down

0 comments on commit 0b7c747

Please sign in to comment.