Registry / database / convert-json-schema-to-mongoose

convert-json-schema-to-mongoose

JSON →
library0.3.2jsnpmunverified

Converts JSON Schema definitions into Mongoose 5 schema objects. Version 0.3.2 is the latest stable release (no recent updates). It handles nested objects, references ($ref), defaults, and formats. Written in TypeScript with type declarations. Alternatives exist but none have more recent activity.

npm install convert-json-schema-to-mongoose
INSTALL
IMPORT
SIG · CONVERT-JSON-SCHEM
C
convert-json-schema-to-mongoose
databasejavascriptv0.3.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

createMongooseSchema
✓ import { createMongooseSchema } from 'convert-json-schema-to-mongoose'
✗ import createMongooseSchema from 'convert-json-schema-to-mongoose'
This is a named export, not default.
createMongooseSchema (CommonJS)
✓ const { createMongooseSchema } = require('convert-json-schema-to-mongoose')
✗ const createMongooseSchema = require('convert-json-schema-to-mongoose').default
CJS destructuring works; the package has no default export.
TypeScript types
✓ import type { MongooseSchema } from 'convert-json-schema-to-mongoose'
✗ import { MongooseSchema } from 'convert-json-schema-to-mongoose'
MongooseSchema may be a type-only export; use type import to avoid runtime issues.

Converts a JSON Schema with a $ref and a property with default into a Mongoose schema object.

import { createMongooseSchema } from 'convert-json-schema-to-mongoose'; const refs = { yep: { type: 'string', pattern: '^\\d{3}$' } }; const schema = { type: 'object', properties: { id: { $ref: 'yep' }, name: { type: 'string', default: 'foo' } } }; const mongooseSchema = createMongooseSchema(refs, schema); console.log(mongooseSchema); // Output: { id: { type: [Function: String] }, name: { type: [Function: String], default: 'foo' } }
Debug
Known issues
gotchaThe function signature expects two arguments: refs and schema. Passing only one argument (schema) will cause an error.
fix
Provide an object for refs (even if empty) as first argument, and schema as second.
affects: >=0.1.0
gotchaDefaults that are functions (e.g., Date.now) must be passed as strings '[Function=Date.now]' to be interpreted correctly.
fix
Use string format '[Function=Date.now]' instead of the actual function.
affects: >=0.3.0
deprecatedThe package targets Mongoose 5; Mongoose 6+ has breaking changes (e.g., strictQuery default changed). The generated schema may not be compatible with Mongoose 6+.
fix
Consider using a more recent library or manually update the schema for Mongoose 6+.
affects: >=0.1.0
gotchaThe package has no type definitions for MongooseSchema; TypeScript users may need to declare their own types.
fix
Add a declaration file or cast the result to 'any' if needed.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'properties')
Calling createMongooseSchema without providing refs as first argument.
fix
Call createMongooseSchema(refs, schema) where refs is an object (can be empty {}).
Error: Schema is not valid JSON Schema object
The schema object must have a 'type' property set to 'object'.
fix
Ensure the schema has { type: 'object' } at the root.
TypeError: Cannot use 'in' operator to search for '__id' in undefined
One of the $ref references is missing in the refs object.
fix
Include all referenced schemas in the refs object.
Upgrade
Version history
0.3.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
19 hits · last 30 days
node
16
OpenAI (training)
2
Resources
convert-json-schema-to-mongoose — npm install convert-json-schema-to-mongoose · libregistry