User Rating 0.0
Total Usage 0 times
Mongoose Schema Definition
// Schema output will appear here
Is this tool helpful?

Your feedback helps us improve.

About

Mongoose schemas define document structure for MongoDB. Alpaca Forms renders JSON Schema as interactive HTML forms. Bridging these two requires mapping Mongoose types (String, Number, ObjectId, nested objects, arrays) to their JSON Schema equivalents (string, number, object, array) while preserving validators: required, min, max, enum, match (converted to pattern), and default values. Manual conversion is error-prone. A missing required flag or mismatched enum array means your form silently accepts invalid data that your database will reject at write time. This tool parses Mongoose-style schema definitions and outputs a complete Alpaca configuration object with schema, options, and data sections ready for Alpaca rendering.

Limitations: this tool approximates Mongoose schema syntax from plain object notation. It does not execute JavaScript, so dynamic defaults (functions), custom validators with callbacks, virtuals, middleware, and Schema.plugin() calls are not converted. Discriminators and refPath dynamic references produce a basic string type. The parser handles the structural subset that maps cleanly to JSON Schema.

mongoose alpaca schema converter json schema form generator mongodb node.js javascript

Formulas

The converter applies a deterministic mapping function f from each Mongoose field descriptor M to an Alpaca triple (S, O, D) representing schema, options, and data:

f(M) (S, O, D)

Where S = JSON Schema fragment with type, enum, minimum, maximum, minLength, maxLength, pattern, format, default. O = Alpaca options fragment with type (control type), label, helper, placeholder. D = default data value extracted from default property if it is a literal (non-function).

For nested objects, the function recurses:

f(Mnested) = { properties: { k : f(Mnested[k]) for each k keys(Mnested) } }

For arrays [T], the schema becomes type = "array" with items = f(T). The required array at each object level is populated by collecting all field keys where required = TRUE in the Mongoose descriptor.

Reference Data

Mongoose TypeJSON Schema TypeAlpaca FormatValidators MappedNotes
Stringstringtextminlength, maxlength, enum, matchmatchpattern
Numbernumbernumbermin, maxInteger detection not automatic
Booleanbooleancheckbox - Default mapped if present
Datestringdatemin, maxFormat set to date
ObjectIdstringtextrefRef stored in options label
Bufferstringfile - Binary data as file upload
Mixed / Schema.Types.Mixedanyany - No validation enforced
Mapobjectobject - additionalProperties from of
[String]array of stringarrayArray-level validatorsItems schema generated
[{ nested }]array of objectarrayRecursive parsingFull nested schema in items
Nested { field: { ... } }objectobjectRecursive parsingProperties generated recursively
Decimal128numbernumbermin, maxHigh-precision decimal
Int32integerintegermin, maxExplicit integer type

Frequently Asked Questions

An ObjectId field maps to JSON Schema type string. The ref value (e.g., "User") is placed in the Alpaca options as a helper annotation: "Reference: User". Alpaca does not natively resolve MongoDB references, so you would need a custom data source connector to populate a dropdown from the referenced collection.
A match validator containing a regular expression like /^[a-z]+$/i is converted to the JSON Schema pattern property as a string: "^[a-z]+$". Regex flags are noted in the options helper text but cannot be enforced by JSON Schema pattern, which is always case-sensitive per spec.
No. Discriminators add conditional schemas based on a discriminator key at runtime, which requires executing JavaScript logic. Plugins modify the schema programmatically. Both are outside the scope of static schema parsing. The tool handles the declarative subset: plain object field definitions with types, validators, and nested structures.
If your schema definition includes timestamps: true (detected as a top-level key), the converter adds two additional fields: createdAt and updatedAt, both with type string and format date-time. These are marked as read-only in Alpaca options.
Function defaults like default: Date.now or default: () => [] cannot be serialized to JSON. The converter detects function references and omits the default property from the schema output, adding a helper note: "Dynamic default (function) - not serializable".
The output targets JSON Schema Draft-04, which Alpaca Forms uses internally. This means required is an array at the object level (not per-field boolean), type is a string, and additionalProperties defaults to true. If you need Draft-07 features like if/then/else, manual post-processing is required.