CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-fast-json-patch

Fast implementation of JSON-Patch (RFC-6902) with duplex capabilities for applying, generating, and observing JSON document changes

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

core-operations.mddocs/

Core Operations

Core JSON Patch functionality for applying patch operations to JSON documents following RFC-6902. These functions handle all standard patch operations: add, remove, replace, move, copy, and test.

Capabilities

Apply Patch Array

Applies a complete JSON Patch array to a document, processing all operations in sequence.

/**
 * Apply a full JSON Patch array on a JSON document.
 * Returns the {newDocument, result} of the patch.
 * @param document The document to patch
 * @param patch The patch array to apply
 * @param validateOperation false for no validation, true for default validation, or custom validator function
 * @param mutateDocument Whether to mutate the original document or clone it before applying
 * @param banPrototypeModifications Whether to ban modifications to __proto__, defaults to true
 * @returns Array of operation results with newDocument property
 */
function applyPatch<T>(
  document: T,
  patch: ReadonlyArray<Operation>,
  validateOperation?: boolean | Validator<T>,
  mutateDocument?: boolean,
  banPrototypeModifications?: boolean
): PatchResult<T>;

Usage Examples:

import { applyPatch } from "fast-json-patch";

const document = { name: "John", age: 30, hobbies: ["reading"] };
const patch = [
  { op: "replace", path: "/name", value: "Jane" },
  { op: "add", path: "/email", value: "jane@example.com" },
  { op: "add", path: "/hobbies/1", value: "swimming" }
];

// Apply with validation
const result = applyPatch(document, patch, true);
console.log(result.newDocument);
// { name: "Jane", age: 30, hobbies: ["reading", "swimming"], email: "jane@example.com" }

// Apply without mutating original
const result2 = applyPatch(document, patch, false, false);
// document remains unchanged, result2.newDocument contains the updated version

Apply Single Operation

Applies a single JSON Patch operation to a document.

/**
 * Apply a single JSON Patch Operation on a JSON document.
 * Returns the {newDocument, result} of the operation.
 * @param document The document to patch
 * @param operation The operation to apply
 * @param validateOperation false for no validation, true for default validation, or custom validator function
 * @param mutateDocument Whether to mutate the original document or clone it before applying
 * @param banPrototypeModifications Whether to ban modifications to __proto__, defaults to true
 * @param index Operation index for error reporting
 * @returns Operation result with newDocument, and optionally removed value or test result
 */
function applyOperation<T>(
  document: T,
  operation: Operation,
  validateOperation?: boolean | Validator<T>,
  mutateDocument?: boolean,
  banPrototypeModifications?: boolean,
  index?: number
): OperationResult<T>;

Usage Examples:

import { applyOperation } from "fast-json-patch";

const document = { users: [{ name: "Alice" }] };

// Add operation
const addResult = applyOperation(document, {
  op: "add",
  path: "/users/1",
  value: { name: "Bob" }
});

// Remove operation
const removeResult = applyOperation(document, {
  op: "remove",
  path: "/users/0"
});
console.log(removeResult.removed); // { name: "Alice" }

// Test operation
const testResult = applyOperation(document, {
  op: "test",
  path: "/users/0/name",
  value: "Alice"
});
console.log(testResult.test); // true

Apply Reducer

Applies a single operation as a reducer function, suitable for use with Array.reduce().

/**
 * Apply a single JSON Patch Operation on a JSON document.
 * Returns the updated document. Suitable as a reducer.
 * @param document The document to patch
 * @param operation The operation to apply
 * @param index Operation index for error reporting
 * @returns The updated document
 */
function applyReducer<T>(document: T, operation: Operation, index: number): T;

Usage Examples:

import { applyReducer } from "fast-json-patch";

const document = { counter: 0 };
const operations = [
  { op: "replace", path: "/counter", value: 1 },
  { op: "add", path: "/status", value: "active" }
];

// Use as reducer
const result = operations.reduce(applyReducer, document);
console.log(result); // { counter: 1, status: "active" }

Get Value By Pointer

Retrieves a value from a JSON document using a JSON pointer path.

/**
 * Retrieves a value from a JSON document by a JSON pointer.
 * Returns the value.
 * @param document The document to get the value from
 * @param pointer An escaped JSON pointer
 * @returns The retrieved value
 */
function getValueByPointer(document: any, pointer: string): any;

Usage Examples:

import { getValueByPointer } from "fast-json-patch";

const document = {
  users: [
    { name: "Alice", profile: { age: 25 } },
    { name: "Bob", profile: { age: 30 } }
  ]
};

// Get nested values
const name = getValueByPointer(document, "/users/0/name"); // "Alice"
const age = getValueByPointer(document, "/users/1/profile/age"); // 30
const users = getValueByPointer(document, "/users"); // entire users array
const root = getValueByPointer(document, ""); // entire document

Operation Types

All operations inherit from the base operation interface:

interface BaseOperation {
  path: string;
}

Add Operation

Adds a value at the specified path.

interface AddOperation<T> extends BaseOperation {
  op: 'add';
  value: T;
}

Remove Operation

Removes the value at the specified path.

interface RemoveOperation extends BaseOperation {
  op: 'remove';
}

Replace Operation

Replaces the value at the specified path.

interface ReplaceOperation<T> extends BaseOperation {
  op: 'replace';
  value: T;
}

Move Operation

Moves a value from one path to another.

interface MoveOperation extends BaseOperation {
  op: 'move';
  from: string;
}

Copy Operation

Copies a value from one path to another.

interface CopyOperation extends BaseOperation {
  op: 'copy';
  from: string;
}

Test Operation

Tests that the value at the specified path equals the given value.

interface TestOperation<T> extends BaseOperation {
  op: 'test';
  value: T;
}

Result Types

Operation Result

Result of applying a single operation.

interface OperationResult<T> {
  /** The removed value (for remove, replace, move operations) */
  removed?: any;
  /** Test result (for test operations) */
  test?: boolean;
  /** The updated document */
  newDocument: T;
}

Patch Result

Result of applying a patch array, extends Array with newDocument property.

interface PatchResult<T> extends Array<OperationResult<T>> {
  /** The final updated document after all operations */
  newDocument: T;
}

Error Handling

Operations can throw JsonPatchError with specific error codes for different failure scenarios:

  • OPERATION_NOT_AN_OBJECT: Operation is not a valid object
  • OPERATION_OP_INVALID: Invalid operation type
  • OPERATION_PATH_INVALID: Invalid path format
  • OPERATION_VALUE_REQUIRED: Missing required value property
  • OPERATION_FROM_REQUIRED: Missing required from property
  • OPERATION_PATH_UNRESOLVABLE: Path cannot be resolved
  • TEST_OPERATION_FAILED: Test operation returned false

Security protections prevent modification of __proto__ and constructor.prototype properties by default.

docs

change-observation.md

core-operations.md

index.md

json-pointer-utilities.md

validation.md

tile.json