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

tiny-checker

v1.0.0

Published

A tiny, composable and battle-tested utility library to validate string inputs, typically used in forms.

Downloads

7

Readme

tiny-checker

A tiny (~1KB), composable and battle-tested utility library for string validation, built in TypeScript. Typically used to check the user input in applications.

npm version npm downloads gzip size modules MIT License PRs Welcome

Quick start

import { checkIf } from "tiny-checker";

const input = "Some input";

console.log(checkIf(input).hasMinLength(3).not().hasDigit().isValid()); // true

API

This library has several built-in validators that check for specific attributes in the string input:

  • hasDigit(atLeast: number, errorMessage: string): Checks if input contains atLeast number of digits. Defaults to 1.

  • hasLength(length: number, errorMessage: string): Checks if input has exact length of characters. Defaults to 0.

  • hasMinLength(length: number, errorMessage: string) :Checks if input has minimum length characters. Defaults to 1.

  • hasMaxLength(length: number, errorMessage: string): Checks if input has maximum length characters. Defaults to 1.

  • hasLowerCase(atLeast: number, errorMessage: string): Checks if input contains atLeast number of lower-case characters. Defaults to 1.

  • hasUpperCase(atLeast: number, errorMessage: string): Checks if input contains atLeast number of upper-case characters. Defaults to 1.

  • isEmail(errorMessage: string): Checks if input is in email format. Uses https://emailregex.com/ for validation.

  • matches(regex: string | RegExp, errorMessage: string): Checks if input matches the regex pattern

There's also a special function called not() that when combined with one of the validators, it negates whatever check is going to be done by the following validator.

// Checks if input doesn't contain digits
checkIf("something").not().hasDigit().isValid(); // true

To get the result of the validation you can use two different functions:

  • isValid(): Returns a boolean with the result of the validation
  • hasErrors(): Returns an array of strings with the error messages if they exist or an empty array otherwise.

Combining multiple validators

One of the main objectives of tiny-checker is to provide a composable API that allows combining multiple validators in an intuitive way. If you're familiar with jest and other testing frameworks, you'll find this very similar:

// Check if input is a strong password
checkIf("StrongPassword123")
    .hasMinLength(9)
    .hasMaxLength(32)
    .hasUpperCase()
    .hasLowerCase()
    .hasDigit()
    .isValid();

Custom error messages

For each validation function it's possible to define custom error messages. This is useful to provide specific error messages for special situations or for using with different languages.

checkIf("something").hasDigit(1, "needs to be alphanumeric").hasErrors();
// ["needs to be alphanumeric"]

checkIf("something").hasDigit(1, "não contém números").hasErrors();
// ["não contém números"]

By default the error messages use the function parameters where applicable. This is done by replacing the $value placeholder in the error message.

checkIf("something").hasDigit(1).hasErrors();
// ["should have at least 1 digit(s)"]

checkIf("something")
    .hasDigit(2, "please add at least $value digit(s)")
    .hasErrors();
// ["please add at least 2 digit(s)"]

Extending default behaviour

It's possible to extend the behaviour of tiny-checker by adding new validators, either with a custom implementation or installed from an external plugin.

To build a new validator you have access to the TinyCheckerBase class that provides all necessary logic to build the custom validator.

import { TinyCheckerBase, defaultValidators } from "tiny-checker";

function isUpperCase<T extends TinyCheckerBase>(
    this: T,
    errorMessage: string = "should be all uppercase"
): T {
    return this._processValue(
        (this._str.match(new RegExp(/\p{Lu}/, "gu")) || []).length >=
            this._str.length,
        errorMessage
    );
}

export const { checkIf } = TinyCheckerBase.load({
    ...defaultValidators,
    isUpperCase,
});

This will make a new checkIf function available with the additional validator, fully typed and ready to be used in conjuction with the existing validators.

The reason why we also make defaultValidators available is because in some scenarios you might want to just use a few of the default validators so that can be done by just loading the necessary ones:

import { TinyCheckerBase, defaultValidators } from "tiny-checker";

const { hasDigit, hasLength } = defaultValidators;

export const { checkIf } = TinyCheckerBase.load({
    hasDigit,
    hasLength,
});

Finally you can also load 3rd party validators that are available as npm packages:

import { TinyCheckerBase, defaultValidators } from "tiny-checker";
import isCreditCard from "tiny-checker-cc";
import { isZipCode } from "tiny-checker-address";

export const { checkIf } = TinyCheckerBase.load({
    ...defaultValidators,
    isCreditCard,
    isZipCode,
});

Local Development

Below is a list of commands you will probably find useful.

npm start or yarn start

Runs the project in development/watch mode. Your project will be rebuilt upon changes. TSDX has a special logger for you convenience. Error messages are pretty printed and formatted for compatibility VS Code's Problems tab.

Your library will be rebuilt if you make edits.

npm run build or yarn build

Bundles the package to the dist folder. The package is optimized and bundled with Rollup into multiple formats (CommonJS, UMD, and ES Module).

npm test or yarn test

Runs the test watcher (Jest) in an interactive mode. By default, runs tests related to files changed since the last commit.

This project was bootstrapped with TSDX.