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

my-routes-validator

v1.0.6

Published

express-route-validator is a lightweight middleware for validating request data (body, params) in Express.js before reaching the controller, ensuring clean and secure route handling.

Downloads

404

Readme

Validator Middleware for Express

This is a simple validation middleware for Express.js that allows you to apply field validations on your route handlers. You can define validation rules in an easy-to-read object format, and the middleware will check the request's body (and req.params if needed) for errors before the route logic is executed.

Features

  • Field-level validation with various rules (required, minLength, maxLength, email, etc.).
  • Support for custom validation logic.
  • MongoDB ID validation for routes that require an ID parameter (req.params.id).
  • Error handling with clear messages for invalid fields.
  • Easy integration into Express.js route handlers.

Installation

Here are the available validation rules you can use:

  • required: Ensures the field is not empty.
  • minLength: Ensures the field has at least the specified number of characters.
  • maxLength: Ensures the field does not exceed the specified number of characters.
  • min: Ensures the field value is greater than or equal to the specified number.
  • max: Ensures the field value is less than or equal to the specified number.
  • email: Validates if the field is a valid email address.
  • pattern: Validates if the field matches a custom regular expression.
  • numeric: Ensures the field value is a valid numeric value.
  • boolean: Ensures the field value is a boolean.
  • date: Ensures the field value is a valid date.
  • decimal2: Ensures the field value has up to 2 decimal places.
  • decimal6: Ensures the field value has up to 6 decimal places.
  • custom: You can provide a custom validation function that returns true for valid - values or false for invalid values.

Example use

import express from "express";
import bodyParser from "body-parser";
import { validator } from "my-routes-validator";

const app = express();
app.use(bodyParser.json());

const options = {
  "/custom-validation": [
    {
      field: "username",
      message: "Username is required",
      required: true,
    },
    {
      field: "age",
      message: "Age must be a valid number and greater than 18",
      required: true,
      custom: (value) => !isNaN(value) && value > 18,
    },
    {
      field: "password",
      message: "Password must include at least one special character",
      required: true,
      custom: (value) => /[!@#$%^&*]/.test(value),
    },
    {
      field: "number",
      message:
        "Number must have up to two decimal places and up to six decimal places",
      required: true,
      decimal2: true,
      decimal6: true,
    },
  ],
};

app.post("/custom-validation", validator(options), (req, res) => {
  res.status(200).json({ message: "Validation passed!", data: req.body });
});

app.listen(3000, () => {
  console.log("Server is running on http://localhost:3000");
});

Installation

You can install this package via npm:

npm install my-routes-validator