{"record":{"id":"f30b1ae32e0070b7","repo":"krisk/Fuse","slug":"invalid-value-for-key-key","errorCode":null,"errorMessage":"Invalid value for key ${key}","messagePattern":"Invalid value for key (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/core/queryParser.ts","lineNumber":127,"sourceCode":"      if (isObjectLike(value) && !isArray(value)) {\n        const obj: ParsedLeaf = {\n          keyId: createKeyId(key),\n          fieldQuery: value\n        }\n\n        if (auto) {\n          const compile = getObjectCompiler()\n          if (!compile) {\n            throw new Error(ErrorMsg.OBJECT_QUERY_UNAVAILABLE)\n          }\n          const keyPath = isArray(key) ? key.join('.') : String(key)\n          obj.searcher = compile(value, keyPath, options)\n        }\n\n        return obj\n      }\n\n      throw new Error(ErrorMsg.LOGICAL_SEARCH_INVALID_QUERY_FOR_KEY(key))\n    }\n\n    const node: ParsedOperator = {\n      children: [],\n      operator: keys[0]\n    }\n\n    keys.forEach((key) => {\n      const value = query[key]\n\n      if (isArray(value)) {\n        value.forEach((item: any) => {\n          node.children.push(next(item))\n        })\n      }\n    })\n\n    return node","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/krisk/Fuse/blob/edf2fb608eca0461508d1d71317e6e58309ffada/src/core/queryParser.ts#L109-L145","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Ensure each field's value is a string pattern or a valid extended-search operator object ({ $eq: ... }, { $contains: ... }).","Validate/log the query object before calling search; guard builders against undefined values.","Enable extended search so operator objects are recognized instead of rejected."],"exampleFix":"// before\nfuse.search({ $and: [{ title: undefined }] })\n// after\nconst title = getTitle() ?? ''\nfuse.search({ $and: [{ title }] })","handlingStrategy":"validation","validationCode":"const isValidFieldValue = (v) =>\n  typeof v === 'string' || (v !== null && typeof v === 'object' && !Array.isArray(v))\nfor (const [key, value] of Object.entries(query)) {\n  if (!isValidFieldValue(value)) throw new TypeError(`Invalid value for key ${key}`)\n}\nfuse.search(query)","typeGuard":"const isValidFieldValue = (v) =>\n  typeof v === 'string' || (v !== null && typeof v === 'object' && !Array.isArray(v))","tryCatchPattern":"try {\n  return fuse.search(query)\n} catch (e) {\n  if (e.message.startsWith('Invalid value for key')) {\n    console.warn('Dropping malformed logical query:', e.message)\n    return []\n  }\n  throw e\n}","preventionTips":["Sanitize programmatically built queries (no undefined/null values).","Use only string patterns or valid $-operator objects as field values.","Unit-test query builders with edge-case inputs."],"tags":["query-validation","logical-search","query-parser"],"backgroundTag":"invalid-query-syntax","analyzedSha":"edf2fb608eca0461508d1d71317e6e58309ffada","analyzedAt":"2026-09-02T02:46:54.623Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}