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

paperwork

v2.1.1

Published

Lightweight JSON validation for REST APIs.

Downloads

100

Readme

Paperwork

Lightweight JSON validation for node.js REST APIs.

Why ?

If you build a REST API with node.js, you'll end up validating JSON sooner or later. It's not exactly difficult, but life would be easier if:

  • You knew precisely what the structure of data your API accepts.
  • It was easy for your fellow developers to document the requirements of the API.
  • It was easy for people consuming the API to understand why their request was rejected.
  • You didn't have to write boilerplate code.

Install

npm install paperwork

How it works

Simple ! Just write a JSON template:

var blogPostTemplate = {
  article_id: Number,
  title: String,
  body: String,
  publish_immediately: Boolean,
  tags: [String]
};

You can then validate JSON like that. It will return an array of missing or incorrect fields. The array is of course empty is everything is OK:

paperwork(blogPostTemplate, incomingPost, function (err, validated) {
  if (err) {
    // err is the list of incorrect fields
    console.error(err);
  } else {
    // JSON was validated, extra fields were removed.
  }
});

Express integration

If you're using Express, things are even simpler:

app.post('/post', paperwork.accept(blogPostTemplate), function (req, res) {
  // req.body is now validated: you can use it without checking anything
});

Invalid requests will receive an HTTP 400 response and will be silently rejected. The response will contain a helpful message indicating what was wrong:

{
  "status": "bad_request",
  "reason": "Body did not satisfy requirements",
  "errors": [
    "body.alias: should match /^[a-z0-9]+$/",
    "body.name: missing",
    "body.admin: should be a boolean",
    "body.age: should be a number"
  ]
}

Changes from 1.x

  • Paperwork now silently removes unknown fields from the validated blob. This is done so you never pass unvalidated data to your code. For instance, if an attacker was to pass an extra id, you might end up using it to update the wrong object in your database.

  • Paperwork now accepts an (error, validated) callback. It will pass either an error (the list of fields that did not match your requirements) or a validated JSON.

  • You must now pass paperwork.accept to Express.

Advanced Usage

User profile with minimal email validation and optional fields

var userProfileTemplate = {
  email: /[^@]+@[^@]+/,                    // validates only strings matching this regex
  name: String,
  age: Number,
  admin: Boolean,
  phone: paperwork.optional(String),       // makes the field optional
  country: paperwork.optional(/[a-z]{2}/)
};

Validating the content of an array

var betterBlogPostTemplate = {
  title: String,
  body: String,
  attachments: paperwork.optional([{      // validates an array of attachments
    content_type: String,
    data: String,
    size: Number
  }])
};

Custom validation, multiple conditions

var betterUserProfile = {
  email: /[^@]+@[^@]+/,
  name: String,
  age: function (age) {
    return age > 0;
  }
};

var evenBetterUserProfile = {
  email: /[^@]+@[^@]+/,
  name: String,
  age: paperwork.all(Number, function (age) {
    return age > 0;
  })
};