Starcounter-Jack / JSON-Patch

Lean and mean Javascript implementation of the JSON-Patch standard (RFC 6902). Update JSON documents using delta patches.
MIT License
1.78k stars 215 forks source link

applyPatch() validator index is not incrementing #300

Open beam-bence opened 1 year ago

beam-bence commented 1 year ago

Using applyPatch, the index parameter of the custom validator function stays 0 with every call:

       applyPatch(
          json,
          operations,
          (change, index, tree, existingPath) => {
            const parentPath = change.path
              .split('/')
              .slice(0, change.path.split('/').length - 1)
              .join('/');
            if (!jsonpointer.exists(tree, parentPath)) {
              const extendedTree = {...tree};
              jsonpointer.set(extendedTree, parentPath, {});
              const fixChanges = compare(tree, extendedTree);

              //TODO fix bug index is always 0
              fixedChanges.splice(index, 0, ...fixChanges);
              throw new JsonPatchError(
                'Cannot perform operation at the desired path',
                'OPERATION_PATH_UNRESOLVABLE',
                index,
                change,
                tree
              );
            }
          },
          false //mutateDocument
        ).newDocument;

In the source, constant zero is passed instead of index: image