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

fun-validation

v1.0.4

Published

🥳 JS Functional validation library

Downloads

4

Readme

Fun validation logo

JS Functional validation library

npm

Installation

npm install fun-validation
yarn add fun-validation

Usage

This library exports a bunch of small validation functions that all have the following signature:

(value: any) => boolean
// or when the validation requires additional parameters, we have currying:
(n:number) => (value: any) => boolean

So you would call a function like this:

import { isEmail } from 'fun-validation';

const isEmailValid = isEmail(someString);

The library also exports the validate function.

This function has the following signature:

(value: any, rules) => validationResult;

Where:

  • rules must have the same shape as the value that was passed in
  • and consequently the validationResult will also be in that same shape

Meaning:

  • If the value is a primitive (string, number...etc), the rules must be a validation fn with the signature: (value: any) => boolean, and the validationResult will be a boolean
import { validate, isString, isInteger, isNumberMax } from 'fun-validation';

const someString = 'this is a string';

validate(someString, isString); // returns boolean

// or, you can compose your own validation fn

const someNumber = 3;

validate(someNumber, value => isInteger(value) && isNumberMax(5)(value)); // returns boolean
  • If the value is an object (a plain object), the rules must an object with the same shape as value but the leaf nodes (primitives) have validation fns as values, and the validationResult will be and object in the same shape
import { validate, isString } from 'fun-validation';
import { isDate } from 'date-fns';

const user = {
  name: 'Dusan',
  dateOfBirth: new Date(1992, 9, 2),
};

const rules = {
  name: isString,
  dateOfBirth: isDate,
};

const result = validate(user, rules);

// result
{
  name: boolean;
  dateOfBirth: boolean;
}
  • If the value is an array, the rules must an array with two elements. First element is a validation fn and is used to validate the array itself (for example length of the array). The second element corresponds to the shape of the element in the value array. The validationResult will be an array with the first element as boolean (result of validating the array itself), and the second element as an array of booleans (result of validation for each of the elements in the value array).
import { validate, isArray, isString, isInteger } from 'fun-validation';

// array of primitives

const hobbies = ['tennis', 'basketball'];

const rules = [isArray, isString];

const result = validate(hobbies, rules);

// result
[true, [true, true]];

// array of objects

const friends = [
  { name: 'Dusan', age: 29 },
  { name: 'Peter', age: 33 },
];

const rules = [isArray, { name: isString, age: isInteger }];

const result = validate(friends, rules);

// result
[
  true,
  [
    { name: true, age: true },
    { name: true, age: true },
  ],
];

You get the picture!

So, to recapitulate:

  • The rules must be of the same shape as value with the leaf nodes (primitives) being functions that look like this: (value: any) => boolean
  • The validationResult is more or less of the same shape as value and rules (except in the case of arrays) while the leaf nodes are always booleans

API

const validate: (value: any, rules) => validationResult

const isString: (value: any): boolean;

const isStringLongerThan: (len: number) => (value: any) => boolean

const isStringShorterThan:(len: number) => (value: any) => boolean

const isStringOfLength: (len: number) => (value: any) => boolean

const isStringOfMinLength: (len: number) => (value: any) => boolean

const isStringOfMaxLength:(len: number) => (value: any) => boolean                                                                                                       7

const isFunction: (obj: any) => boolean

const isObject: (value: any) => boolean

const isPromise: (value: any) => boolean

const isArray: (value: any) => boolean

const isArrayLongerThan: (len: number) => (value: any) => boolean

const isArrayShorterThan: (len: number) => (value: any) => boolean

const isArrayOfLength: (len: number) => (value: any) => boolean

const isArrayMinLength: (len: number) => (value: any) => boolean

const isArrayMaxLength: (len: number) => (value: any) => boolean

const isNumber: (value: any) => boolean

const isInteger: (value: any) => boolean

const isFloat: (value: any) => boolean

const isNumberMoreThan: (n: number) => (value: any) => boolean

const isNumberLessThan: (n: number) => (value: any) => boolean

const isNumberEqual: (n: number) => (value: any) => boolean

const isNumberMin: (n: number) => (value: any) => boolean

const isNumberMax: (n: number) => (value: any) => boolean

const isPattern: (regex: RegExp) => (value: any) => boolean

const isEmail: (value: any) => boolean

const isUrl: (value: any) => boolean