{"record":{"id":"ba83494b3da27c15","repo":"Automattic/mongoose","slug":"encryptiontype-of-a-nested-schema-must-match-the-e","errorCode":null,"errorMessage":"encryptionType of a nested schema must match the encryption type of the parent schema.","messagePattern":"encryptionType of a nested schema must match the encryption type of the parent schema\\.","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/schema.js","lineNumber":916,"sourceCode":"      } else {\n        // Either the type is non-POJO or we interpret it as Mixed anyway\n        if (prefix) {\n          this.nested[prefix.substring(0, prefix.length - 1)] = true;\n        }\n        this.path(prefix + key, val);\n        if (!val?.instanceOfSchema && utils.isPOJO(val?.discriminators)) {\n          const schemaType = this.path(prefix + key);\n          for (const key in val.discriminators) {\n            schemaType.discriminator(key, val.discriminators[key]);\n          }\n        }\n      }\n    }\n\n    if (val.instanceOfSchema && val.encryptionType() != null) {\n      // schema.add({ field: <instance of encrypted schema> })\n      if (this.encryptionType() != val.encryptionType()) {\n        throw new MongooseError('encryptionType of a nested schema must match the encryption type of the parent schema.');\n      }\n\n      for (const [encryptedField, encryptedFieldConfig] of Object.entries(val.encryptedFields)) {\n        const path = fullPath + '.' + encryptedField;\n        this._addEncryptedField(path, encryptedFieldConfig);\n      }\n    } else if (typeof val === 'object' && 'encrypt' in val) {\n      // schema.add({ field: { type: <schema type>, encrypt: { ... }}})\n      const { encrypt } = val;\n\n      if (this.encryptionType() == null) {\n        throw new MongooseError('encryptionType must be provided');\n      }\n\n      this._addEncryptedField(fullPath, encrypt);\n    } else {\n      // if the field was already encrypted and we re-configure it to be unencrypted, remove\n      // the encrypted field configuration","sourceCodeStart":898,"sourceCodeEnd":934,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/schema.js#L898-L934","documentation":"Error \"encryptionType of a nested schema must match the encryption type of the parent schema.\" thrown in Automattic/mongoose.","triggerScenarios":"Thrown at lib/schema.js:916 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"}