From 6f6f7d6135e47b603e1f5c39c7ebdd65d96991a1 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 10:20:35 +0000 Subject: [PATCH 1/4] ci: run GitHub Actions on Node 22 --- .github/workflows/run-tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yaml b/.github/workflows/run-tests.yaml index 48ee847..e5f62c1 100644 --- a/.github/workflows/run-tests.yaml +++ b/.github/workflows/run-tests.yaml @@ -18,7 +18,7 @@ jobs: - name: node uses: actions/setup-node@v4 with: - node-version: 20.9.0 + node-version: 22 - name: Deps run: | From 185315b46f45f56d6acc3e736dc94d9d28308493 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 11:13:07 +0000 Subject: [PATCH 2/4] build: drop baseUrl so the 'module' builtin doesn't resolve to ./module --- tsconfig.json | 1 - 1 file changed, 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 1f63d59..1bb8ddc 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,5 @@ { "compilerOptions": { - "baseUrl": ".", "rootDir": "src", "skipLibCheck": true, "declaration": true, From 41d4f150e92c371ff5b591770be934670007bd66 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 11:41:33 +0000 Subject: [PATCH 3/4] fix: remove a property when removing definitions empties its allOf/anyOf/oneOf --- src/index.ts | 11 +++++++++++ types/index.d.ts | 1 + 2 files changed, 12 insertions(+) diff --git a/src/index.ts b/src/index.ts index 55f1f6c..f75317d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -151,6 +151,7 @@ export class JSONSchemaPatch { const fullPath = currentPath + '/' + key; const op: Operation = { op: 'remove', path: fullPath }; applyPatch(this.schema, [op]); + this.removePropertyWithEmptyComposition(currentPath); } } // Recurse further into the object @@ -163,6 +164,16 @@ export class JSONSchemaPatch { findPaths(this.schema, ''); } + // Remove a property whose allOf/anyOf/oneOf list was emptied by a definition removal + private removePropertyWithEmptyComposition(compositionPath: string): void { + const match = compositionPath.match(/^(.*)\/properties\/([^/]+)\/(allOf|anyOf|oneOf)$/); + if (!match) return; + const composition = getValueAtPath(this.schema, compositionPath); + if (Array.isArray(composition) && composition.length === 0) { + this.removeProperty(match[1], match[2]); + } + } + // Add a property at a specified path addProperty(path: string, propertyName: string, property: any): void { path = path.trim() === '/' ? '' : path; diff --git a/types/index.d.ts b/types/index.d.ts index 5423f06..9c8f62d 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -15,6 +15,7 @@ export declare class JSONSchemaPatch { addDefinition(name: string, definition: any): void; removeDefinition(name: string): void; private removePropertiesUsingDefinition; + private removePropertyWithEmptyComposition; addProperty(path: string, propertyName: string, property: any): void; removeProperty(path: string, propertyName: string): void; private removeRequiredFromProperty; From 95384fd70b8bd8806d0b51c9d040079d486b272b Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 11:44:18 +0000 Subject: [PATCH 4/4] fix: keep a property's other rules and skip removed paths when removing composition refs --- __tests__/patch.remove-defns-adv2.test.ts | 38 +++++++++++++++++++++++ src/index.ts | 19 +++++++++--- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/__tests__/patch.remove-defns-adv2.test.ts b/__tests__/patch.remove-defns-adv2.test.ts index c247485..391c120 100644 --- a/__tests__/patch.remove-defns-adv2.test.ts +++ b/__tests__/patch.remove-defns-adv2.test.ts @@ -39,3 +39,41 @@ describe('JSONSchemaPatch - Remove Complex allOf References', () => { }); }); }); + +describe('JSONSchemaPatch - Remove definitions from compositions with other rules', () => { + it('keeps a property that still has other rules and its required entry', () => { + const schema = { + $defs: { asset: { type: "object" } }, + properties: { + item: { description: "an item", allOf: [{ $ref: "#/$defs/asset" }] } + }, + required: ["item"] + }; + new JSONSchemaPatch(schema).removeDefinition('asset'); + expect(schema).toEqual({ + $defs: {}, + properties: { item: { description: "an item" } }, + required: ["item"] + }); + }); + + it('handles several references to the same definition on one property', () => { + const schema = { + $defs: { asset: { type: "object" } }, + properties: { + item: { + allOf: [{ $ref: "#/$defs/asset" }], + anyOf: [{ $ref: "#/$defs/asset" }, { $ref: "#/$defs/asset" }] + }, + name: { type: "string" } + }, + required: ["item", "name"] + }; + new JSONSchemaPatch(schema).removeDefinition('asset'); + expect(schema).toEqual({ + $defs: {}, + properties: { name: { type: "string" } }, + required: ["name"] + }); + }); +}); diff --git a/src/index.ts b/src/index.ts index f75317d..841835e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -135,7 +135,11 @@ export class JSONSchemaPatch { // Helper method to remove properties that reference a specific definition private removePropertiesUsingDefinition(defName: string): void { const findPaths = (obj: any, currentPath: string) => { - Object.entries(obj).forEach(([key, value]) => { + const entries = Object.entries(obj); + // Walk arrays from the end so removing an item doesn't shift the ones still to visit + if (Array.isArray(obj)) entries.reverse(); + entries.forEach(([key, value]) => { + if (getValueAtPath(this.schema, currentPath) !== obj) return; if (typeof value === 'object' && value !== null) { // Check if the object has a $ref that matches the definition //@ts-ignore @@ -164,13 +168,18 @@ export class JSONSchemaPatch { findPaths(this.schema, ''); } - // Remove a property whose allOf/anyOf/oneOf list was emptied by a definition removal + // Drop an allOf/anyOf/oneOf list emptied by a definition removal; a property left with no rules is removed private removePropertyWithEmptyComposition(compositionPath: string): void { - const match = compositionPath.match(/^(.*)\/properties\/([^/]+)\/(allOf|anyOf|oneOf)$/); + const match = compositionPath.match(/^(.*)\/(allOf|anyOf|oneOf)$/); if (!match) return; const composition = getValueAtPath(this.schema, compositionPath); - if (Array.isArray(composition) && composition.length === 0) { - this.removeProperty(match[1], match[2]); + if (!Array.isArray(composition) || composition.length > 0) return; + const owner = getValueAtPath(this.schema, match[1]); + const property = match[1].match(/^(.*)\/properties\/([^/]+)$/); + if (property && Object.keys(owner).length === 1) { + this.removeProperty(property[1], property[2]); + } else { + applyPatch(this.schema, [{ op: 'remove', path: compositionPath }]); } }