# json-schema-patch

> json-schema-patch

Latest version **1.0.2** (published 2024-09-26) · SEE LICENSE IN LICENSE license · 0 weekly downloads

## Install

```sh
npm install json-schema-patch
pnpm add json-schema-patch
yarn add json-schema-patch
bun add json-schema-patch
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2024-09-26 |
| First published | 2024-05-17 |
| Weekly downloads | 0 |
| License | SEE LICENSE IN LICENSE |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 50.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Dan Lynch |
| Maintainers | pyramation |

## Links

- npm: https://www.npmjs.com/package/json-schema-patch
- Repository: https://github.com/cosmology-tech/json-schema-patch
- Homepage: https://github.com/cosmology-tech/json-schema-patch#readme
- Issues: https://github.com/cosmology-tech/json-schema-patch/issues
- npm.io page: https://npm.io/package/json-schema-patch

## Dependencies (1)

- [fast-json-patch](https://npm.io/package/fast-json-patch.md) ^3.1.1

## Recent versions

- 1.0.2 (latest) — 2024-09-26
- 1.0.1 — 2024-05-18
- 1.0.0 — 2024-05-18
- 0.10.0 — 2024-05-18
- 0.9.0 — 2024-05-18
- 0.8.0 — 2024-05-18
- 0.7.0 — 2024-05-18
- 0.6.0 — 2024-05-17
- 0.5.0 — 2024-05-17
- 0.4.0 — 2024-05-17
- 0.3.0 — 2024-05-17
- 0.2.0 — 2024-05-17
- 0.1.0 — 2024-05-17
- 0.0.1 — 2024-05-17

## 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);
```

---
_Source: https://npm.io/package/json-schema-patch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
