Skip to content

Commit d84f482

Browse files
committed
fix(protect): use canonical JSONPath format for root-level array indices
- Handle paths starting with '[' to produce $[0] instead of $.[0] - Update test expectation for root-level array index - Add edge case tests: large indices, deeply nested paths, nested arrays
1 parent 9040555 commit d84f482

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

packages/protect/__tests__/jsonb-helpers.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,19 @@ describe('toJsonPath', () => {
5151
})
5252

5353
it('handles array index at root level', () => {
54-
expect(toJsonPath('[0].name')).toBe('$.[0].name')
54+
expect(toJsonPath('[0].name')).toBe('$[0].name')
55+
})
56+
57+
it('handles large array index', () => {
58+
expect(toJsonPath('items[999].value')).toBe('$.items[999].value')
59+
})
60+
61+
it('handles deeply nested path after array index', () => {
62+
expect(toJsonPath('data[0].user.profile.settings')).toBe('$.data[0].user.profile.settings')
63+
})
64+
65+
it('handles root array with nested array', () => {
66+
expect(toJsonPath('[0].items[1].name')).toBe('$[0].items[1].name')
5567
})
5668
})
5769

packages/protect/src/helpers/jsonb.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export function toJsonPath(path: string): string {
2020
if (path.startsWith('$.')) return path
2121
if (path.startsWith('$')) return `$.${path.slice(1)}`
2222
if (path.startsWith('.')) return `$${path}`
23+
if (path.startsWith('[')) return `$${path}`
2324
return `$.${path}`
2425
}
2526

0 commit comments

Comments
 (0)