compile: Skip bad keys in json serialization
This commit is contained in:
@ -193,8 +193,8 @@ class FluentQ {
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
ids (key: string, val: string[] | Symbol, opts: T.IdsQuery): this {
|
ids (key: string, val: (string | Symbol)[]): this {
|
||||||
this[kState].push(Q.ids(key, val, opts))
|
this[kState].push(Q.ids(key, val))
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -91,7 +91,19 @@ namespace Q {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function compileUnsafe<TInput extends Record<string, any> = Record<string, any>> (query: Record<string, any>): t.compiledFunction<TInput> {
|
export function compileUnsafe<TInput extends Record<string, any> = Record<string, any>> (query: Record<string, any>): t.compiledFunction<TInput> {
|
||||||
let stringified = JSON.stringify(query, (key, value) => typeof value === 'symbol' ? `###${value.description!}###` : value)
|
let stringified = JSON.stringify(query, (key, value) => {
|
||||||
|
if (typeof value === 'symbol') {
|
||||||
|
return `###${value.description!}###`
|
||||||
|
} else if (key === '__proto__') {
|
||||||
|
return undefined
|
||||||
|
} else if (key === 'constructor' && typeof value === 'object' &&
|
||||||
|
value !== null && value.prototype !== undefined) {
|
||||||
|
return undefined
|
||||||
|
} else {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
const keys: string[] = []
|
const keys: string[] = []
|
||||||
const matches = stringified.match(/"###\w+###"/g)
|
const matches = stringified.match(/"###\w+###"/g)
|
||||||
if (matches === null) {
|
if (matches === null) {
|
||||||
@ -121,6 +133,7 @@ namespace Q {
|
|||||||
export function compile<TInput extends Record<string, any> = Record<string, any>> (query: Record<string, any>): t.compiledFunction<TInput> {
|
export function compile<TInput extends Record<string, any> = Record<string, any>> (query: Record<string, any>): t.compiledFunction<TInput> {
|
||||||
const params: Array<{ path: string[], key: string }> = []
|
const params: Array<{ path: string[], key: string }> = []
|
||||||
traverse(query, [])
|
traverse(query, [])
|
||||||
|
|
||||||
if (params.length === 0) {
|
if (params.length === 0) {
|
||||||
throw new Error('The query does not contain any use of `Q.params`')
|
throw new Error('The query does not contain any use of `Q.params`')
|
||||||
}
|
}
|
||||||
@ -154,11 +167,23 @@ namespace Q {
|
|||||||
export function compileJson<TInput extends Record<string, any> = Record<string, any>> (query: Record<string, any>): t.compiledFunction<TInput> {
|
export function compileJson<TInput extends Record<string, any> = Record<string, any>> (query: Record<string, any>): t.compiledFunction<TInput> {
|
||||||
const params: Array<{ path: string[], key: string }> = []
|
const params: Array<{ path: string[], key: string }> = []
|
||||||
traverse(query, [])
|
traverse(query, [])
|
||||||
|
|
||||||
if (params.length === 0) {
|
if (params.length === 0) {
|
||||||
throw new Error('The query does not contain any use of `Q.params`')
|
throw new Error('The query does not contain any use of `Q.params`')
|
||||||
}
|
}
|
||||||
|
|
||||||
const stringified = JSON.stringify(query, (key, value) => typeof value === 'symbol' ? `###${value.description!}###` : value)
|
const stringified = JSON.stringify(query, (key, value) => {
|
||||||
|
if (typeof value === 'symbol') {
|
||||||
|
return `###${value.description!}###`
|
||||||
|
} else if (key === '__proto__') {
|
||||||
|
return undefined
|
||||||
|
} else if (key === 'constructor' && typeof value === 'object' &&
|
||||||
|
value !== null && value.prototype !== undefined) {
|
||||||
|
return undefined
|
||||||
|
} else {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
return function (input: TInput): Record<string, any> {
|
return function (input: TInput): Record<string, any> {
|
||||||
const q = JSON.parse(stringified)
|
const q = JSON.parse(stringified)
|
||||||
@ -334,13 +359,12 @@ namespace Q {
|
|||||||
return generateValueObject('fuzzy', key, val, opts)
|
return generateValueObject('fuzzy', key, val, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ids (key: string, val: string[] | Symbol, opts: T.IdsQuery): { ids: Record<string, T.IdsQuery> } {
|
export function ids (key: string, val: (string | Symbol)[]): { ids: Record<string, T.IdsQuery> } {
|
||||||
return {
|
return {
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
ids: {
|
ids: {
|
||||||
[key]: {
|
[key]: {
|
||||||
values: val,
|
values: val
|
||||||
...opts
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user