Skip to content

Invalid JsonPatchError for OPERATION_VALUE_OUT_OF_BOUNDS when validating patches #220

@ianvonholt

Description

@ianvonholt

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions