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

not-valid

v2.0.0

Published

Composable message-based validation

Downloads

2,199

Readme

not-valid

Composable message-based validation

Installation

$ npm install not-valid --save

Usage

Overview

import { validate } from "not-valid";

// Use createValidator to specify a rule and the message given for breaking that rule
const mustContainA = createValidator<string>(v => v.indexOf("A") !== -1, "Value must contain the letter 'a'");

// pass in array of validation functions and a value to validate
validate([ mustContainA ], "cheese"); // [ "Value must contain the letter 'a'" ] - returns error messages

// can use a factory pattern for your validation methods to make things nice
const mustContain = (requirement: any) => {
    return createValidator<string | Array<any>>(v => v.indexOf(requirement) !== -1, `Value must contain '${requirement}'`);
};

const lengthWithinBounds = (min: number, max: number) => {
    return createValidator<string>(v => v.length < min || v.length > max, `Value must have length between ${min} and ${max}`);
};

// you can pass in multiple validators
validate([ 
    mustContain("Z"), 
    lengthWithinBounds(2, 6)
], "Too long a string, they say!"); // [ "Value must contain 'Z'", "Value must have length between 2 and 6" ]

Validators

A number of validation functions come bundled with this package. You can use them like so:

import { validators } from "not-valid";

validate([
    validators.validLength({ min: 6, max: 12 })
], "Good value");

The validators included are as follows:

  • requiredString
  • requiredNumber
  • validLength
  • validEmail
  • validAlphaNumeric
  • validOption
  • validPhoneNumber
  • validNINumber
  • validUKDrivingLicence
  • validSortCode
  • validBankAccountNumber
  • validVATNumber

Creating validation functions

A validation function must take in a value value, and return Result.Pass if value is valid, or Result.Fail(message) is value is invalid. They can also return Result.Stop, which will silently stop the validation cycle (no more errors).

This can be done with the helper method createValidator in not-valid:

import { createValidator } from "not-valid";

const mustContainA = createValidator<string>(v => v.indexOf("A") !== -1, "Value must contain the letter 'a'");

You can use factory patterns around this to make it nicer:

const mustContain = (requirement: any) => {
    return createValidator<string | Array<any>>(v => v.indexOf(requirement) !== -1, `Value must contain '${requirement}'`);
};

All validators (except for validators that explicitly check for "required") should treat empty string, null and undefined as valid. This is because we can combine validators with "required" validators in order to enforce something being valid and not empty, but also allows us to accept nothing being entered if desired.

Options

The third parameter of validate is an object containing options.

interface ValidationOptions {
    sequential?: boolean;
}

sequential

Default: true

The validation will break on the first error, therefore only returning a single validation error.

validate([ something, another ], 5, { sequential: false });

If something fails validation, another will not be called.

License

Made with :sparkling_heart: by NewOrbit in Oxfordshire, and licensed under the MIT Licence