Automattic/mongoose · error · MongooseError

Converting unsupported SchemaType to JSON Schema: ${this.ins

Error message

Converting unsupported SchemaType to JSON Schema: ${this.instance} at path "${this.path}"

What it means

The base SchemaType.toJSONSchema() always throws; every convertible type (String, Number, ObjectId, arrays, subdocuments, ...) overrides it. This error means the schema tree contains at least one SchemaType without a JSON-Schema representation — typically Schema.Types.Mixed, a Map with a non-convertible value type, or a custom SchemaType subclass.

Source

Thrown at lib/schemaType.js:1930

/*!
 * If _duplicateKeyErrorMessage is a string, replace unique index errors "E11000 duplicate key error" with this string.
 *
 * @api private
 */

SchemaType.prototype._duplicateKeyErrorMessage = null;

/**
 * Returns this schema type's representation in a JSON schema.
 *
 * @param {object} [options]
 * @param {boolean} [options.useBsonType=false] If true, return a representation with `bsonType` for use with MongoDB's `$jsonSchema`.
 * @returns {object} JSON schema properties
 */

SchemaType.prototype.toJSONSchema = function toJSONSchema(_options) { // eslint-disable-line no-unused-vars
  throw new MongooseError(`Converting unsupported SchemaType to JSON Schema: ${this.instance} at path "${this.path}"`);
};

/**
 * Returns the BSON type that the schema corresponds to, for automatic encryption.
 * @api private
 */
SchemaType.prototype.autoEncryptionType = function autoEncryptionType() {
  return null;
};

/*!
 * Module exports.
 */

module.exports = exports = SchemaType;

exports.CastError = CastError;

View on GitHub (pinned to 49cdab0136)

Solutions

  1. Replace Mixed paths with concrete types ({ type: Map, of: String }, subdocuments, or defined keys)
  2. For custom SchemaTypes, override toJSONSchema(options) to return the type's JSON-Schema object
  3. If Mixed is unavoidable, catch the error and generate the schema for the convertible subset only

Example fix

// before
const schema = new Schema({ data: Schema.Types.Mixed });
schema.toJSONSchema(); // throws
// after
const schema = new Schema({ data: { type: Map, of: String } });
schema.toJSONSchema();
Defensive patterns

Strategy: try-catch

Validate before calling

function schemaHasMixed(schema) {
  return [...schema.paths.values()].some(p =>
    p.instance === 'Mixed' ||
    (p.$isMongooseMap && p.$__schemaType && p.$__schemaType.instance === 'Mixed'));
}
if (!schemaHasMixed(schema)) { schema.toJSONSchema(); } else { /* replace Mixed first */ }

Type guard

function isJSONSchemaConvertible(schema) { return !schemaHasMixed(schema); }

Try / catch

try { return schema.toJSONSchema(); } catch (err) { if (/Converting unsupported SchemaType/.test(err.message)) { log.warn(`Skipping JSON schema for ${schema.modelName}: ${err.message}`); return null; } throw err; }

Prevention

When it happens

Trigger: Calling schema.toJSONSchema() (with or without { useBsonType: true }) on a schema containing a Mixed path, e.g. new Schema({ data: Schema.Types.Mixed }); defining a custom class extending SchemaType without implementing toJSONSchema.

Common situations: Generating JSON Schema for MongoDB $jsonSchema validators, API documentation, or runtime validation; schemas that use Mixed for flexible payloads; upgrading to a Mongoose version where toJSONSchema() was introduced.

Related errors


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