{"record":{"id":"f950d373174838c4","repo":"Automattic/mongoose","slug":"converting-unsupported-schematype-to-json-schema","errorCode":null,"errorMessage":"Converting unsupported SchemaType to JSON Schema: ${this.instance} at path \"${this.path}\"","messagePattern":"Converting unsupported SchemaType to JSON Schema: (.+?) at path \"(.+?)\"","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/schemaType.js","lineNumber":1930,"sourceCode":"\n/*!\n * If _duplicateKeyErrorMessage is a string, replace unique index errors \"E11000 duplicate key error\" with this string.\n *\n * @api private\n */\n\nSchemaType.prototype._duplicateKeyErrorMessage = null;\n\n/**\n * Returns this schema type's representation in a JSON schema.\n *\n * @param {object} [options]\n * @param {boolean} [options.useBsonType=false] If true, return a representation with `bsonType` for use with MongoDB's `$jsonSchema`.\n * @returns {object} JSON schema properties\n */\n\nSchemaType.prototype.toJSONSchema = function toJSONSchema(_options) { // eslint-disable-line no-unused-vars\n  throw new MongooseError(`Converting unsupported SchemaType to JSON Schema: ${this.instance} at path \"${this.path}\"`);\n};\n\n/**\n * Returns the BSON type that the schema corresponds to, for automatic encryption.\n * @api private\n */\nSchemaType.prototype.autoEncryptionType = function autoEncryptionType() {\n  return null;\n};\n\n/*!\n * Module exports.\n */\n\nmodule.exports = exports = SchemaType;\n\nexports.CastError = CastError;\n","sourceCodeStart":1912,"sourceCodeEnd":1948,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/schemaType.js#L1912-L1948","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nconst schema = new Schema({ data: Schema.Types.Mixed });\nschema.toJSONSchema(); // throws\n// after\nconst schema = new Schema({ data: { type: Map, of: String } });\nschema.toJSONSchema();","handlingStrategy":"try-catch","validationCode":"function schemaHasMixed(schema) {\n  return [...schema.paths.values()].some(p =>\n    p.instance === 'Mixed' ||\n    (p.$isMongooseMap && p.$__schemaType && p.$__schemaType.instance === 'Mixed'));\n}\nif (!schemaHasMixed(schema)) { schema.toJSONSchema(); } else { /* replace Mixed first */ }","typeGuard":"function isJSONSchemaConvertible(schema) { return !schemaHasMixed(schema); }","tryCatchPattern":"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; }","preventionTips":["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"],"tags":["mongoose","json-schema","mixed","schema-introspection"],"backgroundTag":"json-schema-conversion-failed","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}