Automattic/mongoose · error · Error

encrypted fields cannot be declared on both the base schema

Error message

encrypted fields cannot be declared on both the base schema and the child schema in a discriminator. path=${path}

What it means

Error "encrypted fields cannot be declared on both the base schema and the child schema in a discriminator. path=${path}" thrown in Automattic/mongoose.

Source

Thrown at lib/helpers/model/discriminator.js:40

 * Validate fields declared on the child schema when either schema is configured for encryption.  Specifically, this function ensures that:
 *
 * - any encrypted fields are declared on exactly one of the schemas (not both)
 * - encrypted fields cannot be declared on either the parent or child schema, where the other schema declares the same field without encryption.
 *
 * @param {Schema} parentSchema
 * @param {Schema} childSchema
 */
function validateDiscriminatorSchemasForEncryption(parentSchema, childSchema) {
  if (parentSchema.encryptionType() == null && childSchema.encryptionType() == null) return;

  const allSharedNestedPaths = setIntersection(
    allNestedPaths(parentSchema),
    allNestedPaths(childSchema)
  );

  for (const path of allSharedNestedPaths) {
    if (parentSchema._hasEncryptedField(path) && childSchema._hasEncryptedField(path)) {
      throw new Error(`encrypted fields cannot be declared on both the base schema and the child schema in a discriminator. path=${path}`);
    }

    if (parentSchema._hasEncryptedField(path) || childSchema._hasEncryptedField(path)) {
      throw new Error(`encrypted fields cannot have the same path as a non-encrypted field for discriminators. path=${path}`);
    }
  }

  function allNestedPaths(schema) {
    return [...Object.keys(schema.paths), ...Object.keys(schema.singleNestedPaths)];
  }

  /**
   * @param {Iterable<string>} i1
   * @param {Iterable<string>} i2
   */
  function* setIntersection(i1, i2) {
    const s1 = new Set(i1);
    for (const item of i2) {

View on GitHub (pinned to 49cdab0136)

When it happens

Trigger: Thrown at lib/helpers/model/discriminator.js:40 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of Automattic/mongoose@49cdab0136 (2026-08-21). Data as JSON: /api/errors/d0779e112e2fdb21. Report an issue: GitHub.