https://github.com/hyperweb-io/json-schema-patch
https://github.com/hyperweb-io/json-schema-patch
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/hyperweb-io/json-schema-patch
- Owner: hyperweb-io
- License: mit
- Created: 2024-05-17T09:58:37.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-26T20:53:10.000Z (over 1 year ago)
- Last Synced: 2025-02-13T00:18:17.125Z (over 1 year ago)
- Language: TypeScript
- Size: 66.4 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# json-schema-patch
`json-schema-patch` provides a dynamic way to modify JSON schemas by applying a series of operations that can add, remove, or replace properties and definitions within the schema.
## Features
- Add, remove, and replace properties within a JSON schema.
- Manage schema `$defs` with operations to add and remove definitions.
- Automatically handle dependencies and cleanup for removed definitions.
## Installation
```bash
npm install json-schema-patch
```
## Usage
### import the `JSONSchemaPatch` class
```js
import { JSONSchemaPatch } from 'json-schema-patch';
```
### Create an instance
```js
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);
```
### Applying Operations
```js
// 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);
```