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
- Replace Mixed paths with concrete types ({ type: Map, of: String }, subdocuments, or defined keys)
- For custom SchemaTypes, override toJSONSchema(options) to return the type's JSON-Schema object
- 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
- Avoid Schema.Types.Mixed in schemas earmarked for $jsonSchema validators
- Implement toJSONSchema() overrides on custom SchemaType subclasses from day one
- Run toJSONSchema() in a build step so unsupported types fail before deploy
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
- Arguments must be aggregate pipeline operators
- Invalid addFields() argument. Must be an object
- Invalid project() argument. Must be string or object
- Aggregate `near()` must be called with non-nullish argument
- Aggregate `near()` argument must have a `near` property
AI-assisted analysis of Automattic/mongoose@49cdab0136 (2026-08-21).
Data as JSON: /api/errors/f950d373174838c4.
Report an issue: GitHub.