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

issable

v0.1.2

Published

Swiss army knife for runtime payload sanitisation and type-checking.

Downloads

7

Readme

Issable -- Your Swiss Army Knife for runtime payload sanitisation and type-checking. Provides actionable error messges out-of-the-box.

There should be no learning curve for runtime payload sanitisation and type-checking. Issable is lightweight, readable, and highly extensible.

Installation

npm install --save issable

Usage

import { is } from 'issable'

is('foo').string() // returns true
is('foo').boolean() // Throws an error

Also supports CommonJS Require

const { is, not, define } = require('issable')

Intuitive style (ExpressJS and Fastify compatible)

Do away with vague, or troublesome error messaging/handling. One-size-fit-all example in using Issable with ExpressJS/Fastify:


app.get('/', (req, res, next) => {
    const payload = req.body
    const schema = {
        name: 'string',
        age: 'number',
        mobile: ['string', 'number']
    }

    const sanitised = is(payload)
        .exact()
        .statusCode(400) // Error instance will contain property `statusCode`. See #errorHandler, `error.statusCode` below.
        .object(schema) // throws error if not matched, with details on mismatch with schema.

    // if check is passed, `sanitised` contains the sanitised object for use
    doSomethingWithPayload(sanitised)
})

// the above error will throw into Express/Fastify's error handler:
app.use(errorHandler)

function errorHandler(err, req, res) {
    res.statusCode = (error.statusCode) ? error.statusCode : 500
    
    res.locals.message = err.message
    res.locals.error = err
    
    res.send({
        success: false,
        ...res.locals
    })
}

Simple checks, and you can define our own error message:

function simpleAPIChecks(stringPayload, arrayPayload) {

    is(stringPayload, 'nameOfString')
        .msg('Invalid `string`.') // custom error message.
        .string()

    is(stringPayload, 'nameOfArray')
        .msg('Invalid `array`.')
        .string()

})

Define your custom type

import { define } from 'issable'

    define({
        nameOfTyping: 'currency',
        primitives: ['string', 'number'], // could be string like '$10.00'

        // a function to return true or false
        toPass: (candidate: any) => {

            // $10.00, remove '$', check if can be parsed to a float.
            if (typeof candidate === 'string') {
                candidate = candidate.replace('$', '')
                return is(parseFloat(candidate)).safe().number()
            }

            // will be a number here, therefore will pass.
            return true
        },
    })
})

#not

is(stringVar).not().boolean() // true

// is equivalent to

import { not } from 'issable'

not(stringVar).boolean() // true
not(stringVar).string() // throws error