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-house-rules

v0.1.0

Published

Express middleware for house-rules. Validates request req.query, req.params, req.body

Downloads

8

Readme

express-house-rules

Build Status

Use https://github.com/bradwestfall/house-rules validation rules to validate incoming requests in Express.

Install

npm install --save express-house-rules

Example

Example house-rules-schema.js file:

import { Is, Schema } from 'house-rules'

// Base Rule for IDs
const id = Is.numeric().positive().integer().required()

// Base Rule for Names (Allow Spaces)
const strict = false
const name = Is.string().alpha(strict).required()

const schema = new Schema({
  userId: id.label('User ID'),
  firstName: name.label('First Name'),
  lastName: name.label('Last Name'),
  email: Is.string().email().required(),
  password: Is.string().minLength(8).maxLength(100).required(),
  birthDate: Is.date('MM-DD-YYYY').beforeToday().required()
})

Example usage of express-house-rules:

import express from 'express'
import { registerHouseRules, validQuery } from 'express-house-rules'
import houseRulesSchema from './house-rules-schema' // wherever your house rules are

const app = express()

// Register your house rules schema with express-house-rules
registerHouseRules(houseRulesSchema)

// A route which is not using input validation
app.get('/', (req, res) => {
  res.send('Hello World!')
})

// A route which validates that the query has a `firstName` value and that it is
// valid according to your "house rules"
app.get('/test', validQuery('firstName'), (req, res) => {
  res.send('Test worked')
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

Start by creating an Express application and register a house-rules schema to be used with express-house-rules. Then use validQuery, validParams, or validBody middleware on routes you wish to validate inbound values for. All three middlewares work by taking any quantity of string arguments that match the rule names of your house rules. For instance, given our house rules example we can validate a login form like this:

app.post('/login', validBody('email', 'password'), (req, res) => {
  // Check `req.body.email` and `req.body.password` against the database here
})

In the above example, the validBody middleware will ensure that email and password meet your validation requirements for what constitutes a valid email and password. If the values are not valid, the middleware will call next(error) with errors that house-rules found. Note that in express, any time a middleware calls next(errors) with an errors argument, your route will be bypassed and your express configuration will catch the error in a special error handling route. The main point being that if the values are valid, then your /login route will be called and you can rest assure that email and password meet your validation requirements. You would then do proceed to your business logic like checking the values against the database for authentication.

Note that validQuery should be used for Express requests which are expecting query arguments such as /example?foo=bar, validParams should be used for Express requests which use params such as /example/:foo, and validBody should be used for HTTP Post requests. Also note that in Express, you'll need to use other modules to get Express to handle Post requests correctly.

Required and Optional

Depending on how your strategy for setting up house rules, it can be common to have all rules start as either required or optional. The house rules in the example above all start as required. But what if we want to keep the house rules set that way for most cases, but we have that one route where lastName is optional? In these cases, you can make changes the rule's requiredness by adding a prefix:

app.post('/signup', validBody('optional:firstName', 'optional:lastName', 'email'), (req, res) => {
})

Note that you can use optional: or its aliases opt: and o: to identify that the rule needs to be adjusted to be optional. Likewise, if a house-rule starts off as optional and you want to make it required on a per-house basis, you can use required: or the aliases req: and r:

Highly Custom Rules

In order to use the middleware with string arguments, you need to call registerHouseRules() ahead of time. But using the middleware with string arguments an adjusting the rule's requiredness might not be custom enough for your needs. An alternative use of the middleware is to pass house-rules schemas directly into the express-house-rules middleware like this:

import { Is, Schema } from 'house-rules'

const loginSchema = new Schema({
  email: Is.string().email().required(),
  password: Is.string().minLength(8).maxLength(100).required()
})

app.post('/login', validBody(loginSchema), (req, res) => {
  // Check `req.body.email` and `req.body.password` against the database here
})

This gives you the full power of house-rules schemas and sub schemas. See house-rules documentation for more details.

Note that this use of the middleware only takes one argument (schema) and cannot be mixed with the other use of the middleware to take any number of string arguments.