From c1cca86d9fae550f392ffec289c528ca585be1fe Mon Sep 17 00:00:00 2001 From: Josh Kelley Date: Sat, 15 May 2021 12:25:42 -0400 Subject: [PATCH] Add docs for createTests --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index 05635f9..8f94c3a 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,26 @@ const my_patch = createPatch(input, output, myDiff) ``` This will short-circuit on encountering an instance of `MyObject`, but otherwise recurse as usual. +### `createTest(input: any, patch: Operation[]): Operation[]` + +Returns a list of `test` JSON Patch operations to verify that +existing values in an object are identical to the those captured at some +checkpoint (whenever this function is called). + +The resulting operations don't modify the object. You can use the resulting +operations to validate that an object is in the expected state before applying a +patch. + +Sample usage: + +```js +const patch = rfc6902.createPatch({first: 'Chris', last: 'Brown'}, {first: 'Chris', last: 'Smith'}); +const tests = rfc6902.createTests({first: 'Chris', last: 'Brown'}, patch); +const problems = rfc6902.applyPatch({first: 'Chris', last: 'Cook'}, tests).filter(i => i != null); +console.log([problem]); +// [ Error [TestError]: Test failed: Cook != Brown ] +``` + ### `Operation` ```typescript