Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid JsonPatchError for OPERATION_VALUE_OUT_OF_BOUNDS when validating patches #220

Closed
ianvonholt opened this issue Jan 16, 2019 · 2 comments
Assignees

Comments

@ianvonholt
Copy link
Contributor

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
Copy link
Collaborator

PR in #221

@alshakero
Copy link
Collaborator

Fixed in #221 and released 2.1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants