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

validation-chainer

v1.1.3

Published

A tool to validate your inputs in a visualy pleasing and flexible way.

Downloads

12

Readme

Validation Chainer

A tool to validate your inputs in a visualy pleasing and flexible way.

npm

Install

With npm:

npm install validation-chainer

With yarn:

yarn add validation-chainer

Usage

Basic Usage:

import { startChain } from "validation-chainer";

const data = {
    foo: "yes",
    bar: "PLEASE",
};

// starts the chain with the data
// await is used here because pack returns a promise
const errors = await startChain(data)
    // starts check on foo
    .check("foo")
    // does some validation with the property foo (foo's value is used as an argument)
    .validate((foo) => foo typeof "string", "Foo is not a string")
    .validate((foo) => foo.length >= 8, "Foo must be at least 8 characters")

    .check("bar")
    // makes bar lower case (modifies the object getting passed in) so bar is now please
    .sanitize((bar) => bar.toLowerCase())
    .validate((bar) => bar == "please")

    // make sure to call this!
    .pack();

// check to see if there are any errors
if (errors.length > 0)
    // there should only be one error
    console.error(errors);

Logs in console:

[
  {
    property: "foo",
    message: "Foo must be at least 8 characters",
  },
];

Login verify example:

import { startChain } from "validation-chainer";
import argon2 from "argon2";

const data = {
  username: "bob",
  password: "very strong password",
};

const user = await database.findOne({ username: data.username });

const errors = await startChain(data)
  .check("username")
  .validate(() => user != null, "Username doesn't exist")

  .check("password")
  // makes sure username is valid first
  .ensure("username", "Username is invalid")
  // checks password using asynchronous hashing algorithm
  .validate(
    async (storedPass) => await argon2.verify(storedPass, user.password),
    "Password is incorrect"
  )

  .pack();

Using the validator library:

import { startChain } from "validation-chainer";
import validator from "validator";

const data = {
  name: "                 💩      ",
};

const errors = await startChain(data)
  .check("name")
  // removes whitespace at the beggining and end of name so it's now just "💩"
  .sanitize(validator.stripLow)
  .sanitize(validator.trim)
  // property fails here because of the 💩
  .validate(
    validator.isAlphaNumeric,
    "Name must contain valid alpha-numeric characters"
  )

  .pack();

Using TypeScript:

import { startChain } from "validation-chainer";

const data = {
  status: "sad",
};

// starts chain that's specialized with data
const errors = await startChain(data)
  // intilisense info on object
  .check("status")
  // optional type generics on function (default is any)
  .validate<string>((status) => status === "happy", "Why are you not happy")
  .sanitize<string>((status) => status.toUpperCase())

  .pack();

Documentation

The documentation is in the source code written in jsdoc (with typescript). IDE's like vscode will be able to show it while developing. There might be plans to convert it into markdown or html in the future.

Development

Install packages first: (using yarn)

yarn

Change Code.

Do tests:

yarn test

Do linting:

yarn lint

Build:

yarn build

Profit! All of this will also be automatically ran with github actions.