Skip to content

Commit

Permalink
Merge pull request #519 from json-schema-tools/fix/additional-items-t…
Browse files Browse the repository at this point in the history
…raversed

fix: additionalItems should always be traversed
  • Loading branch information
BelfordZ authored Oct 6, 2022
2 parents 26934b1 + cf5b7f9 commit 1c4c45f
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 39 deletions.
8 changes: 2 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ aliases:

defaults: &defaults
working_directory: ~/project
docker:
- image: cimg/node:16.17.0

jobs:
test:
<<: *defaults
docker:
- image: circleci/node:16
steps:
- checkout
- restore_cache: *restore-deps-cache
Expand All @@ -41,8 +41,6 @@ jobs:

build:
<<: *defaults
docker:
- image: circleci/node:16
steps:
- checkout
- restore_cache: *restore-deps-cache
Expand All @@ -53,8 +51,6 @@ jobs:

release:
<<: *defaults
docker:
- image: circleci/node:16
steps:
- checkout
- restore_cache: *restore-deps-cache
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"build": "tsc",
"build:docs": "typedoc",
"test": "jest --coverage",
"lint": "eslint . --ext .ts"
"lint": "eslint . --ext .ts --fix"
},
"author": "Zachary Belford<belfordz66@gmail.com>",
"license": "Apache-2.0",
Expand Down
124 changes: 98 additions & 26 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,41 +278,113 @@ describe("traverse", () => {
expect(mockMutation).toHaveBeenCalledTimes(4);
});

it("traverses additionalItems as boolean", () => {
const testSchema: any = {
additionalItems: true
};
const mockMutation = jest.fn((mockS) => mockS);
describe("additionalItems", () => {
it("as a boolean schema true", () => {
const testSchema: any = {
additionalItems: true
};
const mockMutation = jest.fn((mockS) => mockS);

traverse(testSchema, mockMutation);
traverse(testSchema, mockMutation);

testCalls(mockMutation, testSchema.additionalItems);
testCalls(mockMutation, testSchema);
testCalls(mockMutation, testSchema.additionalItems);
testCalls(mockMutation, testSchema);

expect(mockMutation).toHaveBeenCalledTimes(2);
});
expect(mockMutation).toHaveBeenCalledTimes(2);
});

it("traverses additionalItems as schema", () => {
const testSchema: any = {
additionalItems: {
properties: {
c: {},
d: {},
it("as a boolean schema false", () => {
const testSchema: any = {
additionalItems: false
};
const mockMutation = jest.fn((mockS) => mockS);

traverse(testSchema, mockMutation);

testCalls(mockMutation, testSchema.additionalItems);
testCalls(mockMutation, testSchema);

expect(mockMutation).toHaveBeenCalledTimes(2);
});

it("as a boolean schema true: items is array", () => {
const testSchema: any = {
items: [{ type: "string" }],
additionalItems: true
};
const mockMutation = jest.fn((mockS) => mockS);

traverse(testSchema, mockMutation);

testCalls(mockMutation, testSchema.additionalItems);
testCalls(mockMutation, testSchema.items[0]);
testCalls(mockMutation, testSchema);

expect(mockMutation).toHaveBeenCalledTimes(3);
});

it("as a boolean schema false: items is single schema", () => {
const testSchema: any = {
items: { type: "string" },
additionalItems: false
};
const mockMutation = jest.fn((mockS) => mockS);

traverse(testSchema, mockMutation);

testCalls(mockMutation, testSchema.additionalItems);
testCalls(mockMutation, testSchema.items);
testCalls(mockMutation, testSchema);

expect(mockMutation).toHaveBeenCalledTimes(3);
});

it("schema with nested subschemas: items is array", () => {
const testSchema: any = {
items: [{ type: "string" }],
additionalItems: {
properties: {
c: {},
d: {},
},
},
},
};
const mockMutation = jest.fn((mockS) => mockS);
};
const mockMutation = jest.fn((mockS) => mockS);

traverse(testSchema, mockMutation);
traverse(testSchema, mockMutation);

testCalls(mockMutation, testSchema.additionalItems);
testCalls(mockMutation, testSchema.additionalItems.properties.c);
testCalls(mockMutation, testSchema.additionalItems.properties.d);
testCalls(mockMutation, testSchema);
testCalls(mockMutation, testSchema.additionalItems);
testCalls(mockMutation, testSchema.items[0]);
testCalls(mockMutation, testSchema.additionalItems.properties.c);
testCalls(mockMutation, testSchema.additionalItems.properties.d);
testCalls(mockMutation, testSchema);

expect(mockMutation).toHaveBeenCalledTimes(4);
});
expect(mockMutation).toHaveBeenCalledTimes(5);
});

it("schema with nested subschemas: items is single schema", () => {
const testSchema: any = {
items: { type: "string" },
additionalItems: {
properties: {
c: {},
d: {},
},
},
};
const mockMutation = jest.fn((mockS) => mockS);

traverse(testSchema, mockMutation);

testCalls(mockMutation, testSchema.additionalItems);
testCalls(mockMutation, testSchema.items);
testCalls(mockMutation, testSchema.additionalItems.properties.c);
testCalls(mockMutation, testSchema.additionalItems.properties.d);
testCalls(mockMutation, testSchema);

expect(mockMutation).toHaveBeenCalledTimes(5);
});
});
});


Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,6 @@ export default function traverse(
return result;
});
} else {
let itemsIsSingleSchema = false;

if (schema.items) {
if (schema.items instanceof Array) {
mutableSchema.items = schema.items.map((x, i) => {
Expand All @@ -215,7 +213,6 @@ export default function traverse(
mutableSchema.items = cycledMutableSchema;
}
} else {
itemsIsSingleSchema = true;
mutableSchema.items = traverse(
schema.items,
mutation,
Expand All @@ -230,8 +227,11 @@ export default function traverse(
}
}

if (schema.additionalItems !== undefined && !!schema.additionalItems === true && !itemsIsSingleSchema) {
mutableSchema.additionalItems = rec(schema.additionalItems, [...pathStack, "additionalItems"]);
if (schema.additionalItems !== undefined) {
mutableSchema.additionalItems = rec(
schema.additionalItems,
[...pathStack, "additionalItems"]
);
}

if (schema.properties !== undefined) {
Expand Down
2 changes: 1 addition & 1 deletion src/parent.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import traverse from "./";
import { JSONSchema, JSONSchemaObject } from "@json-schema-tools/meta-schema";
import { JSONSchema } from "@json-schema-tools/meta-schema";

describe("traverse parent", () => {
const test = (s: JSONSchema, parents: JSONSchema[]) => {
Expand Down

0 comments on commit 1c4c45f

Please sign in to comment.