prudence
TypeScript icon, indicating that this package has built-in type declarations

0.10.0 • Public • Published

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.

Package Sidebar

Install

npm i prudence

Weekly Downloads

67

Version

0.10.0

License

MIT

Unpacked Size

45.3 kB

Total Files

17

Last publish

Collaborators

  • zkldi