Hyperlinkv0.8.0-beta.28

Schema

Schema.toDifferJsonPatchfunctioneffect/Schema.ts:14069
<T>(schema: ConstraintCodec<T, unknown>): Differ<T, JsonPatch.JsonPatch>

Derives a JSON Patch differ from a codec. Serializes values to JSON (via toCodecJson), computes RFC 6902 JSON Patch operations between old and new values, and can apply patches back to the typed value.

convertingtoCodecJson
Source effect/Schema.ts:1406915 lines
export function toDifferJsonPatch<T>(schema: ConstraintCodec<T, unknown>): Differ<T, JsonPatch.JsonPatch> {
  const serializer = toCodecJson(schema)
  const get = SchemaParser.encodeSync(serializer)
  const set = SchemaParser.decodeSync(serializer)
  return {
    empty: [],
    diff: (oldValue, newValue) => JsonPatch.get(get(oldValue), get(newValue)),
    combine: (first, second) => [...first, ...second],
    patch: (oldValue, patch) => {
      const value = get(oldValue)
      const patched = JsonPatch.apply(patch, value)
      return Object.is(patched, value) ? oldValue : set(patched)
    }
  }
}