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 🙏

© 2026 – Pkg Stats / Ryan Hefner

favalid

v0.9.1

Published

validator framework

Readme

favalid

Greenkeeper badge

Validator Framework for javascript.

npm version CircleCI Test Coverage Maintainability

Getting Started

Prerequisites

  • Node.js v8+
  • npm or yarn

Installing

$ npm install favalid

Usage

Primitive Validator

  1. Write higher order validator using tester(). tester() takes two arguments. first argument is (value: any) => bool function, which represent validation rules. Second argument is () => string function, which represents validation error message.
import { tester } from 'favalid';

const validator = tester((targetValue) => {
  return targetValue < 100;
}, () => {
  return 'must be less than 100';
})
  1. Use it! tester() returns simple validator function, (value: any) => ({error: bool, message: string}).
console.log(validator(99)); // => { error: false, message: '' }
console.log(validator(101)); // => { error: true, message: 'must ne less than 100' }

Combined Validator

  1. You can combine these validators using combine(...validators). combine() returns also validator.
import { tester, combine } from 'favalid';
const validator1 = tester((targetValue) => {
  return targetValue < 100;
}, () => {
  return 'must be less than 100';
})

const validator2 = tester((targetValue) => {
  return targetValue > 20;
}, () => {
  return 'must be higher than 20';
})

const combinedValidator = combine(validator1, validator2)
  1. Use it.
console.log(combinedValidator(80)) // { error: false, message: '' }

console.log(combinedValidator(101)) // { error: true, message: 'must be less than 100' }

console.log(combinedValidator(19)) // { error: true, message: 'must be higher than 20' }

If target values are failed multiple tests, combined validator returns first failed message.

import { tester, combine } from 'favalid';
const minLength = tester((targetValue) => {
  return [...targetValue].length > 2;
}, () => {
  return 'too few letters';
})

const regex = tester((targetValue) => {
  return /^[a-zA-Z]+$/.test(targetValue) // only contains alphabet letters.
}, () => {
  return 'invalid format';
})

const combinedValidator = combine(minLength, regex)

console.log(combinedValidator('a')) // { error: true, message: 'too few letters' }

console.log(combinedValidator('1')) // { error: true, message: 'too few letters' }

console.log(combinedValidator('asdf1')) // {  error: true, message: 'invalid format' }

Using Predefined Higher Order Validators

You can also use predefined higher order validators found on favalid/lib/validators. These validators can be combined with your original validators.

import { tester, combine, minLength, regexp } from 'favalid';
 
 const combinedValidator = combine(
   minLength(3, () => 'too few letters'), 
   regexp(/^[a-zA-Z]+$/, () => "invalid format", {}),
   tester(() => {
     // some original validation rules
   }, () => ('original error')),
 )

Validation Result Reducers

You can aggregate validate errors with your customize error reducer using conbineWithReducer(validators, reducer, initialValue).

import { tester, combineWithReducer, minLength, maxLength, regexp } from 'favalid';

const REQUIRED_EMAIL_MESSAGE = () => "required.";

const EMAIL_REGEXP = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
const REGEXP_MESSAGE = () => "invalid email.";

const EMAIL_MAX_LENGTH = 100;
const MAX_LENGTH_MESSAGE = () => "exceeds 100 letters.";

const EMAIL_MIN_LENGTH = 10;
const MIN_LENGTH_MESSAGE = () => "at least 10 letters.";

const emailValidatorWithMultipleErrorReducer = (email: string) => {
  const reducer = (prevError, currentError) => {
    if (currentError.error) {
      prevError.push(currentError);
    }
    return prevError;
  };

  return combineWithReducer(
    [
      required(REQUIRED_EMAIL_MESSAGE),
      minLength(EMAIL_MIN_LENGTH, MIN_LENGTH_MESSAGE),
      maxLength(EMAIL_MAX_LENGTH, MAX_LENGTH_MESSAGE),
      regexp(EMAIL_REGEXP, REGEXP_MESSAGE, {}),
    ],
    reducer,
    [],
  )(email);
};

console.log(emailValidatorWithMultipleErrorReducer('[email protected]')) // => []
console.log(emailValidatorWithMultipleErrorReducer('aaa')) // => [{error: true, message: 'at least 10 letters.'}, {error: true, message: 'invalid email.'}]

Examples

See src/examples.

License

This project is licensed under the Apache License 2.0 License - see the LICENSE file for details