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
Implement jsonpatch.validate with tests. #29. #44
Conversation
@@ -462,6 +462,46 @@ var jsonpatch; | |||
return patches; | |||
} | |||
jsonpatch.compare = compare; | |||
|
|||
function validate(patch) { | |||
if(!Array.isArray(patch)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Array.isArray() isn't available on IE8, check json-patch.ts method _isArray()
@joshua-mcginnis cool, thanks! I took the liberty to comment the diff, additionally: .js are actually built from the .ts files: http://www.typescriptlang.org/ You can build the .js with
|
@sonnyp will typescript not let me return mixed types?
Gives me: If that's the case, then how should we return the validation errors if isValid always returns bool. I'd like not to not throw as that will make implementation uglier (try/catch). |
@joshua-mcginnis sorry no idea, I don't know typescript specifics but I'd say it should work |
Me or @warpech could Sent from my iPhone
|
Hey guys how about merging this pull request? |
Guys can you take a look at https://github.com/sonnyp/JSON-Patch/compare/validate and tell me if it's okay for you? Please note that it doesn't test for path validity (in JSON Pointer terms). A (better?) alternative for patch validation might be to use JSON Schema. Usage: var patch = MYPATCH;
var lang = {
'PATCH_TYPE': 'Patch document must be an array.',
'OPERATION_TYPE': 'Operation must be an object.',
'OP_VALUE': '"op" property must add, remove, replace, move, copy or test',
'PATH_TYPE': '"path" property must be a string',
'FROM_TYPE': '"from" string property is required for move and copy operations',
'VALUE_REQUIRED': '"value" property is required for add, replace and test operations '
};
//if undefined or falsy, validate will break after first error
var mutltiple = true;
var errors = jsonpatch.validate(patch, multiple);
//error
if (errors.length) {
errors.forEach(function(error) {
//error.error is the message
//error.index is the index of the item that errored (undefined for 'PATCH_TYPE')
if (typeof error.index === 'number')
console.log(patch[error.index] + ' ' + local[error.error]);
else
console.log(local[error.error]);
});
}
//no error
else {
console.log('patch valid');
} |
@sonnyp why don't you want add validation of individual operations as JSON-Patch specification requires? For example for add operation: http://tools.ietf.org/html/rfc6902#page-5
I think we should have robust module for patch applying and be sure consistency |
@vovan22 because in that case you don't know if the path is valid until you get the document to patch. |
@sonnyp ok. But why do you think that this is enough? Look at implementation of another jsonpatch library code https://github.com/bruth/jsonpatch-js/blob/master/jsonpatch.js#L242 as you can see there is AddPatch.apply method that check index bounds which described by JSON-Patch documentation (look at my previous post). Do you think fast-json-patch module don't nesessary to implement? |
Hey guys does anybody could merge this? |
This will be merged in 0.5.1 |
@skotchio I'm not against an other type of validation. |
#29