Files
payload/packages/ui/src/elements/WhereBuilder/validateWhereQuery.ts
Alessio Gravili ebd43c7763 feat: pre-compile ui and richtext-lexical with react compiler (#7688)
This noticeably improves performance in the admin panel, for example
when there are multiple richtext editors on one page (& likely
performance in other areas too, though I mainly tested rich text).

The babel plugin currently only optimizes files with a 'use client'
directive at the top - thus we have to make sure to add use client
wherever possible, even if it's imported by a parent client component.

There's one single component that broke when it was compiled using the
React compiler (it stopped being reactive and failed one of our admin
e2e tests):
150808f608
opting out of it completely fixed that issue

Fixes https://github.com/payloadcms/payload/issues/7366
2024-08-19 17:31:36 -04:00

47 lines
1.3 KiB
TypeScript

'use client'
import type { Operator, Where } from 'payload'
import { validOperators } from 'payload/shared'
const validateWhereQuery = (whereQuery): whereQuery is Where => {
if (
whereQuery?.or?.length > 0 &&
whereQuery?.or?.[0]?.and &&
whereQuery?.or?.[0]?.and?.length > 0
) {
// At this point we know that the whereQuery has 'or' and 'and' fields,
// now let's check the structure and content of these fields.
const isValid = whereQuery.or.every((orQuery) => {
if (orQuery.and && Array.isArray(orQuery.and)) {
return orQuery.and.every((andQuery) => {
if (typeof andQuery !== 'object') {
return false
}
const andKeys = Object.keys(andQuery)
// If there are no keys, it's not a valid WhereField.
if (andKeys.length === 0) {
return false
}
for (const key of andKeys) {
const operator = Object.keys(andQuery[key])[0]
// Check if the key is a valid Operator.
if (!operator || !validOperators.includes(operator as Operator)) {
return false
}
}
return true
})
}
return false
})
return isValid
}
return false
}
export default validateWhereQuery