als-mongoose-form
is a lightweight utility designed to transform Mongoose schemas into JSON data structures suitable for generating forms. It automates the process of creating form elements by analyzing the schema definition, including properties like types, defaults, and constraints.
- Converts Mongoose schema definitions to JSON form descriptors.
- Supports field types (
String
,Number
,Boolean
,Date
, etc.) and additional Mongoose options (e.g.,enum
,default
,min
,max
). - Handles array fields, subdocuments, and references (
ref
). - Automatically includes
timestamps
and_id
fields based on schema options.
Install the library via npm:
npm install als-mongoose-form
Here’s how to use the library to generate form data from a Mongoose schema:
const mongoose = require('mongoose');
const SchemaToForm = require('als-mongoose-form');
// Or
const {schemaToForm} = require('als-mongoose-form');
// Define a Mongoose schema
const userSchema = new mongoose.Schema({
name: { type: String, required: true, label: 'Full Name' },
age: { type: Number, min: 0, max: 120, label: 'Age' },
gender: { type: String, enum: ['Male', 'Female', 'Other'], label: 'Gender' },
email: { type: String, required: true, label: 'Email', match: /^[^@\s]+@[^@\s]+\.[^@\s]+$/ },
isAdmin: { type: Boolean, default: false, label: 'Administrator' },
address: new mongoose.Schema({
street: { type: String, label: 'Street' },
city: { type: String, label: 'City' },
zipCode: { type: String, label: 'Zip Code', match: /^\d{5}$/ },
},{_id:false,title:'Address'}),
hobbies: [{ type: String, label: 'Hobbies' }], // Array of strings
tasks: [ // Array of objects
new mongoose.Schema({
title: { type: String, required: true, label: 'Task Title' },
completed: { type: Boolean, default: false, label: 'Completed' },
})
],
}, {
timestamps: true,
title: 'User Form',
description: 'Form to manage user details',
});
// Convert schema to form descriptor
const form = new SchemaToForm(userSchema).result;
// Or
const form = schemaToForm(userSchema);
{
"title": "User Form",
"description": "Form to manage user details",
"items": [
{
"tag": "input",
"type": "hidden",
"key": "id"
},
{
"tag": "text",
"key": "createdAt"
},
{
"tag": "text",
"key": "updatedAt"
},
{
"key": "name",
"tag": "input",
"type": "text",
"required": true,
"label": "Full Name"
},
{
"key": "age",
"tag": "input",
"type": "number",
"min": 0,
"max": 120,
"label": "Age"
},
{
"key": "gender",
"tag": "select",
"type": "text",
"options": [
{
"value": "Male",
"text": "Male"
},
{
"value": "Female",
"text": "Female"
},
{
"value": "Other",
"text": "Other"
}
],
"label": "Gender"
},
{
"key": "email",
"tag": "input",
"type": "text",
"required": true,
"label": "Email",
"pattern": "^[^@[ \\t\\r\\n\\f]]+@[^@[ \\t\\r\\n\\f]]+\\.[^@[ \\t\\r\\n\\f]]+$"
},
{
"key": "isAdmin",
"tag": "input",
"type": "checkbox",
"checked": false,
"label": "Administrator"
},
{
"key": "address",
"tag": "input",
"form": {
"title": "Address",
"items": [
{
"key": "street",
"tag": "input",
"type": "text",
"label": "Street"
},
{
"key": "city",
"tag": "input",
"type": "text",
"label": "City"
},
{
"key": "zipCode",
"tag": "input",
"type": "text",
"label": "Zip Code",
"pattern": "^[0-9]{5}$"
}
]
}
},
{
"key": "hobbies",
"tag": "input",
"isArray": true,
"type": "text",
"label": "Hobbies"
},
{
"key": "tasks",
"tag": "input",
"isArray": true,
"form": {
"items": [
{
"tag": "input",
"type": "hidden",
"key": "id"
},
{
"key": "title",
"tag": "input",
"type": "text",
"required": true,
"label": "Task Title"
},
{
"key": "completed",
"tag": "input",
"type": "checkbox",
"checked": false,
"label": "Completed"
}
]
}
}
]
}