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-contract

v2.0.0

Published

A middleware for express that to validate inputs of your REST API with Joi, or any validators.

Downloads

10

Readme

express-contract Build Status

express-contract is a small project that add contract validation to your API.

Installation

yarn add express-contract
npm install --save express-contract

Usage

const Joi = require("joi"); // Or any schema validator (must have a .validate() method)
const contract = require("express-contract").contract;

const schema = Joi.object().keys({
  username: Joi.string().alphanum().min(3).max(30).required(),
});

app.post("/api/user", contract(schema), function (req, res) {
  if (!req.compliance) {
    // Look at req.violation for validation errors

    res.status(400).json({
      error: "Bad request",
    });

    return;
  }

  res.status(200).json(req.body);
});

Variables:

  • req.compliance to know if the contract is respected
  • req.body to access the object return by validator
  • req.originalBody to access original object return by validator
  • req.query to access the object return by validator (GET only)
  • req.originalQuery to access original object return by validator (GET only)
  • req.violation for validation error

You can also precise the property to validate (usually body or query, but can be whatever you want), by default it's body, expect for GET requests.

// like /api/user?username=tot even if it's a POST method
app.post("/api/user", contract(schema, "query"), function (req, res) {
  if (!req.compliance) {
    // Look at req.violation for validation errors

    res.status(400).json({
      error: "Bad request",
    });

    return;
  }

  res.status(200).json(req.query);
});

Or using multiple contract, but while not validate others contracts the previous failed.

app.post(
  "/api/user",
  contract(schema_query, "query"),
  contract(schema_body, "body"),
  function (req, res) {
    if (!req.compliance) {
      // Look at req.violation for validation errors

      res.status(400).json({
        error: "Bad request",
      });

      return;
    }

    res.status(200).json({
      query: req.query,
      body: req.body,
    });
  }
);

Actually, it was test with Joi, and need body-parser.

But should work with other validators if the .validate() method has the following signature:

Schema.validate(schema, callback(err, value));

value returned by the validator's callback is used to get defaults values that are set in the schema.

Keep in touch!