JSON Patch utils
Helpers for client/server applications using and implementing JSON Patch [RFC 6902]
See documentation
Install:
$ npm install --save-dev json-patch-utils
Usage:
createPatch
Creates a JSON Patches given an operator, path and a value
// { op: 'add', path: '/foo', value: 123 } // [Error: The path "foo" did not match the encoded path regexp: /^(\/[a-z0-9~\\\-%^|"\ ]*)*$/gi,The path "bar" did not match the encoded path regexp: /^(\/[a-z0-9~\\\-%^|"\ ]*)*$/gi]
isPatch
Tests if a given object is a valid JSON Patch object according to https://tools.ietf.org/html/rfc6902
let validPatch = op: 'add' path: '/foo/bar' value: 'baz' let invalidPatch = op: 'foo' path: 'bar' if === true // Patch can be safely applied // [// 'The path "bar" did not match the encoded path regexp: /^(\\/[a-z0-9~\\\\\\-%^|"\\ ]*)*$/gi',// 'The operation name is not among the valid names add, replace, test, remove, move, copy'// ]
isPath
Tests if a given string is a valid JSON Pointer according to https://tools.ietf.org/html/rfc6901
// true // The path "foo" did not match the encoded path regexp: /^(\/[a-z0-9~\\\-%^|"\ ]*)*$/gi
isOperation
Tests if a given string is a valid JSON Patch operation name
// true // The operation name is not among the valid names add, replace, test, remove, move, copy
isValueForOperation
Tests if a given value corresponds to the given operation
// true // The value or from path provided must be different than undefined // The path "foo" did not match the encoded path regexp: /^(\/[a-z0-9~\\\-%^|"\ ]*)*$/gi
decodePath
Decodes a path according to https://tools.ietf.org/html/rfc6901 by replacing special symbols
// '/foo/bar' // '/foo/bar~'
listPath
Convert a path into a list of strings. Useful when traversing a path on a JSON Document
// ['foo', 'bar'] // ['foo', 'bar~']
Apply patch functions for various state/data structure libraries
BaobabJS
let tree = foo: bar: 123 let patch = tree // 1000
Credits
https://github.com/Starcounter-Jack/JSON-Patch - for the tests battery in test/tests.json and test/spec_tests.json
Contributing:
Feel free to open issues to propose stuff and participate. Pull requests are also welcome.