{"record":{"id":"d0779e112e2fdb21","repo":"Automattic/mongoose","slug":"encrypted-fields-cannot-be-declared-on-both-the-ba","errorCode":null,"errorMessage":"encrypted fields cannot be declared on both the base schema and the child schema in a discriminator. path=${path}","messagePattern":"encrypted fields cannot be declared on both the base schema and the child schema in a discriminator\\. path=(.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/helpers/model/discriminator.js","lineNumber":40,"sourceCode":" * Validate fields declared on the child schema when either schema is configured for encryption.  Specifically, this function ensures that:\n *\n * - any encrypted fields are declared on exactly one of the schemas (not both)\n * - encrypted fields cannot be declared on either the parent or child schema, where the other schema declares the same field without encryption.\n *\n * @param {Schema} parentSchema\n * @param {Schema} childSchema\n */\nfunction validateDiscriminatorSchemasForEncryption(parentSchema, childSchema) {\n  if (parentSchema.encryptionType() == null && childSchema.encryptionType() == null) return;\n\n  const allSharedNestedPaths = setIntersection(\n    allNestedPaths(parentSchema),\n    allNestedPaths(childSchema)\n  );\n\n  for (const path of allSharedNestedPaths) {\n    if (parentSchema._hasEncryptedField(path) && childSchema._hasEncryptedField(path)) {\n      throw new Error(`encrypted fields cannot be declared on both the base schema and the child schema in a discriminator. path=${path}`);\n    }\n\n    if (parentSchema._hasEncryptedField(path) || childSchema._hasEncryptedField(path)) {\n      throw new Error(`encrypted fields cannot have the same path as a non-encrypted field for discriminators. path=${path}`);\n    }\n  }\n\n  function allNestedPaths(schema) {\n    return [...Object.keys(schema.paths), ...Object.keys(schema.singleNestedPaths)];\n  }\n\n  /**\n   * @param {Iterable<string>} i1\n   * @param {Iterable<string>} i2\n   */\n  function* setIntersection(i1, i2) {\n    const s1 = new Set(i1);\n    for (const item of i2) {","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/helpers/model/discriminator.js#L22-L58","documentation":"Error \"encrypted fields cannot be declared on both the base schema and the child schema in a discriminator. path=${path}\" thrown in Automattic/mongoose.","triggerScenarios":"Thrown at lib/helpers/model/discriminator.js:40 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}