{"id":"421ca4083d62bf18","repo":"mongodb/node-mongodb-native","slug":"text-record-may-only-set-any-of-valid-txt-recor","errorCode":null,"errorMessage":"Text record may only set any of: ${VALID_TXT_RECORDS.join(', ')}","messagePattern":"Text record may only set any of: (.+?)","errorType":"exception","errorClass":"MongoParseError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":123,"sourceCode":"  // Use the result of resolving the TXT record and add options from there if they exist.\n  let record;\n  try {\n    record = await txtResolutionPromise;\n  } catch (error) {\n    if (error.code !== 'ENODATA' && error.code !== 'ENOTFOUND') {\n      throw error;\n    }\n    return hostAddresses;\n  }\n\n  if (record.length > 1) {\n    throw new MongoParseError('Multiple text records not allowed');\n  }\n\n  const txtRecordOptions = new URLSearchParams(record[0].join(''));\n  const txtRecordOptionKeys = [...txtRecordOptions.keys()];\n  if (txtRecordOptionKeys.some(key => !VALID_TXT_RECORDS.includes(key))) {\n    throw new MongoParseError(`Text record may only set any of: ${VALID_TXT_RECORDS.join(', ')}`);\n  }\n\n  if (VALID_TXT_RECORDS.some(option => txtRecordOptions.get(option) === '')) {\n    throw new MongoParseError('Cannot have empty URI params in DNS TXT Record');\n  }\n\n  const source = txtRecordOptions.get('authSource') ?? undefined;\n  const replicaSet = txtRecordOptions.get('replicaSet') ?? undefined;\n  const loadBalanced = txtRecordOptions.get('loadBalanced') ?? undefined;\n\n  if (\n    !options.userSpecifiedAuthSource &&\n    source &&\n    options.credentials &&\n    !AUTH_MECHS_AUTH_SRC_EXTERNAL.has(options.credentials.mechanism)\n  ) {\n    options.credentials = MongoCredentials.merge(options.credentials, { source });\n  }","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/connection_string.ts#L105-L141","documentation":"Thrown by resolveSRVRecord() when a TXT record (for a mongodb+srv:// URI) contains a key that is not in the allowed set. The driver only honors authSource, replicaSet, and loadBalanced from the TXT record. Any other key produces a MongoParseError listing the valid options.","triggerScenarios":"A TXT record carrying keys like retryWrites, ssl, appName, or tls that the driver does not permit via DNS; a stale TXT record from an older driver expectation; an admin who assumed all URI options are allowed in TXT.","commonSituations":"Atlas or self-managed DNS where someone added retryWrites=true to the TXT record; migrating options into DNS that belong only in the URI query string; copying full connection-string options into the TXT record.","solutions":["Inspect the TXT record: dig <host> TXT and read the keys.","Keep only authSource, replicaSet, and/or loadBalanced in the TXT record.","Move disallowed options (retryWrites, appName, tls, etc.) into the URI query string instead.","Re-query DNS after the change to confirm propagation."],"exampleFix":"// DNS TXT before: \"authSource=admin&retryWrites=true\"\n// DNS TXT after:  \"authSource=admin\"\n// URI: mongodb+srv://host/db?retryWrites=true","handlingStrategy":"validation","validationCode":"import { promises as dns } from 'dns';\nconst ALLOWED = ['authSource', 'replicaSet', 'loadBalanced'];\nasync function validateTxtKeys(host: string): Promise<void> {\n  const records = await dns.resolveTxt(host);\n  if (!records.length) return;\n  const params = new URLSearchParams(records[0].join(''));\n  for (const key of params.keys()) {\n    if (!ALLOWED.includes(key)) {\n      throw new Error(`TXT record key '${key}' is not allowed by the driver`);\n    }\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  await client.connect();\n} catch (e) {\n  if (e instanceof MongoParseError && /Text record may only set/.test(e.message)) {\n    throw new Error('Move non-allowed options out of the DNS TXT record into the URI');\n  }\n  throw e;\n}","preventionTips":["Only put authSource, replicaSet, loadBalanced in the TXT record.","Put all other options in the URI query string.","Document the allowed set for whoever manages DNS."],"tags":["connection-string","dns","srv","config"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}