Registry / testing / json-schema-patch

json-schema-patch

JSON →
library1.0.2jsnpmunverified

A library for dynamically modifying JSON Schema documents by applying add, remove, and replace operations on properties and definitions. Version 1.0.2 is the current stable release, with no frequent release cadence. Unlike manual schema manipulation, it automatically handles cleanup of $ref references when definitions are removed. Ships TypeScript types.

npm install json-schema-patch
INSTALL
IMPORT
SIG · JSON-SCHEMA-PATCH
J
json-schema-patch
testingjavascriptv1.0.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

JSONSchemaPatch
import { JSONSchemaPatch } from 'json-schema-patch'
const JSONSchemaPatch = require('json-schema-patch')
ESM-only; CommonJS require is not supported.
PatchOperation
import type { PatchOperation } from 'json-schema-patch'
import { PatchOperation } from 'json-schema-patch'
Use type import for PatchOperation type as it is only a type.
JSONSchemaPatchOptions
import type { JSONSchemaPatchOptions } from 'json-schema-patch'
import { JSONSchemaPatchOptions } from 'json-schema-patch'
Options interface, type-only.

Demonstrates instantiation of JSONSchemaPatch, adding/removing definitions and properties, and applying the patch to modify the schema in place.

import { JSONSchemaPatch } from 'json-schema-patch'; const schema = { $defs: { asset: { type: 'object', properties: { id: { type: 'number' } } }, product: { type: 'object', properties: { name: { type: 'string' } } } }, properties: { name: { type: 'string' }, item: { allOf: [ { $ref: '#/$defs/asset' }, { $ref: '#/$defs/product' } ] }, equipment: { allOf: [ { $ref: '#/$defs/asset' } ] } }, required: ['name', 'item', 'equipment'] }; const patcher = new JSONSchemaPatch(schema); // Add a new definition patcher.addDefinition('newDef', { type: 'object', properties: { newProp: { type: 'string' } } }); // Remove a definition and clean up references patcher.removeDefinition('asset'); // Add a new property patcher.addProperty('/properties', 'newProp', { type: 'number' }); // Remove a property patcher.removeProperty('/properties', 'item'); // Apply all prepared operations and finalize changes patcher.applyPatch(); console.log(schema);
Debug
Known issues
breakingThe `applyPatch()` method mutates the original schema object passed to the constructor; it does not return a new copy.
fix
Clone the schema before passing it to JSONSchemaPatch if you need to preserve the original.
affects: >=1.0.0
gotchaOperations are queued until `applyPatch()` is called; calling accessors like `schema.properties` before applying will not reflect queued changes.
fix
Always call `applyPatch()` before reading the modified schema.
affects: >=1.0.0
gotchaThe `addProperty` path must start with a slash and refer to an existing object node; otherwise it may throw or cause unexpected behavior.
fix
Ensure the path is valid (e.g., '/properties') and the parent property exists.
affects: >=1.0.0
breakingRemoving a definition does not remove the $defs entry itself if there are still references; it only cleans up if references become zero after removal.
fix
Verify that no remaining $ref targets the definition after removal; otherwise it will persist.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: JSONSchemaPatch is not a constructor
Using CommonJS require instead of ESM import.
fix
Change const JSONSchemaPatch = require('json-schema-patch') to import { JSONSchemaPatch } from 'json-schema-patch'.
Cannot find module 'json-schema-patch'
Package not installed, or import path incorrect.
fix
Run npm install json-schema-patch and ensure import uses the correct package name.
Property 'addDefinition' does not exist on type 'JSONSchemaPatch'
Using TypeScript but not importing the type correctly, or using an older version without this method.
fix
Ensure you have version 1.0.0 or later, and import the class correctly: import { JSONSchemaPatch } from 'json-schema-patch'.
Cannot read properties of undefined (reading 'applyPatch')
JSONSchemaPatch was not instantiated (e.g., missing new keyword).
fix
Instantiate with new: const patcher = new JSONSchemaPatch(schema);
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
json-schema-patch — npm install json-schema-patch · libregistry