Comments (16)
@joshua-mcginnis, this shouldn't be too difficult to add. You should make up a PR if you've got the time :)
from json-patch.
@jmeas There's my initial crack at it.
from json-patch.
I have pushed to master
my take on this, which is a generalised version of changes proposed by @joshua-mcginnis, @sonnyp and others.
Please share your thoughts. Below is the description of the new method (excerpt from README.md)
Validating a sequence of patches:
var obj = {user: {firstName: "Albert"}};
var patches = [{op: "replace", path: "/user/firstName", value: "Albert"}, {op: "replace", path: "/user/lastName", value: "Einstein"}];
var errors = jsonpatch.validate(patches, obj);
if (errors.length == 0) {
//there are no errors!
}
else {
for (var i=0; i<errors.length; i++) {
if (!errors[i]) {
console.log("Valid patch at index", i, patches[i]);
}
else {
console.error("Invalid patch at index", i, errors[i], patches[i]);
}
}
}
jsonpatch.validate (patches
Array, tree
Object (optional)) : errors
Array
Available in json-patch-duplex.js
Validates a sequence of operations. If tree
parameter is provided, the sequence is additionally validated against the object tree.
If there are errors, returns an array with error codes located at the array index of the operation. If there are no errors, returns an empty array (length 0).
Possible errors:
Error code | Description |
---|---|
SEQUENCE_NOT_AN_ARRAY | Sequence of operations is not an array |
OPERATION_NOT_AN_OBJECT | Operation is not an object |
OPERATION_OP_INVALID | Operation op property is not one of operations defined in RFC-6902 |
OPERATION_PATH_INVALID | Operation path property is not a string |
OPERATION_FROM_REQUIRED | Operation from property is not present (applicable in move and copy operations) |
OPERATION_VALUE_REQUIRED | Operation value property is not present (applicable in add , replace and test operations) |
OPERATION_PATH_ALREADY_EXISTS | Cannot perform an add operation at a path that already exists |
OPERATION_PATH_UNRESOLVABLE | Cannot perform a replace , remove , move or copy operation at a path that doesn't exist |
from json-patch.
@warpech cool.
I think the initial multiple option from my PR remains useful. User might not be interested in errors and might want break validation after the first error. Also having an options argument allow to extend the API later on.
I'd even go further and make tree a property of the options argument.
jsonpatch.validate (patch, {multiple: true, tree: document});
"Cannot perform an add operation at a path that already exists" actuall IIRC specs says that add will replace value if path exists.
Next time you ask for comments could you make a PR instead? Much easier to review. Also http://alblue.bandlem.com/2011/06/git-tip-of-week-rebasing.html 😉
from json-patch.
@sonnyp, you are right about the add
operation:
If the target location specifies an object member that does exist,
that member's value is replaced.
I will fix that.
My motivation for removing the multiple
option was to have only one behaviour that suits all needs. If you just want to know if there are no errors, check errors.length === 0
. Otherwise you can iterate over the array to use the detailed information about what and where is the error. I think providing a wider range of options would make it both harder to learn (for users) and maintain (for me).
Thanks for mentioning git rebase, of course I know it but did not want to mess with the merge. My changes are in these commits:
from json-patch.
@warpech the use case is to stop validation after first error, so removing multiple doesn't suit my needs.
What's wrong with returning an array of length 1 if multiple is set to false?
"both harder to learn " just a flag
"and maintain (for me)" how hard is it to maintainif (options.multiple === false) ... break
; plus I wrote tests
from json-patch.
I appreciate your contribution so I want to reflect your needs. Could you describe your use case a little bit more?
For me, invalid patches are rare and fatal errors, so the efficiency of returning just the first error is irrelevant.
from json-patch.
If you accept incoming patches from untrusted source it allow to prevent process from blocking if a large faulty patch is being validated, so on production you'd probably turn multiple/iterate/whatever to false.
from json-patch.
I start to get the point. So I guess you are using it on server? Is it in Redbooth?
I think we can:
- add a
stopOnFirstError
boolean parameter - expose
_validate
method so you can overwrite it if you want to add custom validations (I find it useful for myself)
from json-patch.
@warpech we don't currently use it at redbooth. Yes I use it server side.
Sounds good, thanks. Would you like me to do the PR?
from json-patch.
No worries, I will do it and let you do the testing :)
from json-patch.
@sonnyp Pushed. Would you take a look at the commit b64474b and share your thoughts?
from json-patch.
I have reworked the validation. The result is a faster and simpler API.
Summary of changes:
jsonpatch.validate
returns and error object nowjsonpatch.apply
with third parameter set totrue
throws an error if it encounters an invalid patch
This should please @sonnyp - only the first error is returned now.
See the changes at branch https://github.com/Starcounter-Jack/JSON-Patch/tree/issue-29-simplified
(last 2 commits)
The intention is to release it soon as version 0.6.0
from json-patch.
Merged to master
from json-patch.
Released in 0.5.1.
See https://github.com/Starcounter-Jack/JSON-Patch/blob/master/README.md for API and tests for examples
from json-patch.
👍
from json-patch.
Related Issues (20)
- Add user-defined hooks into the deepClone function
- Inefficient patch encoding when prepending to arrays HOT 2
- rrrr HOT 1
- Add performance comparison with jsondiffpatch
- Array patching, over empty elements.. HOT 2
- Any plans for a new release? HOT 1
- Maybe it's already discussed, but anyone think of how to patch array member in a more flexible approach?
- Release 3.1.1 request based on PR merged of #262 HOT 2
- Inaccurate documentation HOT 1
- Error index always 0? HOT 1
- Optional observe callback not called HOT 1
- applyPatch() validator index is not incrementing
- Backport fix for GHSA-8gh8-hqwg-xf34 to v2 HOT 5
- package.json should use `conditional exports`, else esm won't work inexplicitly HOT 1
- Node12+ import documentation incorrect
- Documentation for `generate` mentions wrong signature
- auto add tests to "add/remove" operations like for "replace"
- Mixing default exports and named exports breaks parcel HOT 2
- Attempting to use if mirror has a toJSON and object does not breaks HOT 2
- applying a replace patch is inconsistent with path="" and path="/"
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-patch.