Skip to content
Merged
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
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,6 @@ traverse(mySchema, (schemaOrSubschema) => {

https://json-schema-tools.github.io/traverse/

## Features Todo

- unevaluatedItems (https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.9.3.1.3)
- unevaluatedProperties (https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.9.3.2.4)
- contains (https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.9.3.1.4)
- propertyNames (https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.9.3.2.5)

### Contributing

How to contribute, build and release are outlined in [CONTRIBUTING.md](CONTRIBUTING.md), [BUILDING.md](BUILDING.md) and [RELEASING.md](RELEASING.md) respectively. Commits in this repository follow the [CONVENTIONAL_COMMITS.md](CONVENTIONAL_COMMITS.md) specification.
106 changes: 104 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,109 @@ describe("traverse", () => {
});
});

it("traverses contains", () => {
const testSchema: any = {
contains: { type: "string" }
};
const mockMutation = jest.fn((mockS) => mockS);

traverse(testSchema, mockMutation);

expect(mockMutation).nthCalledWith(
1,
testSchema.contains,
expect.anything(),
expect.anything(),
expect.anything(),
);
expect(mockMutation).nthCalledWith(
2,
testSchema,
expect.anything(),
expect.anything(),
undefined,
);

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

it("traverses propertyNames", () => {
const testSchema: any = {
propertyNames: { type: "string" }
};
const mockMutation = jest.fn((mockS) => mockS);

traverse(testSchema, mockMutation);

expect(mockMutation).nthCalledWith(
1,
testSchema.propertyNames,
expect.anything(),
expect.anything(),
expect.anything(),
);
expect(mockMutation).nthCalledWith(
2,
testSchema,
expect.anything(),
expect.anything(),
undefined,
);

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

it("traverses unevaluatedItems as boolean", () => {
const testSchema: any = {
unevaluatedItems: false,
};
const mockMutation = jest.fn((mockS) => mockS);

traverse(testSchema, mockMutation);

expect(mockMutation).nthCalledWith(
1,
testSchema.unevaluatedItems,
expect.anything(),
expect.anything(),
expect.anything(),
);
expect(mockMutation).nthCalledWith(
2,
testSchema,
expect.anything(),
expect.anything(),
undefined,
);

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

it("traverses unevaluatedProperties as schema", () => {
const testSchema: any = {
unevaluatedProperties: { type: "string" },
};
const mockMutation = jest.fn((mockS) => mockS);

traverse(testSchema, mockMutation);

expect(mockMutation).nthCalledWith(
1,
testSchema.unevaluatedProperties,
expect.anything(),
expect.anything(),
expect.anything(),
);
expect(mockMutation).nthCalledWith(
2,
testSchema,
expect.anything(),
expect.anything(),
undefined,
);

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

describe("schema.type being an array", () => {
it("allows type to be an array", () => {
Expand Down Expand Up @@ -1041,8 +1144,6 @@ describe("traverse", () => {
testCalls(mockMutation, testSchema.properties.foo.items[1], false, 4);
});
});
});

describe("Mutability settings", () => {
it("defaults to being immutable", () => {
const s = {
Expand Down Expand Up @@ -1215,3 +1316,4 @@ describe("Mutability settings", () => {
});
});
});
});
28 changes: 28 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,20 @@ export default function traverse(
);
}

if (schema.contains !== undefined) {
mutableSchema.contains = rec(
schema.contains,
[...pathStack, "contains"],
);
}

if (schema.unevaluatedItems !== undefined) {
mutableSchema.unevaluatedItems = rec(
schema.unevaluatedItems,
[...pathStack, "unevaluatedItems"],
);
}

if (schema.properties !== undefined) {
const sProps: { [key: string]: JSONSchema } = schema.properties;
const mutableProps: { [key: string]: JSONSchema } = {};
Expand All @@ -242,6 +256,20 @@ export default function traverse(
if (schema.additionalProperties !== undefined && !!schema.additionalProperties === true) {
mutableSchema.additionalProperties = rec(schema.additionalProperties, [...pathStack, "additionalProperties"]);
}

if (schema.propertyNames !== undefined) {
mutableSchema.propertyNames = rec(
schema.propertyNames,
[...pathStack, "propertyNames"],
);
}

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

if (opts.skipFirstMutation === true && depth === 0) {
Expand Down
Loading