From 8eb8e7f2a1a721ced65eb2f29bbd8c0f471c1770 Mon Sep 17 00:00:00 2001 From: miyconst Date: Mon, 22 Jun 2015 15:42:55 +0300 Subject: [PATCH] Update .ts files https://github.com/Starcounter-Jack/JSON-Patch/issues/73 --- src/json-patch-duplex.ts | 23 +++++++++++++++++++++++ src/json-patch.ts | 23 +++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/json-patch-duplex.ts b/src/json-patch-duplex.ts index 45151a8a..837bb3d3 100644 --- a/src/json-patch-duplex.ts +++ b/src/json-patch-duplex.ts @@ -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) @@ -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; diff --git a/src/json-patch.ts b/src/json-patch.ts index 8afa78bf..6c81fa71 100644 --- a/src/json-patch.ts +++ b/src/json-patch.ts @@ -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) @@ -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;