mongoose-schematypes-extend

0.3.2 • Public • Published

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

Package Sidebar

Install

npm i mongoose-schematypes-extend

Weekly Downloads

47

Version

0.3.2

License

MIT

Last publish

Collaborators

  • shold