json-patch-to-crdt 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -183,6 +183,35 @@ If you prefer a non-throwing low-level compile+apply path, use `jsonPatchToCrdtS
183
183
  - `"-"` is treated as append for array inserts.
184
184
  - `test` operations can be evaluated against `head` or `base` using the `testAgainst` option.
185
185
 
186
+ ## Runtime JSON Guardrails
187
+
188
+ By default, runtime inputs are accepted as-is (`jsonValidation: "none"`) for backward compatibility.
189
+
190
+ You can opt into stricter runtime behavior on `createState`, `applyPatch`/`tryApplyPatch`/`validateJsonPatch`, and `diffJsonPatch`:
191
+
192
+ - `jsonValidation: "strict"`: reject non-JSON runtime values (for example `NaN`, `Infinity`, and `undefined`).
193
+ - `jsonValidation: "normalize"`: coerce non-JSON values into JSON-safe values.
194
+ - non-finite numbers become `null`
195
+ - invalid array elements become `null`
196
+ - invalid object-property values are omitted
197
+
198
+ Examples:
199
+
200
+ ```ts
201
+ const strictState = createState(payload as any, {
202
+ actor: "A",
203
+ jsonValidation: "strict",
204
+ });
205
+
206
+ const next = applyPatch(state, patch as any, {
207
+ jsonValidation: "normalize",
208
+ });
209
+
210
+ const delta = diffJsonPatch(base as any, target as any, {
211
+ jsonValidation: "strict",
212
+ });
213
+ ```
214
+
186
215
  ### Semantics Modes
187
216
 
188
217
  - `semantics: "sequential"` (default): applies operations one-by-one against the evolving head (RFC-like execution).
@@ -387,8 +416,9 @@ Internals helpers like `jsonPatchToCrdtSafe` and `tryMergeDoc` return the same s
387
416
  - `tryApplyPatchInPlace(state, patch, options?)` - Non-throwing in-place apply result.
388
417
  - `validateJsonPatch(baseJson, patch, options?)` - Preflight patch validation (non-mutating).
389
418
  - `toJson(docOrState)` - Materialize a JSON value from a doc or state.
390
- - `applyPatch`/`tryApplyPatch` options: `base` expects a prior `CrdtState` snapshot (not a raw doc), plus `semantics` and `testAgainst`.
419
+ - `applyPatch`/`tryApplyPatch` options: `base` expects a prior `CrdtState` snapshot (not a raw doc), plus `semantics`, `testAgainst`, and optional `jsonValidation` runtime guardrails.
391
420
  - `PatchError` - Error class thrown for failed patches (`code`, `reason`, `message`, optional `path`/`opIndex`).
421
+ - `JsonValueValidationError` - Error class thrown by strict runtime validation in APIs that accept raw JSON values (for example `createState` and `diffJsonPatch`).
392
422
 
393
423
  ### Merge helpers
394
424
 
@@ -398,7 +428,7 @@ Internals helpers like `jsonPatchToCrdtSafe` and `tryMergeDoc` return the same s
398
428
 
399
429
  ### Patch helpers
400
430
 
401
- - `diffJsonPatch(baseJson, nextJson, options?)` - Compute a JSON Patch delta between two JSON values.
431
+ - `diffJsonPatch(baseJson, nextJson, options?)` - Compute a JSON Patch delta between two JSON values (`arrayStrategy`, `lcsMaxCells`, and optional `jsonValidation` guardrails).
402
432
 
403
433
  ### Serialization
404
434