(patch: JsonPatch, oldValue: Schema.Json): Schema.JsonApplies a JSON Patch to a JSON document.
When to use
Use to execute patches generated by get, transform documents with manually constructed patches, or process patch operations from external sources.
Details
Executes patch operations sequentially, so later operations see changes made
by earlier operations. It never mutates the input document; array and object
operations copy the affected containers. An empty patch returns the original
reference, and a root replace (path: "") returns the provided value
directly.
Gotchas
Invalid paths, missing properties, and out-of-bounds array indices throw errors.
Example (Applying a patch)
import { JsonPatch } from "effect"
const document = { items: [1, 2, 3], total: 6 }
const patch: JsonPatch.JsonPatch = [
{ op: "add", path: "/items/-", value: 4 },
{ op: "replace", path: "/total", value: 10 }
]
const result = JsonPatch.apply(patch, document)
// { items: [1, 2, 3, 4], total: 10 }export function function apply(
patch: JsonPatch,
oldValue: Schema.Json
): Schema.Json
Applies a JSON Patch to a JSON document.
When to use
Use to execute patches generated by
get
, transform documents
with manually constructed patches, or process patch operations from external
sources.
Details
Executes patch operations sequentially, so later operations see changes made
by earlier operations. It never mutates the input document; array and object
operations copy the affected containers. An empty patch returns the original
reference, and a root replace (path: "") returns the provided value
directly.
Gotchas
Invalid paths, missing properties, and out-of-bounds array indices throw
errors.
Example (Applying a patch)
import { JsonPatch } from "effect"
const document = { items: [1, 2, 3], total: 6 }
const patch: JsonPatch.JsonPatch = [
{ op: "add", path: "/items/-", value: 4 },
{ op: "replace", path: "/total", value: 10 }
]
const result = JsonPatch.apply(patch, document)
// { items: [1, 2, 3, 4], total: 10 }
apply(patch: JsonPatch(parameter) patch: {
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<JsonPatchOperation>>): Array<JsonPatchOperation>; (...items: Array<JsonPatchOperation | ConcatArray<JsonPatchOperation>>): Array<JsonPatchOperation> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<JsonPatchOperation>;
indexOf: (searchElement: JsonPatchOperation, fromIndex?: number) => number;
lastIndexOf: (searchElement: JsonPatchOperation, fromIndex?: number) => number;
every: { (predicate: (value: JsonPatchOperation, index: number, array: ReadonlyArray<JsonPatchOperation>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: JsonPatchOperation, index: number, array: ReadonlyArray<JsonPatchOp…;
some: (predicate: (value: JsonPatchOperation, index: number, array: ReadonlyArray<JsonPatchOperation>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: JsonPatchOperation, index: number, array: ReadonlyArray<JsonPatchOperation>) => void, thisArg?: any) => void;
map: (callbackfn: (value: JsonPatchOperation, index: number, array: ReadonlyArray<JsonPatchOperation>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: JsonPatchOperation, index: number, array: ReadonlyArray<JsonPatchOperation>) => value is S, thisArg?: any): Array<S>; (predicate: (value: JsonPatchOperation, index: number, array: ReadonlyArray<JsonPatchOperation>) =>…;
reduce: { (callbackfn: (previousValue: JsonPatchOperation, currentValue: JsonPatchOperation, currentIndex: number, array: ReadonlyArray<JsonPatchOperation>) => JsonPatchOperation): JsonPatchOperation; (callbackfn: (previousValue: JsonPatchOperatio…;
reduceRight: { (callbackfn: (previousValue: JsonPatchOperation, currentValue: JsonPatchOperation, currentIndex: number, array: ReadonlyArray<JsonPatchOperation>) => JsonPatchOperation): JsonPatchOperation; (callbackfn: (previousValue: JsonPatchOperatio…;
find: { (predicate: (value: JsonPatchOperation, index: number, obj: ReadonlyArray<JsonPatchOperation>) => value is S, thisArg?: any): S | undefined; (predicate: (value: JsonPatchOperation, index: number, obj: ReadonlyArray<JsonPatchOperation>) =…;
findIndex: (predicate: (value: JsonPatchOperation, index: number, obj: ReadonlyArray<JsonPatchOperation>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, JsonPatchOperation]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<JsonPatchOperation>;
includes: (searchElement: JsonPatchOperation, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: JsonPatchOperation, index: number, array: Array<JsonPatchOperation>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => JsonPatchOperation | undefined;
findLast: { (predicate: (value: JsonPatchOperation, index: number, array: ReadonlyArray<JsonPatchOperation>) => value is S, thisArg?: any): S | undefined; (predicate: (value: JsonPatchOperation, index: number, array: ReadonlyArray<JsonPatchOperation…;
findLastIndex: (predicate: (value: JsonPatchOperation, index: number, array: ReadonlyArray<JsonPatchOperation>) => unknown, thisArg?: any) => number;
toReversed: () => Array<JsonPatchOperation>;
toSorted: (compareFn?: ((a: JsonPatchOperation, b: JsonPatchOperation) => number) | undefined) => Array<JsonPatchOperation>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<JsonPatchOperation>): Array<JsonPatchOperation>; (start: number, deleteCount?: number): Array<JsonPatchOperation> };
with: (index: number, value: JsonPatchOperation) => Array<JsonPatchOperation>;
}
patch: type JsonPatch =
readonly JsonPatchOperation[]
A JSON Patch document (an ordered list of operations).
When to use
Use to store, serialize, pass, or validate complete patch documents.
Details
Represents a complete transformation as a readonly sequence of immutable
operations. Operations are applied sequentially from first to last, and later
operations observe the document state produced by earlier operations. An empty
array represents a no-op patch and returns the original document.
Example (Defining a multi-operation patch)
import { JsonPatch } from "effect"
const patch: JsonPatch.JsonPatch = [
{ op: "add", path: "/items/-", value: "apple" },
{ op: "replace", path: "/count", value: 5 },
{ op: "remove", path: "/oldField" }
]
const result = JsonPatch.apply(patch, { count: 3, oldField: "value" })
// { count: 5, items: ["apple"] }
JsonPatch, oldValue: Schema.JsonoldValue: import SchemaSchema.type Json =
| string
| number
| boolean
| Schema.JsonArray
| Schema.JsonObject
| null
Recursive TypeScript type for any valid immutable JSON value: null,
number, boolean, string, a readonly array of Json values, or a
readonly record of string → Json. For the corresponding schema, see the
Json
const.
Schema that accepts and validates any immutable JSON-compatible value.
Example (Validating a JSON value)
import { Schema } from "effect"
const result = Schema.decodeUnknownOption(Schema.Json)({ key: [1, true, null] })
console.log(result._tag) // "Some"
Json): import SchemaSchema.type Json =
| string
| number
| boolean
| Schema.JsonArray
| Schema.JsonObject
| null
Recursive TypeScript type for any valid immutable JSON value: null,
number, boolean, string, a readonly array of Json values, or a
readonly record of string → Json. For the corresponding schema, see the
Json
const.
Schema that accepts and validates any immutable JSON-compatible value.
Example (Validating a JSON value)
import { Schema } from "effect"
const result = Schema.decodeUnknownOption(Schema.Json)({ key: [1, true, null] })
console.log(result._tag) // "Some"
Json {
let let doc: Schema.Jsondoc = oldValue: Schema.JsonoldValue
for (const const op: JsonPatchOperationop of patch: JsonPatch(parameter) patch: {
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<JsonPatchOperation>>): Array<JsonPatchOperation>; (...items: Array<JsonPatchOperation | ConcatArray<JsonPatchOperation>>): Array<JsonPatchOperation> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<JsonPatchOperation>;
indexOf: (searchElement: JsonPatchOperation, fromIndex?: number) => number;
lastIndexOf: (searchElement: JsonPatchOperation, fromIndex?: number) => number;
every: { (predicate: (value: JsonPatchOperation, index: number, array: ReadonlyArray<JsonPatchOperation>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: JsonPatchOperation, index: number, array: ReadonlyArray<JsonPatchOp…;
some: (predicate: (value: JsonPatchOperation, index: number, array: ReadonlyArray<JsonPatchOperation>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: JsonPatchOperation, index: number, array: ReadonlyArray<JsonPatchOperation>) => void, thisArg?: any) => void;
map: (callbackfn: (value: JsonPatchOperation, index: number, array: ReadonlyArray<JsonPatchOperation>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: JsonPatchOperation, index: number, array: ReadonlyArray<JsonPatchOperation>) => value is S, thisArg?: any): Array<S>; (predicate: (value: JsonPatchOperation, index: number, array: ReadonlyArray<JsonPatchOperation>) =>…;
reduce: { (callbackfn: (previousValue: JsonPatchOperation, currentValue: JsonPatchOperation, currentIndex: number, array: ReadonlyArray<JsonPatchOperation>) => JsonPatchOperation): JsonPatchOperation; (callbackfn: (previousValue: JsonPatchOperatio…;
reduceRight: { (callbackfn: (previousValue: JsonPatchOperation, currentValue: JsonPatchOperation, currentIndex: number, array: ReadonlyArray<JsonPatchOperation>) => JsonPatchOperation): JsonPatchOperation; (callbackfn: (previousValue: JsonPatchOperatio…;
find: { (predicate: (value: JsonPatchOperation, index: number, obj: ReadonlyArray<JsonPatchOperation>) => value is S, thisArg?: any): S | undefined; (predicate: (value: JsonPatchOperation, index: number, obj: ReadonlyArray<JsonPatchOperation>) =…;
findIndex: (predicate: (value: JsonPatchOperation, index: number, obj: ReadonlyArray<JsonPatchOperation>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, JsonPatchOperation]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<JsonPatchOperation>;
includes: (searchElement: JsonPatchOperation, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: JsonPatchOperation, index: number, array: Array<JsonPatchOperation>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => JsonPatchOperation | undefined;
findLast: { (predicate: (value: JsonPatchOperation, index: number, array: ReadonlyArray<JsonPatchOperation>) => value is S, thisArg?: any): S | undefined; (predicate: (value: JsonPatchOperation, index: number, array: ReadonlyArray<JsonPatchOperation…;
findLastIndex: (predicate: (value: JsonPatchOperation, index: number, array: ReadonlyArray<JsonPatchOperation>) => unknown, thisArg?: any) => number;
toReversed: () => Array<JsonPatchOperation>;
toSorted: (compareFn?: ((a: JsonPatchOperation, b: JsonPatchOperation) => number) | undefined) => Array<JsonPatchOperation>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<JsonPatchOperation>): Array<JsonPatchOperation>; (start: number, deleteCount?: number): Array<JsonPatchOperation> };
with: (index: number, value: JsonPatchOperation) => Array<JsonPatchOperation>;
}
patch) {
switch (const op: JsonPatchOperationop.op: "add" | "remove" | "replace"op) {
case "replace": {
let doc: Schema.Jsondoc = const op: {
readonly op: "replace"
readonly path: string
readonly value: Schema.Json
readonly description?: string
}
op.path: stringJSON Pointer to the target location. Use "" to replace the root document.
When to use
Use to identify which location the replace operation overwrites.
path === "" ? const op: {
readonly op: "replace"
readonly path: string
readonly value: Schema.Json
readonly description?: string
}
op.value: Schema.Jsonvalue : function setAt(
doc: Schema.Json,
pointer: string,
val: Schema.Json | undefined,
mode: "replace" | "remove"
): Schema.Json
setAt(let doc: Schema.Jsondoc, const op: {
readonly op: "replace"
readonly path: string
readonly value: Schema.Json
readonly description?: string
}
op.path: stringJSON Pointer to the target location. Use "" to replace the root document.
When to use
Use to identify which location the replace operation overwrites.
path, const op: {
readonly op: "replace"
readonly path: string
readonly value: Schema.Json
readonly description?: string
}
op.value: Schema.Jsonvalue, "replace")
break
}
case "add": {
let doc: Schema.Jsondoc = function addAt(
doc: Schema.Json,
pointer: string,
val: Schema.Json
): Schema.Json
addAt(let doc: Schema.Jsondoc, const op: {
readonly op: "add"
readonly path: string
readonly value: Schema.Json
readonly description?: string
}
op.path: stringJSON Pointer to the target location. For arrays, the last token may be -
to append.
When to use
Use to identify where the add operation inserts its value.
path, const op: {
readonly op: "add"
readonly path: string
readonly value: Schema.Json
readonly description?: string
}
op.value: Schema.Jsonvalue)
break
}
case "remove": {
let doc: Schema.Jsondoc = function setAt(
doc: Schema.Json,
pointer: string,
val: Schema.Json | undefined,
mode: "replace" | "remove"
): Schema.Json
setAt(let doc: Schema.Jsondoc, const op: {
readonly op: "remove"
readonly path: string
readonly description?: string
}
op.path: stringJSON Pointer to the target location.
When to use
Use to identify which location the remove operation deletes.
path, var undefinedundefined, "remove")
break
}
}
}
return let doc: Schema.Jsondoc
}