Skip to content

Commit

Permalink
Update .ts files
Browse files Browse the repository at this point in the history
  • Loading branch information
miyconst committed Jun 22, 2015
1 parent aa91850 commit 8eb8e7f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/json-patch-duplex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,25 @@ module jsonpatch {

export var Error = JsonPatchError;

/**
* Recursively checks whether an object has any undefined values inside.
*/
export function hasUndefined(obj:any): boolean {
if (obj === undefined) {
return true;
}

if (typeof obj == "array" || typeof obj == "object") {
for (var i in obj) {
if (hasUndefined(obj[i])) {
return true;
}
}
}

return false;
}

/**
* Validates a single operation. Called from `jsonpatch.validate`. Throws `JsonPatchError` in case of an error.
* @param {object} operation - operation object (patch)
Expand Down Expand Up @@ -661,6 +680,10 @@ module jsonpatch {
throw new JsonPatchError('Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)', 'OPERATION_VALUE_REQUIRED', index, operation, tree);
}

else if ((operation.op === 'add' || operation.op === 'replace' || operation.op === 'test') && hasUndefined(operation.value)) {
throw new JsonPatchError('Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)', 'OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED', index, operation, tree);
}

else if (tree) {
if (operation.op == "add") {
var pathLen = operation.path.split("/").length;
Expand Down
23 changes: 23 additions & 0 deletions src/json-patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,25 @@ module jsonpatch {

export var Error = JsonPatchError;

/**
* Recursively checks whether an object has any undefined values inside.
*/
export function hasUndefined(obj:any): boolean {
if (obj === undefined) {
return true;
}

if (typeof obj == "array" || typeof obj == "object") {
for (var i in obj) {
if (hasUndefined(obj[i])) {
return true;
}
}
}

return false;
}

/**
* Validates a single operation. Called from `jsonpatch.validate`. Throws `JsonPatchError` in case of an error.
* @param {object} operation - operation object (patch)
Expand Down Expand Up @@ -318,6 +337,10 @@ module jsonpatch {
throw new JsonPatchError('Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)', 'OPERATION_VALUE_REQUIRED', index, operation, tree);
}

else if ((operation.op === 'add' || operation.op === 'replace' || operation.op === 'test') && hasUndefined(operation.value)) {
throw new JsonPatchError('Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)', 'OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED', index, operation, tree);
}

else if (tree) {
if (operation.op == "add") {
var pathLen = operation.path.split("/").length;
Expand Down

0 comments on commit 8eb8e7f

Please sign in to comment.