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 🙏

© 2025 – Pkg Stats / Ryan Hefner

validation-kit

v0.0.9

Published

Simple validation toolkit with common validations and a single-line validation function.

Downloads

19

Readme

Validation Kit

Validation Kit is a set of common type checking functions, including single-line validation function to validate variables and parameters.

Features

  • Common type checking functions that are not common enough to be in Lodash.
  • Single-line runtime validation function to check variable types, throw errors or use defaults.
  • Single-line typescript validation function to reduce boilerplate code for runtime type checking.

Functions

check(value, validator, options)

Arguments:

  • value: Input value to test.
  • validator: (object) Specifies how to validate the value.
    • validate: (function) Takes the value as input and should provide a boolean output.
    • expected: (string) A description of the expected value.
    • options: (object, optional)
      • name: (string, optional) Name of the value to be used in error/warning messages. Defaults to 'value'.
      • default: Value to return if input value did not pass validation. Can be a function that takes input value as argument.
      • warn: (function) Function to determine whether a warning should be displayed. Default behaviour is that warnings are only displayed if a non-nil value was given.
  • options: (object/string, optional) Overrides the options of the validator. If a string is provided, it will be treated as the name option.

Returns: The value if it passed validation, the provided default value if it did not. Throws an error if value did not pass validation and no default was provided.

Usage:

Javascript:

// Simple example:
let fullName = check(123, { validate: _.isString, expected: "string"}); // will throw error

// Full example:
let fullName = check(123, { validate: _.isString, expected: 'string'}, {
	name: "fullName",
	default: "John Doe",
	warn(x) {
		return !_.isNil(x)
	}
});

// Using predefined validator object:
let fullName = check(123, IS_STRING, {default: "John Doe", warn: x => !_.isNil(x)})

Typescript:

let fullName = check<string>(123, {validate: _.isString, expected: "string"});

Adding the dynamic type will let Typescript know the result is checked to be the given type. Make sure the dynamic type matches the runtime type validator!

Using predefined validators, the return value of check will be typed accordingly:

let fullName = check(123, IS_STRING); // TypeScript will determine fullName to be string

Custom validators can be predefined as well:

const MY_VALIDATOR:Validator<MyType> = {
	validate: x => myValidation(x),
    expected: 'my validated value'
}
let value = check(123, MY_VALIDATOR);

Functions

instanceOf(Class)

Returns a validator function that checks if an argument is of the given class.

  • Class: (Class/function) Resulting validator will check if its argument is of this class.

Example Usage:

let peter = new Person();
check(peter, instanceOf(Person), 'Person'); // ok

parseNumberStrict(value)

Attempts to parse the given string to a number, but only if the string representation of the resulting number is the same as the input string.

  • value: (string) string to parse to number

Example Usage:

parseNumberStrict("1.2"); // 1.2
parseNumberStrict([]); // null
parseNumberStrict("01"); // null (whereas parseFloat would parse as 1)
parseNumberStrict("1.20"); // null (whereas parseFloat would parse as 1.2)

subClass(Parent, includeIdentity)

Returns a validator function that checks if an argument is a subclass of the given class.

  • Parent: (Class/function) Resulting validator will check if its argument is a subclass of this class.
  • includeIdentity: (boolean, optional) Determines whether the Parent class itself should return true in the validator. Defaults to true.

Example Usage:

class Animal {}
class Cow extends Animal {}
check(Cow, subClass(Animal), "subclass of Animal"); // ok
check(Animal, subClass(Animal), "subclass of Animal"); // ok
check(Animal, subClass(Animal, false), "subclass of Animal"); // error 

Classes

ValidationError extends Error

An error to be thrown when validation fails for some value.

constructor(value, expectedType, name)

  • value: Value that was validated
  • expectedType: (string) Description of the type
  • name: (string, optional) Name of the validated value

static getMessage(value, expectedType, name)

Generates a human-readable error message as to why validation failed for the given value.

  • value: Value that was validated
  • expectedType: (string) Description of the type
  • name: (string, optional) Name of the validated value
  • returns: (string) Error message

Validators

Below is an overview of included validators, and what a true return value of these validators means:

  • isAlphanumeric: argument is string or number
  • isClass: argument is a function (semantic alias of Lodash's isFunction)
  • isPrimitive: argument is string, number or boolean