diff --git a/commonjs/helpers.js b/commonjs/helpers.js index 5f2350e..84b06e1 100644 --- a/commonjs/helpers.js +++ b/commonjs/helpers.js @@ -62,6 +62,11 @@ function _deepClone(obj) { exports._deepClone = _deepClone; //3x faster than cached /^\d+$/.test(str) function isInteger(str) { + // A leading zero is not a valid array index per RFC 6901 section 4 + // (an index is "0", or a digit 1-9 followed by digits). + if (str.length > 1 && str.charCodeAt(0) === 48) { + return false; + } var i = 0; var len = str.length; var charCode; diff --git a/module/helpers.mjs b/module/helpers.mjs index 592f560..5fc5c46 100644 --- a/module/helpers.mjs +++ b/module/helpers.mjs @@ -58,6 +58,11 @@ export function _deepClone(obj) { } //3x faster than cached /^\d+$/.test(str) export function isInteger(str) { + // A leading zero is not a valid array index per RFC 6901 section 4 + // (an index is "0", or a digit 1-9 followed by digits). + if (str.length > 1 && str.charCodeAt(0) === 48) { + return false; + } var i = 0; var len = str.length; var charCode; diff --git a/src/helpers.ts b/src/helpers.ts index 8a14629..590c5b3 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -45,6 +45,11 @@ export function _deepClone(obj) { } //3x faster than cached /^\d+$/.test(str) export function isInteger(str: string): boolean { + // A leading zero is not a valid array index per RFC 6901 section 4 + // (an index is "0", or a digit 1-9 followed by digits). + if (str.length > 1 && str.charCodeAt(0) === 48) { + return false; + } let i = 0; const len = str.length; let charCode; diff --git a/test/spec/validateSpec.mjs b/test/spec/validateSpec.mjs index 36941a7..4ee48ac 100644 --- a/test/spec/validateSpec.mjs +++ b/test/spec/validateSpec.mjs @@ -150,6 +150,14 @@ describe('validate', function() { expect(error.name).toBe('OPERATION_NOT_AN_OBJECT'); }); + it('should return an error for an array index with a leading zero', function() { + const error = jsonpatch.validate([ + { op: 'add', path: '/01', value: 9 } + ], [10, 20, 30]); + expect(error instanceof jsonpatch.JsonPatchError).toBe(true); + expect(error.name).toBe('OPERATION_PATH_ILLEGAL_ARRAY_INDEX'); + }); + it('should return an error if the operation "op" property is not a string', function() { const error = jsonpatch.validate([ {