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

prudence

v0.10.0

Published

The Simple Object Validator.

Downloads

56

Readme

Prudence (Logo here, please)

The simple JS object validator.

Prudence is currently heavily in development, and should not be used in any environment.

The API may (will!) suddenly change underfoot. Do not depend on this library for anything at the moment.

Documentation

Read the documentation here.

Motivation

Validating non-primitive input is a pain, and I found myself rewriting input validation a lot of the time.

Existing solutions didn't really appeal to me, especially ones that defined their own complicated schema format to do things instead of using functions.

In constrast, Prudence uses the schema such that it's identical to the structure of an expected object. Prudence also only two options for validation, a typeof check or a provided function predicate.

Benefits

  • Damn simple. 0 dependencies and counting.

  • Primarily uses functions, which means you do not have to make pull requests for features, you can just write them yourself.

  • Schema is the exact same structure as the object, no wildcarding workarounds or weird exceptional use cases.

  • Automatic high quality error messages.

  • Wrote in TypeScript (tests are in JS).

Importing

npm i prudence
import Prudence from "prudence"; // ES6 (preferred)
const Prudence = require("prudence").default; // CJS

Returns

Prudence returns null on success, and an error object on failure.

let err = Prudence(/* */);
if (err) {}

Example use cases

Validating non-primitive user input is the most common use for Prudence.

Here's a common use case of wanting to validate JSON input.

import fs from "fs";

let userInput = JSON.parse(fs.readFileSync("config.json"));

// minimal implementation of the mocha config
let schema = {
    diff: "boolean",
    extension: ["string"],
    package: "string",
    slow: Prudence.isPositiveInteger,
    ui: Prudence.isIn("bdd", "tdd")
}

let err = Prudence(userInput, schema);

if (err) {
    console.error(err);
    process.exit(1);
}

Prudence also comes with a built in express middleware generator.

You can use this to create automatic input validation for your endpoints

import express from "express";
const router = express.Router();

let schema = {
    username: "string",
    password: (self) => typeof self === "string" && self.length > 8,
    confirmPassword: (self, parent) => self === parent.password,
    rememberMe: "boolean"
}

// send the user an error message (your 400 handler here, basically).
let errorHandler = (req, res, next, errObject) => res.status(400).send(errObject.message);

router.post("/register", Prudence.middleware(schema, errorHandler), (req, res) => {
    return res.status(200).send("registered account!");
});

Static Functions

Because Prudence only has two schema validation methods, string or function, it also comes with some static functions to help with validation.

let schema = {
    age: Prudence.isPositiveInteger,
    favouriteFruit: Prudence.isIn("apple", "banana", "orange"),
    testScore: Prudence.isBoundedInteger(0, 60)
    // etc.
}

There are a decent amount of these static functions, and you can see more of them in the documentation.