krisk/Fuse · error · Error

Invalid value for key ${key}

Error message

Invalid value for key ${key}

What it means

When parsing a logical/field query, each key's value must be a valid query shape for that key. If a value cannot be compiled into a valid field query (wrong type for the operator or key path), Fuse throws `Invalid value for key ${key}` naming the offending key.

Source

Thrown at src/core/queryParser.ts:127

      if (isObjectLike(value) && !isArray(value)) {
        const obj: ParsedLeaf = {
          keyId: createKeyId(key),
          fieldQuery: value
        }

        if (auto) {
          const compile = getObjectCompiler()
          if (!compile) {
            throw new Error(ErrorMsg.OBJECT_QUERY_UNAVAILABLE)
          }
          const keyPath = isArray(key) ? key.join('.') : String(key)
          obj.searcher = compile(value, keyPath, options)
        }

        return obj
      }

      throw new Error(ErrorMsg.LOGICAL_SEARCH_INVALID_QUERY_FOR_KEY(key))
    }

    const node: ParsedOperator = {
      children: [],
      operator: keys[0]
    }

    keys.forEach((key) => {
      const value = query[key]

      if (isArray(value)) {
        value.forEach((item: any) => {
          node.children.push(next(item))
        })
      }
    })

    return node

View on GitHub (pinned to edf2fb608e)

Solutions

  1. Ensure each field's value is a string pattern or a valid extended-search operator object ({ $eq: ... }, { $contains: ... }).
  2. Validate/log the query object before calling search; guard builders against undefined values.
  3. Enable extended search so operator objects are recognized instead of rejected.

Example fix

// before
fuse.search({ $and: [{ title: undefined }] })
// after
const title = getTitle() ?? ''
fuse.search({ $and: [{ title }] })
Defensive patterns

Strategy: validation

Validate before calling

const isValidFieldValue = (v) =>
  typeof v === 'string' || (v !== null && typeof v === 'object' && !Array.isArray(v))
for (const [key, value] of Object.entries(query)) {
  if (!isValidFieldValue(value)) throw new TypeError(`Invalid value for key ${key}`)
}
fuse.search(query)

Type guard

const isValidFieldValue = (v) =>
  typeof v === 'string' || (v !== null && typeof v === 'object' && !Array.isArray(v))

Try / catch

try {
  return fuse.search(query)
} catch (e) {
  if (e.message.startsWith('Invalid value for key')) {
    console.warn('Dropping malformed logical query:', e.message)
    return []
  }
  throw e
}

Prevention

When it happens

Trigger: fuse.search({ $and: [{ title: 42 }] }) or a field value that is neither a string nor a recognized operator object — e.g. { author: true }, { tags: null }, or a nested structure the parser cannot interpret (often from a programmatic builder producing undefined).

Common situations: Building queries programmatically where a variable ends up undefined/null; typos in operator names leaving unrecognized objects; passing numbers/booleans where string patterns are expected.

Related errors


AI-assisted analysis of krisk/Fuse@edf2fb608e (2026-09-02). Data as JSON: /api/errors/f30b1ae32e0070b7. Report an issue: GitHub.