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

req-valida

v1.0.2

Published

A strict request validate middleware intended for ExpressJS.

Downloads

51

Readme

req-valida

A strict express request validator middleware, used for ensuring the payload meets the requirement of an endpoint.

Whether amount is required in request.body or customerID in request.params, req-valida can handle everything. As a strict validator, req-valida throws error for any field present in request that is not expected for an endpoint.

valida is a Romanian verb, which means validate in English language.

req-valida enables the strict type-checking for request payloads even in Javascript projects. With the extra validation available via the rules (see bellow in data section), req-valida can be a single tool for all sorts of payload validation.

Installation

npm install req-valida

or

yarn add req-valida

Usage

For cjs,

const { validate } = require("req-valida");

For esm,

import { validate } from "req-valida";

validate method takes an object of which looks like,

// regex is required here to be used in rules

{
  location: "body",
  data: {
    amount: {
        rules: ["number", regex.number],
    },
    customerID: {
        rules: ["string"],
        isOptional: true,
    },
  },
}

location

location can be body | query | params. The validate method will use location to look for required data on each invocation.

data

data object should be defined as how the payload is expected for that endpoint. However, each key in data object must have rules array. First element on rules array is the expected type of the key (in string format). Such as, in above example, amount is required in the body of some endpoint, and amount must be a number.

Second element on the rules array is optional RegEx validation. If provided, validate will test the value of the amount against the RegEx string.

isOptional

After that, there's another optional property isOptional which can be sent with the each fields. Such as, in above example, customerID is optional in that request, so isOptional is sent true.

There can be case where every field in request is optional, in that case, instead of sending isOptional with the fields, validate allows another property outside of data called isOptional (similar to the fields) for these situation. It will tell validate to consider the whole data object to be optional.

Examples

An example router with validate from req-valida,

// necessary and required lines here

paymentRouter.post(
    "/intent",

    validate({
        location: "body",
        data: {
            amount: {
                rules: ["number", regex.number],
            },
            customerID: {
                rules: ["string"],
                isOptional: true,
            },
        },
    }),

    (request, response, next) => Payment.Intent.create(request, response, next)
);

// necessary and required lines here

validate should be used used on the same endpoint for separate concerns. For example, an endpoint might have something in the request.params and something in request.body, then it will look like,

// necessary and required lines here

customerRouter.put(
    "/:customerID",

    validate({
        location: "params",
        data: {
            customerID: {
                rules: ["string"],
            },
        },
    }),

    validate({
        location: "body",
        data: {
            name: {
                rules: ["string"],
            },
            email: {
                rules: ["string", regex.email],
            },
            phone: {
                rules: ["string", regex.phone],
            },
        },
        isOptional: true,
    }),

    (request, response, next) => Customer.update(request, response, next)
);

// necessary and required lines here

Contributing

While this is being used in some projects, req-valida has to improve a lot. A few things like unit-test, changelog, contributing guide, and a better README etc. needs to be added.

Even discussing about the req-valida in the GitHub repository's discussion is a form of contribution.

All contributions are welcomed. Interested parties are requested to follow the general procedure until the steps are explicitly mentioned here.