Automattic/mongoose · error · Error

Can't customize discriminator option ${_key} (can only modif

Error message

Can't customize discriminator option ${_key} (can only modify ${Object.keys(CUSTOMIZABLE_DISCRIMINATOR_OPTIONS).join(', ')})

What it means

Error "Can't customize discriminator option ${_key} (can only modify ${Object.keys(CUSTOMIZABLE_DISCRIMINATOR_OPTIONS).join(', ')})" thrown in Automattic/mongoose.

Source

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

    if (baseSchema.options.collection) {
      schema.options.collection = baseSchema.options.collection;
    }
    const toJSON = schema.options.toJSON;
    const toObject = schema.options.toObject;
    const _id = schema.options._id;
    const id = schema.options.id;

    const keys = Object.keys(schema.options);
    schema.options.discriminatorKey = baseSchema.options.discriminatorKey;
    const userProvidedOptions = schema._userProvidedOptions;
    for (const _key of keys) {
      if (!CUSTOMIZABLE_DISCRIMINATOR_OPTIONS[_key]) {
        // Use `schema.options` in `deepEqual()` because of `discriminatorKey`
        // set above. We don't allow customizing discriminator key, always
        // overwrite. See gh-9238
        if (_key in userProvidedOptions && !utils.deepEqual(schema.options[_key], baseSchema.options[_key])) {
          throw new Error('Can\'t customize discriminator option ' + _key +
            ' (can only modify ' +
            Object.keys(CUSTOMIZABLE_DISCRIMINATOR_OPTIONS).join(', ') +
            ')');
        }
      }
    }
    schema.options = clone(baseSchema.options);

    for (const _key of Object.keys(userProvidedOptions)) {
      schema.options[_key] = userProvidedOptions[_key];
    }
    if (toJSON) schema.options.toJSON = toJSON;
    if (toObject) schema.options.toObject = toObject;
    if (typeof _id !== 'undefined') {
      schema.options._id = _id;
    }
    schema.options.id = id;
    if (mergeHooks) {

View on GitHub (pinned to 49cdab0136)

When it happens

Trigger: Thrown at lib/helpers/model/discriminator.js:216 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/322e86f867d575a0. Report an issue: GitHub.