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 🙏

© 2025 – Pkg Stats / Ryan Hefner

expressjs-check

v1.1.7

Published

A simple data validation library.

Readme

expressjs-check

A simple library for data validation.

npm Snyk Vulnerabilities for npm package

expressjs-check allows pattern-based data validation. This build is specifically implemented to support expressJS functionalities, such as auto-respond with error messages.

Installation and Setup

You can install expressjs-check by simply entering the command npm i expressjs-check. Once it has installed you can use it in your Express app however you'd like. Below is a little example:

const express = require('express');
const app = express();
const port = 3000;

const checker = require('expressjs-check');

app.get('/', (req, res) => {

    // Check GET Parameters
    if(checker.check(req.query, res, {
        someString: {type:"string", required:true}
    })) return;

    // If parameter "someString" is passed
    res.status(200);
    res.send({ok:true});
});

app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));

Pattern Documentation:

Patterns are JSON Objects defining a certain format of values that should be checked. Every key in the pattern object represents a key of the same name in the check-object. The check() function returns false if all given parameters are valid, true otherwise.

Required Parameters

Values can be set as required using required:true. If null should be allowed, allowNull:true must be set.

Values can be set as required conditionally by using the requiredIf attribute. This allows for a JS-string to be passed that evaluates to either true or false, deciding whether or not the value is required. Other values in the object can be accessed using #keyname, for example:

'#somenumber === 1' will make the value required if the value somenumber is equal to 1.

Default Values

Not-required values can be set to a specified default value by using the default attribute:

{ a: { type:"number", default: 5 } }

The example above would insert the value 5 in the a key of the input object if it was initially undefined. If a value is provided in the input all checks are still run as normal and the original value is not overwritten.

null is, by default, not treated as an undefined value. In order to replace null values with the default values as well, the replaceNull attribute must be set to true.

List of Valid Parameters

A pattern can contain a list of valid parameters in form of an array. The following is a pattern which would allow the property a of the input object to be one of the numbers 1, 1.5 or 2:

{ a: { type:"number", possibleValues:[1, 1.5, 2] } }

All data types except for Objects are compared using the === operand. If the value is within the range of possible values, all other pattern attributes (such as for example min or max) still apply. Objects are recursively compared, as the === operator does not guarantee equality if both values are objects.

Data Types

Data Types can be checked using the type key in the pattern. Supported Types are as follows:

  • object
    • pattern : Allows for a recursive object check using a seperate pattern. requiredIf variables will still only reference the global object, however
  • array
    • pattern : Pattern that must apply to every item within the array
    • minLength : Minimum amount of items in the array
    • maxLength : Maximum amount of items in the array
    • fixedLength : Fixed amount of items in the array (overwrites minLength and maxLength)
    • noDuplicates : If set to true, only arrays containing no duplicate values are allowed.
    • removeDuplicates : If set to true, the check acts similarly to noDuplicates explained above. However, instead of returning an error, the checker removes all duplicate values from the passed object.
  • string
    • minLength : Minimum length of the string
    • maxLength : Maximum length of the string
    • fixedLength : Fixed length of the string (overwrites minLength and maxLength)
    • regex : Regular Expression the string needs to match (passed as a string)
    • regexFlags : Flags to be used with the given regex, as single characters (Example: "gm" for global + multiline)
  • boolean
  • date
    • min : Earliest possible date
    • max : Latest possible date
    • allowTimestamp : Allows Numbers to be passed as timestamps
  • number
    • min : Minimum value
    • max : Maximum value
    • precision : Maximum amount of allowed decimal places
  • integer: Same attributes as number, but does not allow decimal places.

Example Patterns:

// Check for a valid number larger than or equal to 10:
{
	testNumber: {
		required: true,
		type: 'number',
		min: 10
	}
}

// Check for a simple Email address string:
{
	email: {
		required: true,
		type: 'string',
		regex: '.+@.+\..+'
	}
}

// Use of multiple features of expressjs-check
{
    mail: {
      type: "object",
      required: true,
      pattern: {
        enabled: { type: "boolean", required: true },
        delay: {
          type: "object",
          requiredIf: "#mail.enabled",
          pattern: {
            days: { type: "number", required: true, min: 0 },
            months: { type: "number", required: true, min: 0 }
          },
        },
        time: { type: "string", regex: "([0-1][0-9]|2[0-3]):[0-5][0-9]" },
        weekday: { type: "integer", min: 0, max: 7 }
      }
    }
}