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

@foobarhq/validators

v2.0.0

Published

FP composable string parsers and validators.

Downloads

26

Readme

@foobarhq/validators

npm version

The main purpose of this library is (but not limited) to parse and validate data received from HTTP requests.

It achieves this goal by providing a set of composable functions designed to easily build complex input validators.

Usage

Install from NPM: npm install @foobarhq/validators

import { numberValidator } from '@foobarhq/validators';

// create a new validator
const validate = numberValidator();

// use the validator
validate('0xff'); // 255
validate('123');  // 123 (number)
validate(12);     // 12

validate('foo'); // throws InvalidData Error

Validators

stringValidator

Validates strings. It only accepts strings as inputs.

import { stringValidator } from '@foobarhq/validators';

// these are the default options.
const validate = stringValidator({
  // can be either true or 'both' to use String.prototype.trim,
  // 'start' for String.prototype.trimStart
  // 'end' for String.prototype.trimEnd
  // false for no trimming,
  // or a function with the following signature: (string) => string
  trim: true,
  minLength: 0,
  maxLength: Number.POSITIVE_INFINITY,

  // wether the input can be null.
  allowNull: false,

  // the value to return if the input is undefined.
  defaultValue: undefined,
});

validate(' 123 '); // 123

numberValidator

Validates numbers. It accepts strings and numbers as inputs.

import { numberValidator } from '@foobarhq/validators';

// these are the default options.
const validate = numberValidator({

  // refuse numbers < 0
  unsigned: false,

  // refuse non safe integers and floats
  integer: false,

  // accept Infinity and -Infinity
  allowInfinite: false,

  // minimum value for the input
  min: -Infinity,

  // maximum value for the input
  max: +Infinity,

  allowNull: false,
  defaultValue: undefined,
});

validate('Infinity'); // throws
validate('-Infinity'); // throws
validate('10.5'); // 10.5
validate(456); // 456

booleanValidator

Parses booleans. It accepts strings and booleans as inputs.

import { booleanValidator } from '@foobarhq/validators';

// these are the default options.
const validate = booleanValidator({
  allowNull: false,
  defaultValue: undefined,
});

validate('t'); // true
validate('f'); // false
validate('true'); // true
validate(false); // false

dateValidator

Parses dates. It accepts strings and native Date objects as inputs.

import { dateValidator } from '@foobarhq/validators';

// these are the default options.
const validate = dateValidator({
  allowNull: false,
  defaultValue: undefined,
});

validate('2011-10-05T14:48:00.000Z'); // new Date object
validate(new Date('2011-10-05T14:48:00.000Z')); // the date object
validate('true'); // throws

enumValidator

Restricts its input to a set of values.

import { enumValidator } from '@foobarhq/validators';

// these are the default options.
// with an array of values:
const validate = enumValidator([1, 2, 3], {
  allowNull: false,
  defaultValue: undefined,
});

validate(1); // 1
validate(3); // 3
validate(4); // throws

// with an object:
const validate = enumValidator({
  ITEM_1: 1,
  ITEM_2: 'banana',
});

validate(1); // throws
validate('ITEM_1'); // 1
validate('ITEM_2'); // banana

noValidate

The noop of validators, doesn't parse, doesn't validate, just returns its input.

import { noValidate } from '@foobarhq/validators';

// no option available
const validate = noValidate();

validate(1); // 1
validate(3); // 3
validate(' 1_2_3 '); // ' 1_2_3 '

arrayValidator

A composable validator which validates an array and its items.

import { arrayValidator, numberValidator } from '@foobarhq/validators';

// these are the default options.
// without an item validator
const validate = arrayValidator({
  allowNull: false,
  defaultValue: undefined,

  // minimum amount of items in the array.
  min: 0,

  // maximum amount of items in the array.
  max: Number.POSITIVE_INFINITY,

  // in strict mode, throws if the input is not an array. Otherwise, wraps the input in a new array.
  strict: false,

  // remove any duplicate from the array
  unique: false,
});

validate(1); // [1]
validate([1, 2, 3]); // [1, 2, 3]

// with an item validator:
const validate = arrayValidator(numberValidator(), {
  strict: true,
});

validate(1); // throws
validate(['0x00', '0b0', '0', 0]); // [0, 0, 0, 0]

structValidator

Validates an object structure. Accepts simple objects as inputs.

import { structValidator, numberValidator } from '@foobarhq/validators';

// these are the default options.
const validate = structValidator({
  item1: numberValidator(),
}, {
  allowNull: false,
  defaultValue: undefined,
});

validate({}); // throws
validate({ item1: '10' }) // { item1: 5 }

// the options of used validators still apply
const validate = structValidator({
  item1: numberValidator({ defaultValue: 10 }),
}, {
  allowNull: false,
  defaultValue: undefined,
});

validate({}); // { item1: 10 }
validate({ item1: '5' }) // { item1: 5 }