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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@diegoti/simple-validation-for-express

v1.0.7

Published

Simple validation for express

Downloads

11

Readme

simple-validation-for-express

Getting Started

Install it using npm:

npm i @diegoti/simple-validation-for-express

Validations List

  • string
    • required
    • size
    • range
    • email
    • confirmPassword
    • inList
    • equal
    • notEqual
    • custom
  • number
    • required
    • range
    • inList
    • equal
    • notEqual
    • custom

Config object definition

{
  source: 'req property from express: body, query or params',
  type: 'string or number',
  field: 'property name that exists inside source',
  validations: {
    'validations_list_above': { 
      value: 'see examples below.',
      message: 'it overrides default message.',
    }
  }
},

Usage

Define a configuration object with the validations.

const uservalidator = [
  {
    source: 'body',
    type: 'string',
    field: 'name',
    validations: {
      required: { value: true, message: '[name.required] custom message.' },
    },
  },
  {
    source: 'body',
    type: 'string',
    field: 'email',
    validations: {
      required: { value: true, message: '[email.required] custom message.' },
      email: { value: true },
    },
  },
  {
    source: 'body',
    type: 'string',
    field: 'password',
    validations: {
      required: { value: true, message: '[password.required] custom message.' },
      range: { value: { min: 8, max: 16 } },
      confirmPassword: { value: "confirmPassword", message: '[password.confirmPassword] doesn\'t match new password.' },
    },
  },
  {
    source: '',
    type: 'string',
    field: 'confirmPassword',
    validations: {
      custom: {
        value: (body, query, params) => (body.confirmPassword === '123456'),
        message: 'Weak password, choose a strong password.',
      },
    },
  },
  {
    source: 'body',
    type: 'string',
    field: 'role',
    validations: {
      required: { value: false },
      size: { value: 3 },
      inList: { value: ['ADM', 'USE', 'MAN'] },
    },
  },
  {
    source: 'body',
    type: 'number',
    field: 'age',
    validations: {
      required: { value: true },
      range: { value: { min: 0, max: 100 }, message: null },
      inList: { value: [1, 55, 530] },
      equal: { value: 1111 },
    },
  },
];

module.exports = {
  uservalidator,
};

Import simple-validation-for-express and your config object and pass it to your route, if any validation is found, an error object is added to req object.

var express = require('express');
const simpleValidationForExpress = require("@diegoti/simple-validation-for-express");

const { uservalidator } = require('./user.validator');
var router = express.Router();

router.get('/', simpleValidationForExpress.validate(uservalidator), function (req, res, next) {
  res.status(req.errors ? 400 : 200).json({errors: req.errors})
});

module.exports = router;

Example of req.errors:

{
  "errors": [
    {
      "field": "name",
      "message": "[name.required] custom message."
    },
    {
      "field": "email",
      "message": "[email.required] custom message."
    },
    {
      "field": "email",
      "message": "email with value [@[email protected]] is not a valid email."
    },
    {
      "field": "password",
      "message": "[password.required] custom message."
    },
    {
      "field": "password",
      "message": "password must be between 8 and 16 characters."
    },
    {
      "field": "password",
      "message": "[password.confirmPassword] doesn't match new password."
    },
    {
      "field": "confirmPassword",
      "message": "Weak password, choose a strong password."
    },
    {
      "field": "role",
      "message": "role must contain one of these values [ADM,USE,MAN]."
    },
    {
      "field": "role",
      "message": "role must have 3 characters."
    },
    {
      "field": "age",
      "message": "age must be provided"
    },
    {
      "field": "age",
      "message": "age must be equal to [1111]"
    },
    {
      "field": "age",
      "message": "age must contain one of these values [1,55,530]."
    },
    {
      "field": "age",
      "message": "age must be a valid Number."
    },
    {
      "field": "age",
      "message": "age must be between 0 and 100."
    }
  ]
}