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 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-schematypes-extend

v0.3.2

Published

Extending Mongoose schema types

Downloads

194

Readme

mongoose-schematypes-extend

Provides more schema defined validators and modificators for Mongoose data types.

Attention

I've started this project recently - so I may make breaking changes between releases, please check the README for each release for the latest documentation.

Install

npm install mongoose-schematypes-extend

Usage

Setting up
var mongoose = require('mongoose');
require('mongoose-schematypes-extend')(mongoose);

NOTE: Can be called at once before including models (i.e. with mongoose.connect).

Using String capitalize modificator
var testSchema = mongoose.Schema({
    firstName: {
        type: "String",
        capitalize: true    // <- Will transform "aLeXaNdEr" to "Alexander"
    }
});
Using String capitalizeAll modificator

Capitalize all space-separated words.

var testSchema = mongoose.Schema({
    fullName: {
        type: "String",
        capitalizeAll: true    // <- Will transform "aLeXaNdEr aLex" to "Alexander Alex"
    }
});
Using String nomultispaces modificator
var testSchema = mongoose.Schema({
    comment: {
        type: "String",
        nomultispaces: true // <- Will transform "Hello   world" to "Hello world"
    }
});
Using String minwordcount and maxwordcount validators

Number of words calculated by splitting string by space.

var testSchema = mongoose.Schema({
    name: {
        type: "String",
        minwordcount: 2,    // <- "John" will generate a error but "John Smith" will pass this test
        maxwordcount: 3     // <- "John Antony Smith jr" will generate a error but "John Antony Smith" will pass this test
    }
});
Using String validphone validator

Phone validation using Google's libphonenumber (Thanks to Matt Bornski for this). Value for validphone can be true or ISO 3166-1 alpha-2 country code (two upper case letters). If country code present then phone number can be in local format else only international format (starting with +) can be passed.

var testSchema = mongoose.Schema({
    officePhone: {
        type: "String",
        validphone: "RU" // <- "8(495)123-45-67" will pass this test
                         //because in Russia "8(495)" is Moscow code
                         //however phone code for Russia is a "+7" instead of "8"
    },
    mobilePhone: {
        type: "String",
        validphone: true // <- Only full international phone numbers will pass this test
                         // "8(495)123-45-67" will fail this test!!! "+7(495)123-45-67" will pass it.
    }
});

NOTE: If you need to combine my string modificators with internal trim modificator then trim must be after all my modificators.

Using Number roundto modificator
var testSchema = mongoose.Schema({
    price: {
        type: 'Number',
        roundto: 2  // <- Will round 1234.5678 to 1234.57
    },
    amount: {
        type: 'Number',
        roundto: 0  // <- Will round 1234.5678 to 1235
    },
    pack: {
        type: 'Number',
        roundto: -2  // <- Will round 1234.5678 to 1200
    }
});

Feedback

Please issue me if you need some other modificator or schema defined validator for mongoose data types.

Changelog

0.3.2
  • Added "capitalizeAll" modificator for String data type
0.3.1
  • Fix README
0.3.0
  • Added "validphone" validator for String data type
0.2.1
  • Fixed readme
0.2.0
  • Added "nomultispaces" modificator for String data type
  • Added "minwordcount" and "maxwordcount" validators for String data type
0.1.0
  • Added "capitalize" modificator for String data type
  • Added "roundto" modificator for Number data type

License

MIT