json-patch / json-patch2

A possibile revision to the JSON-Patch format
44 stars 0 forks source link

Reverse Patch Elements #29

Open 4thex opened 3 years ago

4thex commented 3 years ago

Summary

The current patch element does not contain enough information to reverse the operation. In version control it is important to be able to make incremental changes to a document, but it is also important to maintain an audit trail and to be able to reverse patches that have previously been applied. I am therefore proposing a method for creating audit records from the patch elements and how to create the reverse patch actions from an audit record, so you can go back to any previous state.

Details

The format of an audit record will look like this:

{
  "timeStamp": "ISO 6801 timestamp for the action",
  "previous": "Properties from the document or the patch before applying the patch element to assist in creating the reverse audit record. See examples below.",
  "action": "The patch element"
}

For each operation, there is an equivalent reverse operation. This list below shows how they map:

Examples

For the examples, I will be using this document:

{
  "books": [
    {
      "title": "Insider Outsider",
      "isbn": "978-0310345039"
    },
    {
      "title": "Ender's Game",
      "isbn": "978-0765370624"
    },
    {
      "title": "Andersen's Fairy Tales",
      "isbn": "978-1420953626"
    }
  ]
}

add

The patch element:

{
  "op": "add",
  "path": "/books/3",
  "value": {
    {
      "title": "How JavaScript Works",
      "isbn": "978-1949815009"
    }
  }
}

Audit record:

{
  "timeStamp": "2020-12-11T11:17:21Z",
  "action": {
    "op": "add",
    "path": "/books/3",
    "value": {
      {
        "title": "How JavaScript Works",
        "isbn": "978-1949815009"
      }
    }
  }
}

Note: The "previous" property is not needed here.

Reverse patch element:

{
    "op": "remove",
    "path": "/books/3"
}

remove

The patch element:

{
  "op": "remove",
  "path": "/books/3",
}

Audit record:

{
  "timeStamp": "2020-12-11T11:17:22Z",
  "previous": {
  {
    "title": "How JavaScript Works",
    "isbn": "978-1949815009"
  },
  "action": {
    "op": "remove",
    "path": "/books/3"
  }
}

Reverse patch element:

{
  "op": "add",
  "path": "/books/3",
  "value": {
    {
      "title": "How JavaScript Works",
      "isbn": "978-1949815009"
    }
  }
}

replace

move

copy

test