npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

als-mongoose-form

v1.0.0

Published

A lightweight utility to convert Mongoose schemas into JSON form descriptors, supporting nested elements, arrays, enums, and validation rules.

Readme

als-mongoose-form

What is als-mongoose-form?

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.

Key Features

  • 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.

Installation

Install the library via npm:

npm install als-mongoose-form

Basic Usage

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);

Output Example

{
   "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"
               }
            ]
         }
      }
   ]
}