Skip to content

Commit

Permalink
fix: use JSON.stringify/parse to clone queries (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackmann authored Dec 27, 2023
1 parent f9f04fd commit 9604b9b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
5 changes: 5 additions & 0 deletions base/src/lib/clone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function clone<T>(obj: T) {
return JSON.parse(JSON.stringify(obj)) as T
}

export { clone }
4 changes: 1 addition & 3 deletions base/src/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,9 +861,7 @@ describe('schema', () => {
expect(
schema.castQuery({ createdAt: { $gte: undefined } })
).toStrictEqual({
createdAt: {
$gte: undefined,
},
createdAt: {},
})

expect(schema.castQuery({ createdAt: { $gte: true } })).toStrictEqual({
Expand Down
6 changes: 4 additions & 2 deletions base/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { clone } from './lib/clone.js'

const types = [
'any',
'array',
Expand Down Expand Up @@ -157,7 +159,7 @@ class Schema {
* With this knowledge, only nested query operators (eg. $in) are also considered.
*/
castQuery(query: Record<string, any>) {
const res = structuredClone(query)
const res = clone(query)

for (const [key, value] of Object.entries(res)) {
const definition = this.getDefinitionAtPath(key)
Expand Down Expand Up @@ -259,7 +261,7 @@ class Schema {
throw new Error('`data` is undefined')
}

const res = structuredClone(data)
const res = clone(data)

for (const [key, definition] of Object.entries(this.schema)) {
const value = res[key]
Expand Down

0 comments on commit 9604b9b

Please sign in to comment.