-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
qs.parse returns invalid key with brackets when using nested array query #1751
Changes from 3 commits
a3ea344
148551a
ee8d70d
0b25bfb
3494d09
0e3ddc4
83b246e
4ae93ee
5547287
09b2a69
cc52a7b
16cc6d9
eaedefa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -307,9 +307,9 @@ function assertEveryFilter( | |
`${pointer.join('/') || '/'}: every must be an array of Filters`, | ||
); | ||
} else { | ||
filter.every.every((value: any, index: number) => | ||
assertFilter(value, pointer.concat(`[${index}]`)), | ||
); | ||
Comment on lines
-310
to
-312
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
filter.every.forEach((value: any, index: number) => { | ||
assertFilter(value, pointer.concat(`[${index}]`)); | ||
}); | ||
} | ||
} | ||
|
||
|
@@ -348,9 +348,10 @@ function assertEqFilter( | |
if (typeof filter.eq !== 'object' || filter.eq == null) { | ||
throw new Error(`${pointer.join('/') || '/'}: eq must be an object`); | ||
} | ||
Object.entries(filter.eq).every(([key, value]) => | ||
assertJSONValue(value, pointer.concat(key)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
); | ||
Object.entries(filter.eq).forEach(([key, value]) => { | ||
assertKey(key, pointer); | ||
assertJSONValue(value, pointer.concat(key)); | ||
}); | ||
} | ||
|
||
function assertContainsFilter( | ||
|
@@ -371,9 +372,10 @@ function assertContainsFilter( | |
if (typeof filter.contains !== 'object' || filter.contains == null) { | ||
throw new Error(`${pointer.join('/') || '/'}: contains must be an object`); | ||
} | ||
Object.entries(filter.contains).every(([key, value]) => | ||
assertJSONValue(value, pointer.concat(key)), | ||
); | ||
Object.entries(filter.contains).forEach(([key, value]) => { | ||
assertKey(key, pointer); | ||
assertJSONValue(value, pointer.concat(key)); | ||
}); | ||
} | ||
|
||
function assertRangeFilter( | ||
|
@@ -419,3 +421,42 @@ function assertRangeFilter( | |
}); | ||
}); | ||
} | ||
|
||
export function assertKey(key: string, pointer: string[]) { | ||
if (key.startsWith('[') && key.endsWith(']')) { | ||
throw new Error( | ||
`${pointer.join('/')}: field names cannot be wrapped in brackets: ${key}`, | ||
); | ||
} | ||
} | ||
|
||
const removeBrackets = (obj: any): any => { | ||
if (!obj || typeof obj !== 'object') return obj; | ||
|
||
// Handle arrays | ||
if (Array.isArray(obj)) { | ||
return obj.map((item) => removeBrackets(item)); | ||
} | ||
|
||
return Object.entries(obj).reduce((acc, [key, value]) => { | ||
// Remove surrounding brackets if they exist | ||
const newKey = key.replace(/^\[(.*)\]$/, '$1'); | ||
|
||
// Handle arrays in values | ||
if (Array.isArray(value)) { | ||
acc[newKey] = value.map((item) => removeBrackets(item)); | ||
} else if (typeof value === 'object' && value !== null) { | ||
// Recursively removeBrackets nested objects | ||
acc[newKey] = removeBrackets(value); | ||
} else { | ||
// Handle primitive values | ||
acc[newKey] = value; | ||
} | ||
|
||
return acc; | ||
}, {} as any); | ||
}; | ||
|
||
export const parseQuery = (queryString: string) => { | ||
return removeBrackets(qs.parse(queryString)); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant to come up with a better name here