hcengineering/platform · error · Error

$all predicate requires array

Error message

$all predicate requires array

What it means

The $all predicate matches documents whose array property contains every element of the given list, so its argument must be an array. predicate.ts validates this immediately when the predicate is created and throws this error otherwise. It is an argument-validation failure, not a data failure.

Source

Thrown at foundations/core/packages/core/src/predicate.ts:56

const predicates: Record<string, PredicateFactory> = {
  $in: (o, propertyKey) => {
    if (!Array.isArray(o)) {
      throw new Error('$in predicate requires array')
    }
    return (docs) =>
      execPredicate(docs, propertyKey, (value) => {
        if (Array.isArray(value)) {
          return o.some((p) => value.includes(p))
        } else {
          // eslint-disable-next-line eqeqeq
          return o.some((p) => p == value)
        }
      })
  },
  $all: (o, propertyKey) => {
    if (!Array.isArray(o)) {
      throw new Error('$all predicate requires array')
    }
    return (docs) =>
      execPredicate(docs, propertyKey, (value: any[]) => {
        for (const val of o) {
          if (!value.includes(val)) return false
        }
        return true
      })
  },
  $nin: (o, propertyKey) => {
    if (!Array.isArray(o)) {
      throw new Error('$nin predicate requires array')
    }
    return (docs) =>
      execPredicate(docs, propertyKey, (value) => {
        if (Array.isArray(value)) {
          return !o.some((p) => value.includes(p))
        } else {

View on GitHub (pinned to 63e28dc964)

Solutions

  1. Pass an explicit array: { $all: ['red', 'blue'] }.
  2. Coerce Sets/iterables with Array.from before querying.
  3. Default missing lists to an empty array or skip the predicate when the list is absent.

Example fix

// before
const q = { tags: { $all: new Set(['a', 'b']) } }
// after
const q = { tags: { $all: Array.from(new Set(['a', 'b'])) } }
Defensive patterns

Strategy: validation

Validate before calling

const all: unknown = requiredTags
if (!Array.isArray(all)) throw new TypeError('$all expects an array, got: ' + typeof all)
const q = { tags: { $all: all } }

Type guard

function isReadonlyArray(v: unknown): v is readonly unknown[] {
  return Array.isArray(v)
}

Try / catch

try {
  return await find(clazz, { tags: { $all: tags } })
} catch (e) {
  if (e instanceof Error && e.message.startsWith('$all predicate requires array')) {
    return [] // or retry with normalized [tags]
  }
  throw e
}

Prevention

When it happens

Trigger: Queries like { tags: { $all: 'red' } } (scalar instead of array) or { tags: { $all: requiredTags } } where requiredTags is undefined, null, or an object such as a Set spread incorrectly.

Common situations: Forgetting to wrap a single required value in an array; a tags/roles list coming from configuration or a remote call as undefined; converting a Set to a query without Array.from.

Related errors


AI-assisted analysis of hcengineering/platform@63e28dc964 (2026-08-29). Data as JSON: /api/errors/b0f44741bf2c9d52. Report an issue: GitHub.