Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions commonjs/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions module/helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions test/spec/validateSpec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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([
{
Expand Down