{"id":48282,"library":"json-schema-patch","title":"JSONSchemaPatch","description":"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.","status":"active","version":"1.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/cosmology-tech/json-schema-patch","tags":["javascript","typescript"],"install":[{"cmd":"npm install json-schema-patch","lang":"bash","label":"npm"},{"cmd":"yarn add json-schema-patch","lang":"bash","label":"yarn"},{"cmd":"pnpm add json-schema-patch","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"ESM-only; CommonJS require is not supported.","wrong":"const JSONSchemaPatch = require('json-schema-patch')","symbol":"JSONSchemaPatch","correct":"import { JSONSchemaPatch } from 'json-schema-patch'"},{"note":"Use type import for PatchOperation type as it is only a type.","wrong":"import { PatchOperation } from 'json-schema-patch'","symbol":"PatchOperation","correct":"import type { PatchOperation } from 'json-schema-patch'"},{"note":"Options interface, type-only.","wrong":"import { JSONSchemaPatchOptions } from 'json-schema-patch'","symbol":"JSONSchemaPatchOptions","correct":"import type { JSONSchemaPatchOptions } from 'json-schema-patch'"}],"quickstart":{"code":"import { JSONSchemaPatch } from 'json-schema-patch';\n\nconst schema = {\n  $defs: {\n    asset: { type: 'object', properties: { id: { type: 'number' } } },\n    product: { type: 'object', properties: { name: { type: 'string' } } }\n  },\n  properties: {\n    name: { type: 'string' },\n    item: {\n      allOf: [\n        { $ref: '#/$defs/asset' },\n        { $ref: '#/$defs/product' }\n      ]\n    },\n    equipment: {\n      allOf: [\n        { $ref: '#/$defs/asset' }\n      ]\n    }\n  },\n  required: ['name', 'item', 'equipment']\n};\n\nconst patcher = new JSONSchemaPatch(schema);\n\n// Add a new definition\npatcher.addDefinition('newDef', { type: 'object', properties: { newProp: { type: 'string' } } });\n\n// Remove a definition and clean up references\npatcher.removeDefinition('asset');\n\n// Add a new property\npatcher.addProperty('/properties', 'newProp', { type: 'number' });\n\n// Remove a property\npatcher.removeProperty('/properties', 'item');\n\n// Apply all prepared operations and finalize changes\npatcher.applyPatch();\n\nconsole.log(schema);","lang":"typescript","description":"Demonstrates instantiation of JSONSchemaPatch, adding/removing definitions and properties, and applying the patch to modify the schema in place."},"warnings":[{"fix":"Clone the schema before passing it to JSONSchemaPatch if you need to preserve the original.","message":"The `applyPatch()` method mutates the original schema object passed to the constructor; it does not return a new copy.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Always call `applyPatch()` before reading the modified schema.","message":"Operations are queued until `applyPatch()` is called; calling accessors like `schema.properties` before applying will not reflect queued changes.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure the path is valid (e.g., '/properties') and the parent property exists.","message":"The `addProperty` path must start with a slash and refer to an existing object node; otherwise it may throw or cause unexpected behavior.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Verify that no remaining $ref targets the definition after removal; otherwise it will persist.","message":"Removing a definition does not remove the $defs entry itself if there are still references; it only cleans up if references become zero after removal.","severity":"breaking","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'1.0.2':22 'add':12 'appli':11 'automat':38 'cadenc':32 'cleanup':40 'current':25 'definit':20,45 'document':9 'dynam':5 'frequent':30 'handl':39 'javascript':51 'json':7 'jsonschemapatch':1 'librari':3 'manipul':36 'manual':34 'modifi':6 'oper':16 'properti':18 'ref':42 'refer':43 'releas':27,31 'remov':13,47 'replac':15 'schema':8,35 'ship':48 'stabl':26 'type':50 'typescript':49,52 'unlik':33 'version':21","created_at":"2026-06-07T16:55:50.037902+00:00","updated_at":"2026-06-07T16:55:50.037902+00:00","problems":[{"fix":"Change const JSONSchemaPatch = require('json-schema-patch') to import { JSONSchemaPatch } from 'json-schema-patch'.","cause":"Using CommonJS require instead of ESM import.","error":"TypeError: JSONSchemaPatch is not a constructor"},{"fix":"Run npm install json-schema-patch and ensure import uses the correct package name.","cause":"Package not installed, or import path incorrect.","error":"Cannot find module 'json-schema-patch'"},{"fix":"Ensure you have version 1.0.0 or later, and import the class correctly: import { JSONSchemaPatch } from 'json-schema-patch'.","cause":"Using TypeScript but not importing the type correctly, or using an older version without this method.","error":"Property 'addDefinition' does not exist on type 'JSONSchemaPatch'"},{"fix":"Instantiate with new: const patcher = new JSONSchemaPatch(schema);","cause":"JSONSchemaPatch was not instantiated (e.g., missing new keyword).","error":"Cannot read properties of undefined (reading 'applyPatch')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/cosmology-tech/json-schema-patch#readme","github":"https://github.com/cosmology-tech/json-schema-patch","docs":null,"changelog":null,"pypi":null,"npm":"json-schema-patch","openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-07","next_check":"2026-09-05","install_tag":null}}