1.0.2 • Published 9 months ago

json-schema-patch v1.0.2

Weekly downloads
-
License
SEE LICENSE IN LI...
Repository
github
Last release
9 months ago

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

npm install json-schema-patch

Usage

import the JSONSchemaPatch class

import { JSONSchemaPatch } from 'json-schema-patch';

Create an instance

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

// 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);
1.0.2

9 months ago

1.0.1

1 year ago

1.0.0

1 year ago

0.10.0

1 year ago

0.9.0

1 year ago

0.8.0

1 year ago

0.7.0

1 year ago

0.6.0

1 year ago

0.5.0

1 year ago

0.4.0

1 year ago

0.3.0

1 year ago

0.2.0

1 year ago

0.1.0

1 year ago

0.0.1

1 year ago