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-jsonschemaVerified import paths — ran on the pinned version, not inferred.
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.
Call `config({ forceRebuild: true })` before building the schema again: `const config = require('mongoose-schema-jsonschema/config'); config({ forceRebuild: true });`Configure `fieldOptionsMapping` to convert your custom Mongoose options to JSON Schema extension properties (e.g., `x-notes`): `config({ fieldOptionsMapping: { notes: 'x-notes' } });`Always use the subpath import for the `config` function: `const config = require('mongoose-schema-jsonschema/config');`Ensure you call the package with your Mongoose instance: `require('mongoose-schema-jsonschema')(mongoose);`Import `config` from the correct module path: `const config = require('mongoose-schema-jsonschema/config');`