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.79k stars 215 forks source link

Invalid JsonPatchError for OPERATION_VALUE_OUT_OF_BOUNDS when validating patches #220

Closed ianvonholt closed 5 years ago

ianvonholt commented 5 years ago

Running the following will produce an incorrect JsonPatchError:

const jsonpatch = require('fast-json-patch')

var original = {
  myObj: {
    arrKey: []
  }
}

var patch = [
  { value: 1234, path: '/id', op: 'add' },
  { value: 12,
    path: '/myObj/arrKey/1',
    op: 'add' }
]

var validationErrors = jsonpatch.validate(patch, original)

if (validationErrors !== undefined) {
  console.log(validationErrors)
}

Output:

{ [OPERATION_VALUE_OUT_OF_BOUNDS: The specified index MUST NOT be greater than the number of elements in the array]
  message:
   'The specified index MUST NOT be greater than the number of elements in the array',
  name: 'OPERATION_VALUE_OUT_OF_BOUNDS',
  index: 0,
  operation: '/myObj/arrKey/1',
  tree: { value: 12, path: '/myObj/arrKey/1', op: 'add' } }

The operation references the path, and the tree references the actual operation. Additionally, the index is wrong, as it does not reflect the index of the patch that is invalid.

All other JSONPatchErrors have the operation referencing the invalid patch operation, index that reflects the position of the invalid patch in an array of patches, and tree that displays the corresponding object that is being patched.

Expected:

{ [OPERATION_VALUE_OUT_OF_BOUNDS: The specified index MUST NOT be greater than the number of elements in the array]
  message: 'The specified index MUST NOT be greater than the number of elements in the array',
  name: 'OPERATION_VALUE_OUT_OF_BOUNDS',
  index: 1,
  operation:{ value: 12, path: '/myObj/arrKey/1', op: 'add' } ,
  tree: { cost: { others: [] } } }

Version Information:

~ : node -v
v10.15.0
~ : npm -v
6.4.1
~ : npm ls | grep fast-json-patch
├─┬ fast-json-patch@2.0.7
alshakero commented 5 years ago

PR in https://github.com/Starcounter-Jack/JSON-Patch/pull/221

alshakero commented 5 years ago

Fixed in #221 and released 2.1.0.