Registry / database / mongoose-schema-jsonschema

mongoose-schema-jsonschema

JSON →
library4.0.1jsnpmunverified

This package extends Mongoose's core classes (`Schema`, `Model`, and `Query`) to automatically generate JSON Schema representations from existing Mongoose definitions. It provides a `jsonSchema()` method accessible directly on Mongoose schemas, models, and queries, streamlining the creation of JSON Schemas for API documentation (e.g., OpenAPI), data validation, or UI generation. The current stable version is 4.0.1, indicating active maintenance and compatibility with Mongoose versions 6.x through 9.x. Key differentiators include its direct integration into Mongoose prototypes, a caching mechanism for built schemas with an explicit `forceRebuild` option, and configurable mapping for custom Mongoose field options to JSON Schema extensions (`fieldOptionsMapping`). The package typically releases new versions as needed to support newer Mongoose versions or JSON Schema specification updates.

npm install mongoose-schema-jsonschema
INSTALL
IMPORT
SIG · MONGOOSE-SCHEMA-JS
M
mongoose-schema-jsonschema
databasejavascriptv4.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

jsonSchema (extension)
const mongoose = require('mongoose'); require('mongoose-schema-jsonschema')(mongoose);
import mongooseSchema from 'mongoose-schema-jsonschema'; mongooseSchema();
The primary usage is to invoke the package's default export with your Mongoose instance to extend its prototypes. In ESM, you would import the default and then call it with your `mongoose` object.
config
const config = require('mongoose-schema-jsonschema/config');
import { config } from 'mongoose-schema-jsonschema';
The configuration function for schema generation options (like `forceRebuild` or `fieldOptionsMapping`) is exposed as a named export from a specific subpath, not the main package entry.

This quickstart demonstrates how to extend Mongoose and then generate a JSON Schema from a simple Mongoose Schema definition, showcasing basic type mapping and required fields.

const mongoose = require('mongoose'); require('mongoose-schema-jsonschema')(mongoose); const Schema = mongoose.Schema; const BookSchema = new Schema({ title: { type: String, required: true, minLength: 3 }, year: { type: Number, min: 1900, max: new Date().getFullYear() }, author: { type: String, required: true }, tags: [{ type: String }], meta: { isbn: String, pages: Number } }); const jsonSchema = BookSchema.jsonSchema(); console.dir(jsonSchema, { depth: null }); /* Example Output: { type: 'object', properties: { title: { type: 'string', minLength: 3 }, year: { type: 'number', minimum: 1900, maximum: 2024 }, author: { type: 'string' }, tags: { type: 'array', items: { type: 'string' } }, meta: { type: 'object', properties: { isbn: { type: 'string' }, pages: { type: 'number' } }, required: [] }, _id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' } }, required: [ 'title', 'author' ] } */
Debug
Known issues
gotchaThe package caches generated JSON schemas for performance. If you modify a Mongoose schema after its JSON Schema has already been built, you must use the `forceRebuild` option in the `config` function to ensure the updated schema is generated.
fix
Call `config({ forceRebuild: true })` before building the schema again: `const config = require('mongoose-schema-jsonschema/config'); config({ forceRebuild: true });`
affects: >=1.4.0
gotchaWhen using custom options in Mongoose schema field definitions (e.g., `notes: 'Some note'`), these will not be included in the generated JSON Schema by default. You need to explicitly map them using `fieldOptionsMapping`.
fix
Configure `fieldOptionsMapping` to convert your custom Mongoose options to JSON Schema extension properties (e.g., `x-notes`): `config({ fieldOptionsMapping: { notes: 'x-notes' } });`
affects: >=1.4.0
breakingPrevious versions might have allowed direct imports of `config` or other utilities from the main package. Since version 1.4.0, the `config` function is accessed via a specific subpath import (`require('mongoose-schema-jsonschema/config')`).
fix
Always use the subpath import for the `config` function: `const config = require('mongoose-schema-jsonschema/config');`
affects: >=1.4.0
Errors
Common errors & fixes
TypeError: BookSchema.jsonSchema is not a function
The `mongoose-schema-jsonschema` package was imported but not invoked with the Mongoose instance, meaning the `jsonSchema` method was not added to the Mongoose prototypes.
fix
Ensure you call the package with your Mongoose instance: `require('mongoose-schema-jsonschema')(mongoose);`
ReferenceError: config is not defined
The `config` function was used without being correctly imported from its specific subpath.
fix
Import `config` from the correct module path: `const config = require('mongoose-schema-jsonschema/config');`
Upgrade
Version history
4.0.1latest on npm
Audit
Dependencies
mongooserequiredThis package extends Mongoose prototypes, requiring an active Mongoose instance to function.
Agent activity
2 hits · last 30 days
node
2
Resources
mongoose-schema-jsonschema — npm install mongoose-schema-jsonschema · libregistry