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

express-validator-builder

v0.0.2

Published

ctavan/express-validator validation schema builder for mongo

Downloads

4

Readme

express-validator-builder

ctavan/express-validator validation schema builder for mongo.

This is not a fully grown project, lots of things are needed to implement like support for:

  • custom validation
  • unique validation
  • build generic rule file to support other DBs
  • etc.

Installation

npm i -S express-validator-builder

Usage

let options = {
  uuid: true // attach 'isUUID' validation to all ref's & _id's (default: true)
}

let Model = mongoose.model('User')
let mongooseSchema = Model.schema.obj

let validationSchema = validationBuilder.build(mongooseSchema, options)
console.log(validationSchema) // see result below

Simple Rule Build

Mongoose Schema - Simple

let schema = new mongoose.Schema({
  _id: {
    type: String,
    required: true,
    trim: true
  },
  email: {
    type: String,
    maxlength: 50,
    required: true,
    unique: true,
    index: true,
    trim: true
  },
  enums: {
    type: String,
    enum: ['pending', 'scheduled', 'accomplished']
  }
})

Result - Simple

{
  "_id": {
    "notEmpty": {
      "errorMessage": "Kindly specify a valid _id"
    },
    "isUUID": {
      "options": [5],
      "errorMessage": "Expecting UUIDv5 for field '_id'"
    }
  },
  "email": {
    "notEmpty": {
      "errorMessage": "Kindly specify a valid email"
    },
    "isEmail": {
      "errorMessage": "Kindly specify a valid email address"
    },
    "isLength": {
      "options": [{"max": 50}],
      "errorMessage": "Field 'email' must not exceed more than 50 characters."
    }
  },
  "enums": {
    "optional": true,
    "matches": {
      "options": "^pending$|^scheduled$|^accomplished$",
      "errorMessage": "Can't find match value on 'enums' enum field. Expecting 'pending, scheduled, accomplished'"
    }
  }
}

Complex Rule Build

Mongoose Schema - Complex

let Schema = mongoose.Schema
let schema = new Schema({
  ref: {
    type: String,
    ref: `Demo1`,
    required: true,
    trim: true
  },
  arrRef: [{
    type: String,
    ref: `Demo2`,
    trim: true
  }],

  arrObj: [{
    destination: {
      type: String,
      ref: `Destination`,
      trim: true
    },
    status: {
      type: String,
      enum: ['pending', 'scheduled', 'accomplished'],
      default: 'pending'
    }
  }],
  
  schemaObj: new Schema({
    title: {
      type: String,
      required: true,
      trim: true
    },
    content: {
      type: String,
      required: true,
      maxlength: 10,
      trim: true
    }
  }, {_id: false}),

  noValidatoin1: {
    type: Boolean,
    default: false
  },
  noValidatoin2: {
    type: String,
    trim: true
  },
  noValidatoin3: {
    type: Number,
    default: 0
  },
  noValidatoin4: {
    type: Date
  },
})

Result - Complex

{
  "ref": {
    "notEmpty": {
      "errorMessage": "Kindly specify a valid ref"
    },
    "isUUID": {
      "options": [5],
      "errorMessage": "Expecting UUIDv5 for field 'ref'"
    }
  },
  "arrRef.*": {
    "optional": true,
    "isUUID": {
      "options": [5],
      "errorMessage": "Expecting UUIDv5 for field 'arrRef.*'"
    }
  },
  "arrObj.*.destination": {
    "optional": true,
    "isUUID": {
      "options": [5],
      "errorMessage": "Expecting UUIDv5 for field 'arrObj.*.destination'"
    }
  },
  "arrObj.*.status": {
    "optional": true,
    "matches": {
      "options": "^pending$|^scheduled$|^accomplished$",
      "errorMessage": "Can't find match value on 'arrObj.*.status' enum field. Expecting 'pending, scheduled, accomplished'"
    }
  },
  "schemaObj.title": {
    "notEmpty": {
      "errorMessage": "Kindly specify a valid schemaObj.title"
    }
  },
  "schemaObj.content": {
    "notEmpty": {
      "errorMessage": "Kindly specify a valid schemaObj.content"
    },
    "isLength": {
      "options": [{"max": 10}],
      "errorMessage": "Field 'schemaObj.content' must not exceed more than 50 characters."
    }
  }
}