@lp-labs/react-json-patch
v0.1.0
Published
Safe RFC 6902 JSON Patch validation, application, preview, and React view.
Maintainers
Readme
@lp-labs/react-json-patch
An immutable-input JSON Patch validator, applier, previewer, and small controlled React preview for the supported RFC 6902 operations.
Example
import { applyPatch, JsonPatchEditor, type PatchOperation } from "@lp-labs/react-json-patch";
import "@lp-labs/react-json-patch/styles.css";
const patch: PatchOperation[] = [
{ op: "test", path: "/profile/name", value: "Grace" },
{ op: "replace", path: "/profile/name", value: "Ada" },
{ op: "add", path: "/tags/-", value: "verified" },
];
const result = applyPatch({ profile: { name: "Grace" }, tags: [] }, patch);
function Preview() { return <JsonPatchEditor document={{ profile: { name: "Grace" }, tags: [] }} patch={patch} />; }React API
JsonPatchEditorProps is {document: unknown; patch: readonly unknown[]; onPatch?: (patch: readonly unknown[]) => void; className?: string}. JsonPatchEditor defaults its class to lp-json-patch; it previews the transformed JSON and error text in an aria-live output. The optional button calls onPatch with a shallow copy of the validated input operations; it does not mutate or replace the supplied document. It is disabled without a callback or if preview fails.
Helpers and types
PatchOperationis the union of RFC-shapedadd,remove,replace,move,copy, andtestoperations. Add/replace/test carryvalue; move/copy carryfrom. Values are arbitrary cloneable JSON-compatible values.JsonPatchErrorextendsError;operationIndex?: numberidentifies the operation that failed (validation or application).decodePointer(pointer: string): string[]andencodePointer(parts: readonly string[]): stringdecode/encode RFC 6901 escaping (~1slash,~0tilde). Invalid pointer structure or escapes throwJsonPatchError. Unsafe segments__proto__,prototype, andconstructorare rejected on decode.validatePatch(patch: readonly unknown[]): PatchOperation[]validates operation names, required properties, path and from pointers. Returns the original validated operation references and throwsJsonPatchErrorwith an index on invalid input.PatchResultis{document: unknown; changed: boolean; operationIndex?: number}.applyPatch<T>(document: T, patch: readonly unknown[]): PatchResultclones the document, then applies operations in order without mutating input.testuses structural equality. Array indices must be canonical nonnegative decimal integers; add permits index equal to length and-for append. Object add creates/replaces a key; remove/replace require an existing key. Root add/replace are supported; root removal is not. Move into a descendant is rejected. Errors throwJsonPatchErrorwith the operation index.previewPatch(document: unknown, patch: readonly unknown[]): PatchResultis an alias in behavior forapplyPatch.JsonPatchEditorPropsis the component props type.
Standards subset and limits
Implements the RFC 6902 operation set and RFC 6901 pointer escaping, with deliberately stricter prototype-related path rejection. It does not handle non-JSON values, custom equality, streaming/large documents, JSON text parsing, conflict resolution, or partial results: any failed operation throws and leaves the original document unchanged. structuredClone must be supported by the runtime. The preview component does not offer a JSON editing surface; callers supply patch data. Output is an accessible live region but does not implement a line-oriented diff. React is the only runtime peer; declarations accompany ESM/CJS builds.
License
MIT.
Support
If this library is useful to you, consider supporting its maintenance with a donation via PayPal.
Author
Created by Libin Prasanth. Connect on LinkedIn for updates and discussion.
