{"id":"f54e24b923cccbbb","repo":"mongodb/node-mongodb-native","slug":"authmechanism-one-of-mechanisms-got-value","errorCode":null,"errorMessage":"authMechanism one of ${mechanisms}, got ${value}","messagePattern":"authMechanism one of (.+?), got (.+?)","errorType":"exception","errorClass":"MongoParseError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":677,"sourceCode":"    transform({ name, options, values: [value] }): MongoCredentials {\n      if (!isRecord(value, ['username', 'password'] as const)) {\n        throw new MongoParseError(\n          `${name} must be an object with 'username' and 'password' properties`\n        );\n      }\n      return MongoCredentials.merge(options.credentials, {\n        username: value.username,\n        password: value.password\n      });\n    }\n  },\n  authMechanism: {\n    target: 'credentials',\n    transform({ options, values: [value] }): MongoCredentials {\n      const mechanisms = Object.values(AuthMechanism);\n      const [mechanism] = mechanisms.filter(m => m.match(RegExp(String.raw`\\b${value}\\b`, 'i')));\n      if (!mechanism) {\n        throw new MongoParseError(`authMechanism one of ${mechanisms}, got ${value}`);\n      }\n      let source = options.credentials?.source;\n      if (\n        mechanism === AuthMechanism.MONGODB_PLAIN ||\n        AUTH_MECHS_AUTH_SRC_EXTERNAL.has(mechanism)\n      ) {\n        // some mechanisms have '$external' as the Auth Source\n        source = '$external';\n      }\n\n      let password = options.credentials?.password;\n      if (mechanism === AuthMechanism.MONGODB_X509 && password === '') {\n        password = undefined;\n      }\n      return MongoCredentials.merge(options.credentials, {\n        mechanism,\n        source,\n        password","sourceCodeStart":659,"sourceCodeEnd":695,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/connection_string.ts#L659-L695","documentation":"The `authMechanism` option must match one of the `AuthMechanism` enum values (DEFAULT, GSSAPI, PLAIN, SCRAM-SHA-1, SCRAM-SHA-256, MONGODB-X509, MONGODB-AWS, MONGODB-OIDC). The transform (src/connection_string.ts:671-698) matches the value case-insensitively as a word boundary inside each enum value; if no enum value contains it, it throws.","triggerScenarios":"`{ authMechanism: 'LDAP' }` (LDAP uses 'PLAIN'); `{ authMechanism: 'scram' }` (ambiguous — must be exactly 'SCRAM-SHA-1' or 'SCRAM-SHA-256'); typos like 'MONGODB-AWS-OLD' or 'X509'.","commonSituations":"Confusing mechanism names with auth-source names; treating LDAP and PLAIN as distinct mechanisms; copy-paste typos; guessing that 'X509' alone works.","solutions":["Use an exact AuthMechanism value: 'SCRAM-SHA-256', 'SCRAM-SHA-1', 'MONGODB-X509', 'MONGODB-AWS', 'MONGODB-OIDC', 'PLAIN', 'GSSAPI', or 'DEFAULT'","For LDAP, use 'PLAIN'","Omit authMechanism to let the driver negotiate SCRAM-SHA-1/256 automatically"],"exampleFix":"// before\nnew MongoClient(uri, { authMechanism: 'LDAP', authSource: '$external' });\n// after\nnew MongoClient(uri, { authMechanism: 'PLAIN', authSource: '$external' });","handlingStrategy":"validation","validationCode":"import { AuthMechanism } from 'mongodb';\nconst VALID = new Set(Object.values(AuthMechanism));\nfunction isValidMechanism(v) {\n  return typeof v === 'string' && [...VALID].some(m => new RegExp(`\\\\b${v}\\\\b`, 'i').test(m));\n}\nif (options.authMechanism && !isValidMechanism(options.authMechanism)) {\n  throw new TypeError(`Invalid authMechanism: ${options.authMechanism}`);\n}","typeGuard":"import { AuthMechanism } from 'mongodb';\nfunction isAuthMechanism(v) {\n  return Object.values(AuthMechanism).includes(v);\n}","tryCatchPattern":null,"preventionTips":["Import AuthMechanism and use the enum constant instead of a string literal","Remember LDAP maps to 'PLAIN'","Omit authMechanism to let the driver negotiate SCRAM automatically"],"tags":["authentication","configuration","options"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}