-
Notifications
You must be signed in to change notification settings - Fork 217
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
feature request: patch validator #29
Comments
@joshua-mcginnis, this shouldn't be too difficult to add. You should make up a PR if you've got the time :) |
@jmeas There's my initial crack at it. |
I have pushed to 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 (
|
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 |
@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. "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 😉 |
@sonnyp, you are right about the
I will fix that. My motivation for removing the 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: |
@warpech the use case is to stop validation after first error, so removing multiple doesn't suit my needs. "both harder to learn " just a flag |
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. |
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. |
I start to get the point. So I guess you are using it on server? Is it in Redbooth? I think we can:
|
@warpech we don't currently use it at redbooth. Yes I use it server side. |
No worries, I will do it and let you do the testing :) |
- add third parameter for `jsonpatch.validate` (stopOnFirstError) - make `jsonpatch.validator` public, which allows to extend it with custom validations (#29)
I have reworked the validation. The result is a faster and simpler API. Summary of changes:
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 The intention is to release it soon as version 0.6.0 |
Merged to master |
Released in 0.5.1. See https://github.com/Starcounter-Jack/JSON-Patch/blob/master/README.md for API and tests for examples |
👍 |
Thanks for this project.
A nice feature would be to validate that a patch document is a valid patch doc. This would allow users to pre-validate that the client sent a valid patch document before the server uses it.
Something like:
jsonpatch.isValid(patch); //true = valid, false = invalid
Or you could return errors, but that might be difficult. Ultimately, if a client sends a bad patch, we're just going to return an http bad request.
The text was updated successfully, but these errors were encountered: